-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
63 lines (54 loc) · 1.68 KB
/
errors.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
package http
import (
"encoding/json"
"net/http"
"github.com/rs/xid"
"github.com/rs/zerolog"
)
type errResponse struct {
Message string `json:"message"`
Code int `json:"code"`
ErrID string `json:"errID"`
}
// ErrBadRequest writes a bad request err response
func (s *Server) ErrBadRequest(w http.ResponseWriter, err error, msg string) {
RequestErr(http.StatusBadRequest, s.log, w, err, msg)
}
// ErrInternal writes an internal err response
func (s *Server) ErrInternal(w http.ResponseWriter, err error, msg string) {
RequestErr(http.StatusInternalServerError, s.log, w, err, msg)
}
// ErrNotFound writes a not found err response
func (s *Server) ErrNotFound(w http.ResponseWriter, err error, msg string) {
RequestErr(http.StatusNotFound, s.log, w, err, msg)
}
// NewErr returns a masked error response with ID and human-readable message.
// The ID can be used to associate the response with a server log record
// to reconstruct the full error message.
func NewErr(status int, msg string) errResponse {
errID := makeErrID()
return errResponse{
Message: msg,
Code: status,
ErrID: errID,
}
}
// RequestErr handles logs an error and writes an error response
func RequestErr(status int, log zerolog.Logger, w http.ResponseWriter, err error, msg string) {
res := NewErr(status, msg)
var lvl zerolog.Level
if status >= 500 {
lvl = zerolog.ErrorLevel
}
log.WithLevel(lvl).Str("errID", res.ErrID).Err(err).Msg(msg)
b, _ := json.Marshal(res)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
_, err = w.Write(b)
if err != nil {
log.Error().Err(err).Msg("error writing to response")
}
}
var makeErrID = func() string {
return xid.New().String()
}