-
Notifications
You must be signed in to change notification settings - Fork 5
/
replies.go
60 lines (52 loc) · 1.47 KB
/
replies.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
package handlers
import (
"encoding/json"
"net/http"
"github.com/clems4ever/go-graphkb/internal/schema"
"github.com/sirupsen/logrus"
)
// ReplyWithSourceGraph sends a reply containing the schema graph
func ReplyWithSourceGraph(w http.ResponseWriter, sg *schema.SchemaGraph) {
responseJSON, err := json.Marshal(sg)
if err != nil {
ReplyWithInternalError(w, err)
return
}
if _, err := w.Write(responseJSON); err != nil {
ReplyWithInternalError(w, err)
}
}
// ReplyWithInternalError send response with internal error.
func ReplyWithInternalError(w http.ResponseWriter, err error) {
logrus.Error(err)
w.WriteHeader(http.StatusInternalServerError)
_, werr := w.Write([]byte(err.Error()))
if werr != nil {
logrus.Error(werr)
}
}
// ReplyWithBadRequest send response with bad request.
func ReplyWithBadRequest(w http.ResponseWriter, err error) {
logrus.Error(err)
w.WriteHeader(http.StatusBadRequest)
_, werr := w.Write([]byte(err.Error()))
if werr != nil {
logrus.Error(werr)
}
}
// ReplyWithUnauthorized send unauthorized response.
func ReplyWithUnauthorized(w http.ResponseWriter) {
w.WriteHeader(http.StatusUnauthorized)
_, werr := w.Write([]byte("Unauthorized"))
if werr != nil {
logrus.Error(werr)
}
}
// ReplyWithTooManyRequests send unauthorized response.
func ReplyWithTooManyRequests(w http.ResponseWriter) {
w.WriteHeader(http.StatusTooManyRequests)
_, werr := w.Write([]byte("Too Many Requests. Retry later."))
if werr != nil {
logrus.Error(werr)
}
}