-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
388 lines (331 loc) · 9.73 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package webutils
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"strings"
"time"
qerrors "github.com/cyberhorsey/errors"
echo "github.com/labstack/echo/v4"
"github.com/neko-neko/echo-logrus/v2/log"
"github.com/sirupsen/logrus"
"google.golang.org/grpc/codes"
)
var logger *log.MyLogger
func init() {
logger = log.Logger()
logger.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: time.RFC3339,
FieldMap: logrus.FieldMap{
logrus.FieldKeyMsg: "message",
},
})
}
// sentinel errors
var (
ErrNoClaims = errors.New("claims is required")
ErrNoKey = errors.New("key is required")
ErrNoSecret = errors.New("secret is required")
ErrNoToken = errors.New("token is required")
ErrInvalidToken = errors.New("jwt token not valid")
ErrNoJWTClaimsInContext = errors.New("jwt claim missing from context")
ErrNoJWTInContext = errors.New("jwt missing from context")
ErrNoNotificationMessage = qerrors.New("message is required")
ErrNoPublicKeyFunction = qerrors.New("public key func is required")
ErrAuthorizationTokenInvalid = qerrors.Unauthorized.NewWithKeyAndDetail(
"ERR_AUTHORIZATION_TOKEN_INVALID",
"Authorization token is invalid",
)
ErrAuthorizationAccessTokenRequired = qerrors.Unauthorized.NewWithKeyAndDetail(
"ERR_AUTHORIZATION_ACCESS_TOKEN_REQUIRED",
"A valid Authorization access token is required",
)
ErrAuthorizationBearerRequired = qerrors.Unauthorized.NewWithKeyAndDetail(
"ERR_AUTHORIZATION_BEARER_REQUIRED",
"Authorization Bearer is required before token",
)
)
// Logger returns a middleware that logs HTTP requests.
func Logger() echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
path := c.Request().URL.Path
if strings.Contains(path, "health") || strings.Contains(path, "metric") {
return nil
}
req := c.Request()
res := c.Response()
start := time.Now()
var err error
if err = next(c); err != nil {
c.Error(err)
}
stop := time.Now()
pid := req.Header.Get(ProvenanceIDHeader)
if pid == "" {
pid = res.Header().Get(ProvenanceIDHeader)
}
rid, ok := RequestIDFromContext(c.Request().Context())
if !ok {
rid = ""
}
logger := log.Logger()
logger.SetFormatter(&logrus.JSONFormatter{
TimestampFormat: time.RFC3339,
FieldMap: logrus.FieldMap{
logrus.FieldKeyMsg: "message",
},
})
l := logger.WithFields(logrus.Fields{
"provenanceId": pid,
"requestId": rid,
"ip": c.RealIP(),
"host": req.Host,
"method": req.Method,
"uri": req.RequestURI,
"status": res.Status,
"latency": stop.Sub(start).Seconds(),
"referer": req.Referer(),
"userAgent": req.UserAgent(),
})
if res.Status == http.StatusOK || res.Status == http.StatusNoContent {
l.Logger.Out = os.Stdout
l.Info("successful http call")
} else {
l.Logger.Out = os.Stderr
l.Error(fmt.Sprintf("%+v", err))
}
return err
}
}
}
// Error is a struct we return through RenderErrors to be able to return multiple errors at once
// from our API.
type Error struct {
Cause error `json:"-"`
Key string `json:"key,omitempty"`
Title string `json:"title"`
Detail string `json:"detail,omitempty"`
}
func (e Error) Error() string {
if e.Cause != nil {
return e.Cause.Error()
}
parts := make([]string, 0)
if e.Key != "" {
parts = []string{e.Key}
}
if e.Title != "" {
parts = append(parts, e.Title)
}
if e.Detail != "" {
parts = append(parts, e.Detail)
}
return strings.Join(parts, ": ")
}
// newUnexpectedError wrapps err in our defactor Internal Server Error
func newUnexpectedError(err error) Error {
return Error{
Cause: err,
Key: "ERR_UNEXPECTED",
Title: http.StatusText(http.StatusInternalServerError),
Detail: "An unexpected error occurred.",
}
}
// ErrorResponse contains a collection of errors
type ErrorResponse struct {
Errors []Error `json:"errors"`
}
// MarshalJSON marshals errors
func (er ErrorResponse) MarshalJSON() (
[]byte,
error,
) {
bs, err := json.Marshal(er.Errors)
if err != nil {
return nil, qerrors.Wrap(err, "json.Marshal(er.Errors)")
}
return append(
[]byte("{\"errors\":"),
append(
bs,
[]byte("}")...,
)...,
), nil
}
// UnmarshalJSON unmarshals errors
func (er *ErrorResponse) UnmarshalJSON(bs []byte) error {
var errorsResult struct {
Errors []Error `json:"errors"`
}
if err := json.Unmarshal(bs, &errorsResult); err != nil {
return qerrors.Wrap(err, "json.Unmarshal(bs, &errorsResult)")
}
er.Errors = errorsResult.Errors
for i := range er.Errors {
detail := ""
if er.Errors[i].Title != "" {
detail = strings.Join(
[]string{
er.Errors[i].Title,
er.Errors[i].Detail,
},
": ",
)
} else {
detail = er.Errors[i].Detail
}
er.Errors[i].Cause = qerrors.NoType.NewWithKeyAndDetail(er.Errors[i].Key, detail)
}
return nil
}
// Error messages combined
func (er ErrorResponse) Error() string {
var errMsgs []string
for _, err := range er.Errors {
errMsgs = append(errMsgs, err.Error())
}
return strings.TrimSuffix(strings.Join(errMsgs, "; "), ";")
}
// RenderErrors prepares collection of errors in an ErrorResponse
func RenderErrors(errs ...error) ErrorResponse {
convertedErrs := make([]Error, 0)
for _, err := range errs {
convertedErrs = append(convertedErrs, convertError(err))
}
return ErrorResponse{Errors: convertedErrs}
}
func convertError(err error) Error {
// If the error is already a webutils.Error; return it
if werr, ok := err.(Error); ok {
return werr
}
var title string
switch qerrors.GetType(err) {
case qerrors.BadRequest:
title = http.StatusText(http.StatusBadRequest)
case qerrors.Forbidden:
title = http.StatusText(http.StatusForbidden)
case qerrors.Unauthorized:
title = http.StatusText(http.StatusUnauthorized)
case qerrors.Validation:
title = http.StatusText(http.StatusUnprocessableEntity)
case qerrors.InvalidParameter:
title = http.StatusText(http.StatusUnprocessableEntity)
case qerrors.NotFound:
title = http.StatusText(http.StatusNotFound)
case qerrors.MissingParameter:
title = "Missing Parameter"
case qerrors.NoType:
// Errors of unknown/default type should not be exposed
return newUnexpectedError(err)
}
return Error{
Cause: err,
Key: qerrors.Key(err),
Title: title,
Detail: qerrors.Detail(err),
}
}
// RenderUnexpectedError renders an unexpected error
func RenderUnexpectedError(err error) ErrorResponse {
e := make([]Error, 0)
e = append(e, newUnexpectedError(err))
errResp := ErrorResponse{Errors: e}
return errResp
}
// ConvertErrorToStatusCode converts err to an HTTP status code using qerrors.GetType. If the
// type is qerrors.NoType or is unknown, http.StatusInternalServerError is used.
func ConvertErrorToStatusCode(err error) int {
switch qerrors.GetType(err) {
case qerrors.BadRequest, qerrors.MissingParameter:
return http.StatusBadRequest
case qerrors.Forbidden:
return http.StatusForbidden
case qerrors.Unauthorized:
return http.StatusUnauthorized
case qerrors.Validation:
return http.StatusUnprocessableEntity
case qerrors.InvalidParameter:
return http.StatusUnprocessableEntity
case qerrors.NotFound:
return http.StatusNotFound
default:
return http.StatusInternalServerError
}
}
// ConvertErrorToGRPCCode converts err to a GRPC error code using qerrors.GetType. If the
// type is qerrors.NoType or is unknown, codes.Unknown is used.
func ConvertErrorToGRPCCode(err error) codes.Code {
switch qerrors.GetType(err) {
case qerrors.BadRequest, qerrors.MissingParameter,
qerrors.Validation, qerrors.InvalidParameter:
return codes.InvalidArgument
case qerrors.Forbidden, qerrors.Unauthorized:
return codes.PermissionDenied
case qerrors.NotFound:
return codes.NotFound
default:
return codes.Unknown
}
}
// ConvertGRPCCodeToStatusCode converts err to an HTTP status code. If the
// type is codes.Unknown or is unknown, http.StatusInternalServerError is used.
func ConvertGRPCCodeToStatusCode(err codes.Code) int {
switch err {
case codes.OK:
return http.StatusOK
case codes.InvalidArgument:
return http.StatusBadRequest
case codes.PermissionDenied:
return http.StatusForbidden
case codes.NotFound:
return http.StatusNotFound
default:
return http.StatusInternalServerError
}
}
// LogAndRenderErrors logs and renders errs to JSON with the provided statusCode.
func LogAndRenderErrors(c echo.Context, statusCode int, errs ...error) error {
errResp := RenderErrors(errs...)
// Log error stack trace
pid, ok := ProvenanceIDFromContext(c.Request().Context())
if !ok {
pid = ""
}
rid, ok := RequestIDFromContext(c.Request().Context())
if !ok {
rid = ""
}
for _, err := range errs {
logger.WithFields(logrus.Fields{"provenanceId": pid, "requestId": rid}).
Error(err)
}
jsonErr := c.JSON(statusCode, errResp)
if jsonErr != nil {
logger.WithFields(logrus.Fields{"provenanceId": pid, "requestId": rid}).Error(jsonErr)
}
return errResp
}
// LogAndRenderUnexpectedError logs the stack trace for err and renders a generic internal server
// error message via `RenderUnexpectedAPIError()`.
func LogAndRenderUnexpectedError(c echo.Context, err error) error {
// Log error stack trace
pid, ok := ProvenanceIDFromContext(c.Request().Context())
if !ok {
pid = ""
}
rid, ok := RequestIDFromContext(c.Request().Context())
if !ok {
rid = ""
}
logger.WithFields(logrus.Fields{"provenanceId": pid, "requestId": rid}).Error(err)
jsonErr := c.JSON(http.StatusInternalServerError, RenderUnexpectedError(err))
if jsonErr != nil {
logger.WithFields(logrus.Fields{"provenanceId": pid, "requestId": rid}).Error(jsonErr)
}
// return the original error which will be logged with Echo's access log
return err
}