-
Notifications
You must be signed in to change notification settings - Fork 617
/
errors.go
80 lines (65 loc) · 1.98 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
package mid
import (
"context"
"net/http"
"github.com/ardanlabs/service/business/web/v1/auth"
"github.com/ardanlabs/service/business/web/v1/response"
"github.com/ardanlabs/service/foundation/logger"
"github.com/ardanlabs/service/foundation/validate"
"github.com/ardanlabs/service/foundation/web"
)
// Errors handles errors coming out of the call chain. It detects normal
// application errors which are used to respond to the client in a uniform way.
// Unexpected errors (status >= 500) are logged.
func Errors(log *logger.Logger) web.Middleware {
m := func(handler web.Handler) web.Handler {
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
if err := handler(ctx, w, r); err != nil {
log.Error(ctx, "message", "msg", err)
ctx, span := web.AddSpan(ctx, "business.web.request.mid.error")
span.RecordError(err)
span.End()
var er response.ErrorDocument
var status int
switch {
case response.IsError(err):
reqErr := response.GetError(err)
if validate.IsFieldErrors(reqErr.Err) {
fieldErrors := validate.GetFieldErrors(reqErr.Err)
er = response.ErrorDocument{
Error: "data validation error",
Fields: fieldErrors.Fields(),
}
status = reqErr.Status
break
}
er = response.ErrorDocument{
Error: reqErr.Error(),
}
status = reqErr.Status
case auth.IsAuthError(err):
er = response.ErrorDocument{
Error: http.StatusText(http.StatusUnauthorized),
}
status = http.StatusUnauthorized
default:
er = response.ErrorDocument{
Error: http.StatusText(http.StatusInternalServerError),
}
status = http.StatusInternalServerError
}
if err := web.Respond(ctx, w, er, status); err != nil {
return err
}
// If we receive the shutdown err we need to return it
// back to the base handler to shut down the service.
if web.IsShutdown(err) {
return err
}
}
return nil
}
return h
}
return m
}