This repository has been archived by the owner on May 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
170 lines (149 loc) · 5.01 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
168
169
170
package router
import (
"context"
"fmt"
"net/http"
"os"
"runtime/debug"
session "github.com/chaostreff-flensburg/moc/context"
)
// HTTPError is an error with a message and an HTTP status code.
type HTTPError struct {
Object string `json:"object"`
Code int `json:"code"`
Message string `json:"msg"`
Json interface{} `json:"json"`
InternalError error `json:"-"`
InternalMessage string `json:"-"`
ErrorID string `json:"error_id,omitempty"`
}
// ======================================
// Return bad request error
// ======================================
func BadRequestError(fmtString string, args ...interface{}) *HTTPError {
return httpError(http.StatusBadRequest, fmtString, args...)
}
// ======================================
// Return internal server error
// ======================================
func InternalServerError(fmtString string, args ...interface{}) *HTTPError {
return httpError(http.StatusInternalServerError, fmtString, args...)
}
// ======================================
// Return not found error
// ======================================
func NotFoundError(fmtString string, args ...interface{}) *HTTPError {
return httpError(http.StatusNotFound, fmtString, args...)
}
// ======================================
// Return unauthorized error
// ======================================
func UnauthorizedError(fmtString string, args ...interface{}) *HTTPError {
return httpError(http.StatusUnauthorized, fmtString, args...)
}
// ======================================
// Return unavailable service error
// ======================================
func UnavailableServiceError(fmtString string, args ...interface{}) *HTTPError {
return httpError(http.StatusServiceUnavailable, fmtString, args...)
}
// ======================================
// Return error as string
// ======================================
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
}
// ======================================
// WithJsonError
// ======================================
func (e *HTTPError) WithJsonError(json interface{}) *HTTPError {
e.Json = json
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{
Object: "error",
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) {
ctx := r.Context()
defer func() {
if rvr := recover(); rvr != nil {
log := session.GetLogger(ctx)
if log != nil {
log.Panic(rvr, debug.Stack())
} else {
fmt.Fprintf(os.Stderr, "Panic: %+v\n", rvr)
debug.PrintStack()
}
se := &HTTPError{
Object: "error",
Code: http.StatusInternalServerError,
Message: http.StatusText(http.StatusInternalServerError),
}
handleError(se, w, r)
}
}()
return nil, nil
}
// ======================================
// Handle Errors
// ======================================
func handleError(err error, w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
log := session.GetLogger(ctx)
errorID := session.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)
}
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(`{object:"error",code":500,"msg":"Internal server error","error_id":"` + *errorID + `"}`)); writeErr != nil {
log.WithError(writeErr).Error("Error writing generic error message")
}
}
}