-
Notifications
You must be signed in to change notification settings - Fork 17
/
error.go
161 lines (135 loc) · 3.94 KB
/
error.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
package rest
import (
"errors"
"net/http"
"github.com/swaggest/usecase/status"
)
// HTTPCodeAsError exposes HTTP status code as use case error that can be translated to response status.
type HTTPCodeAsError int
// Error return HTTP status text.
func (c HTTPCodeAsError) Error() string {
return http.StatusText(int(c))
}
// HTTPStatus returns HTTP status code.
func (c HTTPCodeAsError) HTTPStatus() int {
return int(c)
}
// ErrWithHTTPStatus exposes HTTP status code.
type ErrWithHTTPStatus interface {
error
HTTPStatus() int
}
// ErrWithFields exposes structured context of error.
type ErrWithFields interface {
error
Fields() map[string]interface{}
}
// ErrWithAppCode exposes application error code.
type ErrWithAppCode interface {
error
AppErrCode() int
}
// ErrWithCanonicalStatus exposes canonical status code.
type ErrWithCanonicalStatus interface {
error
Status() status.Code
}
// Err creates HTTP status code and ErrResponse for error.
//
// You can use it with use case status code:
//
// rest.Err(status.NotFound)
func Err(err error) (int, ErrResponse) {
if err == nil {
panic("nil error received")
}
er := ErrResponse{}
var (
withHTTPStatus ErrWithHTTPStatus
withCanonicalStatus ErrWithCanonicalStatus
withAppCode ErrWithAppCode
withFields ErrWithFields
)
er.err = err
er.ErrorText = err.Error()
er.httpStatusCode = http.StatusInternalServerError
if errors.As(err, &withCanonicalStatus) {
us := withCanonicalStatus.Status()
er.httpStatusCode = HTTPStatusFromCanonicalCode(us)
er.StatusText = us.String()
}
if errors.As(err, &withHTTPStatus) {
er.httpStatusCode = withHTTPStatus.HTTPStatus()
}
if errors.As(err, &withAppCode) {
er.AppCode = withAppCode.AppErrCode()
}
if errors.As(err, &withFields) {
er.Context = withFields.Fields()
}
if er.ErrorText == er.StatusText {
er.ErrorText = ""
}
return er.httpStatusCode, er
}
// ErrResponse is HTTP error response body.
type ErrResponse struct {
StatusText string `json:"status,omitempty" description:"Status text."`
AppCode int `json:"code,omitempty" description:"Application-specific error code."`
ErrorText string `json:"error,omitempty" description:"Error message."`
Context map[string]interface{} `json:"context,omitempty" description:"Application context."`
err error // Original error.
httpStatusCode int // HTTP response status code.
}
// Error implements error.
func (e ErrResponse) Error() string {
if e.ErrorText != "" {
return e.ErrorText
}
return e.StatusText
}
// Unwrap returns parent error.
func (e ErrResponse) Unwrap() error {
return e.err
}
// HTTPStatusFromCanonicalCode returns http status accordingly to use case status code.
func HTTPStatusFromCanonicalCode(c status.Code) int {
switch c {
case status.OK:
return http.StatusOK
case status.Canceled:
// Custom nginx status "499 Client Closed Request" is a recommended mapping, but 500 is more compatible.
return http.StatusInternalServerError
case status.Unknown:
return http.StatusInternalServerError
case status.InvalidArgument:
return http.StatusBadRequest
case status.DeadlineExceeded:
return http.StatusGatewayTimeout
case status.NotFound:
return http.StatusNotFound
case status.AlreadyExists:
return http.StatusConflict
case status.PermissionDenied:
return http.StatusForbidden
case status.ResourceExhausted:
return http.StatusTooManyRequests
case status.FailedPrecondition:
return http.StatusPreconditionFailed
case status.Aborted:
return http.StatusConflict
case status.OutOfRange:
return http.StatusBadRequest
case status.Unimplemented:
return http.StatusNotImplemented
case status.Internal:
return http.StatusInternalServerError
case status.Unavailable:
return http.StatusServiceUnavailable
case status.DataLoss:
return http.StatusInternalServerError
case status.Unauthenticated:
return http.StatusUnauthorized
}
return http.StatusInternalServerError
}