/
response.go
73 lines (60 loc) · 1.92 KB
/
response.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
package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
logging "github.com/Decentr-net/logrus/context"
)
// Error ...
// swagger:model
type Error struct {
Error string `json:"error"`
}
// WriteErrorf writes formatted error.
func WriteErrorf(w http.ResponseWriter, status int, format string, args ...interface{}) {
body, _ := json.Marshal(Error{
Error: fmt.Sprintf(format, args...),
})
w.WriteHeader(status)
// nolint:gosec,errcheck
w.Write(body)
}
// WriteError writes error.
func WriteError(w http.ResponseWriter, status int, message string) {
WriteErrorf(w, status, message)
}
// WriteVerifyError writes sign verification(auth) error with proper status.
func WriteVerifyError(ctx context.Context, w http.ResponseWriter, err error) {
switch {
case errors.Is(err, ErrNotVerified):
WriteError(w, http.StatusUnauthorized, err.Error())
case errors.Is(err, ErrInvalidRequest):
WriteError(w, http.StatusBadRequest, err.Error())
default:
WriteInternalError(ctx, w, err, "verify error")
}
}
// WriteInternalError logs error and writes internal error.
func WriteInternalError(ctx context.Context, w http.ResponseWriter, err error, message string) {
WriteInternalErrorf(ctx, w, err, message)
}
// WriteInternalErrorf logs formatted error and writes internal error.
func WriteInternalErrorf(ctx context.Context, w http.ResponseWriter, err error, format string, args ...interface{}) {
if err != nil {
logging.GetLogger(ctx).WithError(err).Errorf(format, args...)
} else {
logging.GetLogger(ctx).Errorf(format, args...)
}
// We don't want to expose internal error to user. So we will just send typical error.
WriteError(w, http.StatusInternalServerError, "internal error")
}
// WriteOK writes json body.
func WriteOK(w http.ResponseWriter, status int, v interface{}) {
body, _ := json.Marshal(v)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
// nolint:gosec,errcheck
w.Write(body)
}