-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.go
46 lines (35 loc) · 908 Bytes
/
utils.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
package main
import (
"encoding/json"
"net/http"
"github.com/getsentry/sentry-go"
)
type response struct {
Success bool `json:"success"`
Message string `json:"message"`
}
func sendJSONResponse(w http.ResponseWriter, r *http.Request, err error) {
var statusCode int
resp := &response{}
if err == nil {
statusCode = http.StatusOK
resp.Success = true
} else {
statusCode = http.StatusInternalServerError
resp.Success = false
resp.Message = err.Error()
sentry.CaptureException(err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(statusCode)
_ = json.NewEncoder(w).Encode(resp)
}
func sendEndpointResponse(w http.ResponseWriter, r *http.Request, endpoint *Endpoint) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
err := json.NewEncoder(w).Encode(endpoint)
if err != nil {
sendJSONResponse(w, r, err)
return
}
}