-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.go
374 lines (323 loc) · 9 KB
/
types.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
package herr
import (
"errors"
"fmt"
"io"
"net/http"
)
type Item struct {
Name string `json:"name,omitempty"`
Code string `json:"code,omitempty"`
Description string `json:"description,omitempty"`
}
// Error.
type Error struct {
CodeField int `json:"code,omitempty"` // The Code of staus.
StatusField string `json:"status,omitempty"` // Status is the status text
ReasonField string `json:"reason,omitempty"` // The reason of the error.
RequestIDField string `json:"request_id,omitempty"` // RequestIDField is request ID.
MessageField string `json:"message,omitempty"` // Message of error.
DescriptionField string `json:"description,omitempty"` // Description of error.
DebugField string `json:"debug,omitempty"` // Debug info
DetailsField map[string]interface{} `json:"details,omitempty"` // Details
ItemsField []Item `json:"items,omitempty"` // Items
err error
errWrapped bool
}
func (e *Error) Unwrap() error {
return e.err
}
func (e *Error) Wrap(err error) {
e.errWrapped = true
e.err = err
}
func (e Error) WithWrap(err error) *Error {
e.errWrapped = true
e.err = err
return &e
}
func (e Error) Is(err error) bool {
switch te := err.(type) {
case Error:
return e.MessageField == te.MessageField &&
e.StatusField == te.StatusField &&
e.CodeField == te.CodeField &&
e.ReasonField == te.ReasonField
case *Error:
return e.MessageField == te.MessageField &&
e.StatusField == te.StatusField &&
e.CodeField == te.CodeField &&
e.ReasonField == te.ReasonField
default:
return false
}
}
func (e Error) As(target interface{}) bool {
switch te := target.(type) {
case *Error:
*te = e
return true
default:
return false
}
}
func (e Error) Status() string {
return e.StatusField
}
func (e Error) Error() string {
switch {
case e.err != nil && e.MessageField == "":
return e.err.Error()
case e.err != nil && e.errWrapped:
return e.MessageField + ": " + e.err.Error()
}
return e.MessageField
}
func (e Error) Message() string {
return e.MessageField
}
func (e Error) RequestID() string {
return e.RequestIDField
}
func (e Error) Reason() string {
return e.ReasonField
}
func (e Error) Debug() string {
return e.DebugField
}
func (e Error) Details() map[string]interface{} {
return e.DetailsField
}
func (e Error) Detail(key string) (interface{}, bool) {
if e.DetailsField == nil {
return nil, false
}
value, ok := e.DetailsField[key]
return value, ok
}
func (e Error) Items() []Item {
return e.ItemsField
}
func (e Error) StatusCode() int {
return e.CodeField
}
// With clones the error.
func (e Error) With() *Error {
return &e
}
// WithStatusCode clones the error with status and status code.
func (e Error) WithStatusCode(code int) *Error {
e.CodeField = code
e.StatusField = http.StatusText(code)
return &e
}
// WithRequestID clones the error with request ID.
func (e Error) WithRequestID(requestID string) *Error {
e.RequestIDField = requestID
return &e
}
// WithRequestID clones the error with request ID.
func (e Error) WithRequest(r *http.Request) *Error {
id := r.Header.Get("x-request-id")
if id == "" {
id = r.Header.Get("request-Id")
}
if id == "" {
id = r.Header.Get("req_id")
}
e.RequestIDField = id
return &e
}
// WithReason clones the error with reason.
func (e Error) WithReason(reason string) *Error {
e.ReasonField = reason
return &e
}
// WithError clones the error with.
func (e Error) WithError(message string) *Error {
e.MessageField = message
return &e
}
// WithErrorf clones the error with formated error message.
func (e Error) WithErrorf(message string, args ...interface{}) *Error {
err := fmt.Errorf(message, args...)
if err := errors.Unwrap(err); err != nil {
e.errWrapped = false
e.err = err
}
return e.WithError(e.Error())
}
// WithDescription clones the error with.
func (e Error) WithDescription(description string) *Error {
e.DescriptionField = description
return &e
}
// WithErrorf clones the error with formated error message.
func (e Error) WithDescriptionf(description string, args ...interface{}) *Error {
return e.WithDescription(fmt.Sprintf(description, args...))
}
// WithDebugf clones the error with.
func (e Error) WithDebugf(debug string, args ...interface{}) *Error {
return e.WithDebug(fmt.Sprintf(debug, args...))
}
// WithDebug clones the error with formated debug info.
func (e Error) WithDebug(debug string) *Error {
e.DebugField = debug
return &e
}
// WithItems clones the error with items.
func (e Error) WithItems(items ...Item) *Error {
e.ItemsField = append(e.ItemsField, items...)
return &e
}
// WithDetail clones the error with detail.
func (e Error) WithDetail(key string, detail interface{}) *Error {
if e.DetailsField == nil {
e.DetailsField = map[string]interface{}{}
}
e.DetailsField[key] = detail
return &e
}
// WithDetailf clones the error with formated additional detail.
func (e Error) WithDetailf(key string, message string, args ...interface{}) *Error {
if e.DetailsField == nil {
e.DetailsField = map[string]interface{}{}
}
e.DetailsField[key] = fmt.Sprintf(message, args...)
return &e
}
// SetStatusCode sets the status and status code.
func (e *Error) SetStatusCode(code int) *Error {
e.CodeField = code
e.StatusField = http.StatusText(code)
return e
}
// SetRequestID sets the error request ID.
func (e *Error) SetRequestID(requestID string) *Error {
e.RequestIDField = requestID
return e
}
// SetRequestID sets the error request ID.
func (e *Error) SetRequest(r *http.Request) *Error {
id := r.Header.Get("x-request-id")
if id == "" {
id = r.Header.Get("request-Id")
}
if id == "" {
id = r.Header.Get("req_id")
}
e.RequestIDField = id
return e
}
// SetReason sets the error reason.
func (e *Error) SetReason(reason string) *Error {
e.ReasonField = reason
return e
}
// SetError sets the error.
func (e *Error) SetError(message string) *Error {
e.MessageField = message
return e
}
// SetErrorf sets the error formated error message.
func (e *Error) SetErrorf(message string, args ...interface{}) *Error {
err := fmt.Errorf(message, args...)
if err := errors.Unwrap(err); err != nil {
e.errWrapped = false
e.err = err
}
return e.SetError(err.Error())
}
// SetDescription sets the description.
func (e Error) SetDescription(description string) *Error {
e.DescriptionField = description
return &e
}
// Setdescription sets the error formated error message.
func (e *Error) SetDescriptionf(description string, args ...interface{}) *Error {
return e.SetDescription(fmt.Sprintf(description, args...))
}
// SetDebug sets the error formated debug info.
func (e *Error) SetDebug(debug string) *Error {
e.DebugField = debug
return e
}
// SetDebugf sets the debug info.
func (e *Error) SetDebugf(debug string, args ...interface{}) *Error {
return e.SetDebug(fmt.Sprintf(debug, args...))
}
// SetItems sets the items.
func (e *Error) SetItems(items ...Item) *Error {
e.ItemsField = append(e.ItemsField, items...)
return e
}
// SetDetail sets details.
func (e *Error) SetDetail(key string, detail interface{}) *Error {
if e.DetailsField == nil {
e.DetailsField = map[string]interface{}{}
}
e.DetailsField[key] = detail
return e
}
// SetDetailf sets the formated additional detail.
func (e *Error) SetDetailf(key string, message string, args ...interface{}) *Error {
if e.DetailsField == nil {
e.DetailsField = map[string]interface{}{}
}
e.DetailsField[key] = fmt.Sprintf(message, args...)
return e
}
func (e Error) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
fmt.Fprintf(s, "rid=%s\n", e.RequestIDField)
fmt.Fprintf(s, "msg=%s\n", e.MessageField)
fmt.Fprintf(s, "reason=%s\n", e.ReasonField)
fmt.Fprintf(s, "details=%+v\n", e.DetailsField)
fmt.Fprintf(s, "debug=%s\n", e.DebugField)
fmt.Fprintf(s, "err=%s\n", e.err)
return
}
fallthrough
case 's':
_, _ = io.WriteString(s, e.Error())
case 'q':
fmt.Fprintf(s, "%q", e.MessageField)
}
}
func ToError(err error) *Error {
e := &Error{
CodeField: http.StatusInternalServerError,
DetailsField: map[string]interface{}{},
}
e.Wrap(err)
if c := MessageCarrier(nil); errors.As(err, &c) {
e.MessageField = c.Message()
}
if c := ReasonCarrier(nil); errors.As(err, &c) {
e.ReasonField = c.Reason()
}
if c := RequestIDCarrier(nil); errors.As(err, &c) && c.RequestID() != "" {
e.RequestIDField = c.RequestID()
}
if c := DetailsCarrier(nil); errors.As(err, &c) && c.Details() != nil {
e.DetailsField = c.Details()
}
if c := StatusCarrier(nil); errors.As(err, &c) && c.Status() != "" {
e.StatusField = c.Status()
}
if c := StatusCodeCarrier(nil); errors.As(err, &c) && c.StatusCode() != 0 {
e.CodeField = c.StatusCode()
}
if c := DebugCarrier(nil); errors.As(err, &c) {
e.DebugField = c.Debug()
}
if c := ItemsCarrier(nil); errors.As(err, &c) {
e.ItemsField = c.Items()
}
if e.StatusField == "" {
e.StatusField = http.StatusText(e.StatusCode())
}
return e
}