/
responses.go
85 lines (74 loc) · 2.5 KB
/
responses.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package utils
import (
"encoding/json"
"log"
"net/http"
"github.com/flagship-io/flagship-proto/decision_response"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/types/known/wrapperspb"
)
// ClientErrorMessage represents a bad request response
type ClientErrorMessage struct {
Message string `json:"message"`
}
// WriteServerError returns a 500 Internal Server Error response
func WriteServerError(w http.ResponseWriter, err error) {
body := &ClientErrorMessage{Message: err.Error()}
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/json")
jsonErr := json.NewEncoder(w).Encode(body)
if jsonErr != nil {
log.Printf("error when encoding body: %v", jsonErr)
}
}
// WriteClientError similarly add a helper for send responses relating to client errors.
func WriteClientError(w http.ResponseWriter, status int, message string) {
body := &ClientErrorMessage{Message: message}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
jsonErr := json.NewEncoder(w).Encode(body)
if jsonErr != nil {
log.Printf("error when encoding body: %v", jsonErr)
}
}
// WriteJSONStringOk similarly add a helper to send json stringified responses with status OK.
func WriteJSONStringOk(w http.ResponseWriter, data string) {
w.Header().Set("Content-Type", "application/json")
_, writeErr := w.Write([]byte(data))
if writeErr != nil {
log.Printf("error when writing body: %v", writeErr)
}
}
// WriteJSONOk similarly add a helper to send json responses with status OK.
func WriteJSONOk(w http.ResponseWriter, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(200)
err := json.NewEncoder(w).Encode(data)
if err != nil {
WriteServerError(w, err)
return
}
}
// WriteNoContent similarly add a helper to send 204 responses .
func WriteNoContent(w http.ResponseWriter) {
w.WriteHeader(204)
}
// WritePanicResponse writes a response for panic mode
func WritePanicResponse(w http.ResponseWriter, visitorID *wrapperspb.StringValue) {
decisionResponse := decision_response.DecisionResponsePanic{}
decisionResponse.Campaigns = []*decision_response.Campaign{}
decisionResponse.VisitorId = visitorID
decisionResponse.Panic = true
marshalOptions := protojson.MarshalOptions{
EmitUnpopulated: true,
}
data, err := marshalOptions.Marshal(&decisionResponse)
if err != nil {
WriteServerError(w, err)
return
}
_, writeErr := w.Write(data)
if writeErr != nil {
log.Printf("error when writing body: %v", writeErr)
}
}