-
Notifications
You must be signed in to change notification settings - Fork 88
/
errors.go
167 lines (144 loc) · 4.66 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package api
import (
"context"
"fmt"
"net/http"
"os"
"runtime/debug"
)
// OAuthError is the JSON handler for OAuth2 error responses
type OAuthError struct {
Err string `json:"error"`
Description string `json:"description,omitempty"`
InternalError error `json:"-"`
InternalMessage string `json:"-"`
}
func (e *OAuthError) Error() string {
if e.InternalMessage != "" {
return e.InternalMessage
}
return fmt.Sprintf("%s: %s", e.Err, e.Description)
}
// WithInternalError adds internal error information to the error
func (e *OAuthError) WithInternalError(err error) *OAuthError {
e.InternalError = err
return e
}
// WithInternalMessage adds internal message information to the error
func (e *OAuthError) WithInternalMessage(fmtString string, args ...interface{}) *OAuthError {
e.InternalMessage = fmt.Sprintf(fmtString, args...)
return e
}
// Cause returns the root cause error
func (e *OAuthError) Cause() error {
if e.InternalError != nil {
return e.InternalError
}
return e
}
func oauthError(err string, description string) *OAuthError {
return &OAuthError{Err: err, Description: description}
}
func badRequestError(fmtString string, args ...interface{}) *HTTPError {
return httpError(http.StatusBadRequest, fmtString, args...)
}
func internalServerError(fmtString string, args ...interface{}) *HTTPError {
return httpError(http.StatusInternalServerError, fmtString, args...)
}
func notFoundError(fmtString string, args ...interface{}) *HTTPError {
return httpError(http.StatusNotFound, fmtString, args...)
}
func unauthorizedError(fmtString string, args ...interface{}) *HTTPError {
return httpError(http.StatusUnauthorized, fmtString, args...)
}
func unprocessableEntityError(fmtString string, args ...interface{}) *HTTPError {
return httpError(http.StatusUnprocessableEntity, fmtString, args...)
}
// HTTPError is an error with a message and an HTTP status code.
type HTTPError struct {
Code int `json:"code"`
Message string `json:"msg"`
InternalError error `json:"-"`
InternalMessage string `json:"-"`
ErrorID string `json:"error_id,omitempty"`
}
func (e *HTTPError) Error() string {
if e.InternalMessage != "" {
return e.InternalMessage
}
return fmt.Sprintf("%d: %s", e.Code, e.Message)
}
// Cause returns the root cause error
func (e *HTTPError) Cause() error {
if e.InternalError != nil {
return e.InternalError
}
return e
}
// WithInternalError adds internal error information to the error
func (e *HTTPError) WithInternalError(err error) *HTTPError {
e.InternalError = err
return e
}
// WithInternalMessage adds internal message information to the error
func (e *HTTPError) WithInternalMessage(fmtString string, args ...interface{}) *HTTPError {
e.InternalMessage = fmt.Sprintf(fmtString, args...)
return e
}
func httpError(code int, fmtString string, args ...interface{}) *HTTPError {
return &HTTPError{
Code: code,
Message: fmt.Sprintf(fmtString, args...),
}
}
// Recoverer is a middleware that recovers from panics, logs the panic (and a
// backtrace), and returns a HTTP 500 (Internal Server Error) status if
// possible. Recoverer prints a request ID if one is provided.
func recoverer(w http.ResponseWriter, r *http.Request) (context.Context, error) {
defer func() {
if rvr := recover(); rvr != nil {
logEntry := getLogEntry(r)
if logEntry != nil {
logEntry.Panic(rvr, debug.Stack())
} else {
fmt.Fprintf(os.Stderr, "Panic: %+v\n", rvr)
debug.PrintStack()
}
se := &HTTPError{
Code: http.StatusInternalServerError,
Message: http.StatusText(http.StatusInternalServerError),
}
handleError(se, w, r)
}
}()
return nil, nil
}
func handleError(err error, w http.ResponseWriter, r *http.Request) {
log := getLogEntry(r)
errorID := getRequestID(r.Context())
switch e := err.(type) {
case *HTTPError:
if e.Code >= http.StatusInternalServerError {
e.ErrorID = errorID
// this will get us the stack trace too
log.WithError(e.Cause()).Error(e.Error())
} else {
log.WithError(e.Cause()).Info(e.Error())
}
if jsonErr := sendJSON(w, e.Code, e); jsonErr != nil {
handleError(jsonErr, w, r)
}
case *OAuthError:
log.WithError(e.Cause()).Info(e.Error())
if jsonErr := sendJSON(w, http.StatusBadRequest, e); jsonErr != nil {
handleError(jsonErr, w, r)
}
default:
log.WithError(e).Errorf("Unhandled server error: %s", e.Error())
// hide real error details from response to prevent info leaks
w.WriteHeader(http.StatusInternalServerError)
if _, writeErr := w.Write([]byte(`{"code":500,"msg":"Internal server error","error_id":"` + errorID + `"}`)); writeErr != nil {
log.WithError(writeErr).Error("Error writing generic error message")
}
}
}