-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
601 lines (487 loc) · 11.5 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
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
package flaw
import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"fmt"
"strings"
"github.com/phogolabs/flaw/format"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
const (
keyCode = "error_code"
keyMessage = "error_message"
keyDetails = "error_details"
keyCause = "error_cause"
keyStack = "error_stack"
)
var (
_ error = &Error{}
_ json.Marshaler = &Error{}
)
// Map is an alias to map[string]interface{}
type Map = map[string]interface{}
// ErrorConstant represents an error that can create a constant / sentinel
// error such as io.EOF
type ErrorConstant string
// Error returns the error message
func (x ErrorConstant) Error() string {
return fmt.Sprintf("%v", x)
}
// Format formats the error
func (x ErrorConstant) Format(state fmt.State, verb rune) {
fmt.Fprintf(state, "%v", string(x))
}
// Error represents a wrapped error
type Error struct {
code int
status int
msg string
details format.StringSlice
stack StackTrace
context map[string]interface{}
reason error
}
// Errorf creates a new error
func Errorf(msg string, data ...interface{}) *Error {
return &Error{
status: 500,
msg: fmt.Sprintf(msg, data...),
context: Map{},
stack: NewStackTrace(),
}
}
// Wrap wraps an error
func Wrap(err error, frames ...StackFrame) *Error {
var errx *Error
if !errors.As(err, &errx) {
stack := StackTrace(frames)
if len(stack) == 0 {
stack = NewStackTrace()
}
errx = &Error{
status: 500,
reason: err,
context: Map{},
stack: stack,
}
}
return errx
}
// WithError creates an error copy with given error wrapped
func (x Error) WithError(err error) *Error {
x.reason = err
x.stack = NewStackTrace()
return &x
}
// WithMessage creates an error copy with given message
func (x Error) WithMessage(text string) *Error {
x.msg = text
return &x
}
// WithDetails creates an error copy with given details
func (x Error) WithDetails(text string, details ...string) *Error {
x.details = append(x.details, text)
x.details = append(x.details, details...)
return &x
}
// WithCode creates an error copy with given status
func (x Error) WithCode(code int) *Error {
x.code = code
return &x
}
// WithStatus creates an error copy with given status
func (x Error) WithStatus(status int) *Error {
x.status = status
return &x
}
// WithContext creates an error copy with given map
func (x Error) WithContext(context Map) *Error {
if context == nil {
context = Map{}
}
x.context = context
return &x
}
// Code returns the error code
func (x *Error) Code() int {
return x.code
}
// Status returns the error status
func (x *Error) Status() int {
return x.status
}
// Message returns the error message
func (x *Error) Message() string {
return x.msg
}
// Details returns the error details
func (x *Error) Details() []string {
return x.details
}
// Cause returns the underlying error
func (x *Error) Cause() error {
return x.reason
}
// GRPCStatus returns the grpc status of this error
func (x *Error) GRPCStatus() *status.Status {
type Provider interface {
GRPCStatus() *status.Status
}
if provider, ok := x.reason.(Provider); ok {
return provider.GRPCStatus()
}
var (
code = codes.Internal
buffer = &bytes.Buffer{}
)
if x.code > 0 {
code = codes.Code(x.code)
}
if x.msg != "" {
fmt.Fprint(buffer, x.msg)
}
if x.reason != nil {
if buffer.Len() > 0 {
fmt.Fprint(buffer, ": ")
}
fmt.Fprintf(buffer, x.reason.Error())
}
payload := status.New(code, buffer.String())
// prepare the details
for _, item := range x.details {
// append the details
payload, _ = payload.WithDetails(&wrapperspb.StringValue{
Value: item,
})
}
if len(x.context) > 0 {
// prepare the context
if details, err := structpb.NewStruct(x.context); err == nil {
// add the error as details
payload, _ = payload.WithDetails(details)
}
}
return payload
}
// StackTrace returns the stack trace where the error occurred
func (x *Error) StackTrace() StackTrace {
return x.stack
}
// Context returns the error's context
func (x *Error) Context() Map {
return x.data()
}
// Wrap wraps the given error
func (x *Error) Wrap(err error) {
x.stack = NewStackTrace()
x.reason = err
}
// Unwrap unwraps the underlying error
func (x *Error) Unwrap() error {
return x.reason
}
// Error returns the error message
func (x *Error) Error() string {
return fmt.Sprintf("%v", x)
}
// Format formats the frame according to the fmt.Formatter interface.
//
// %m error message
// %d error details
// %c error code
// %r error reason
// %v code: %d message: %s details: %d reason: %w
//
// Format accepts flags that alter the printing of some verbs, as follows:
//
// %+s stack trace
// %+v equivalent
func (x *Error) Format(state fmt.State, verb rune) {
switch verb {
case 'c':
fmt.Fprintf(state, "%d", x.code)
case 'm':
fmt.Fprintf(state, "%s", x.msg)
case 'r':
fmt.Fprintf(state, "%v", x.reason)
case 'd':
x.details.Format(state, 'v')
case 's':
x.stack.Format(state, 'v')
case 'v':
formatter := format.NewState(state)
defer formatter.Flush()
if x.code != 0 {
x.title(formatter, "code:")
x.Format(formatter, 'c')
}
if x.msg != "" {
x.title(formatter, "message:")
x.Format(formatter, 'm')
}
if x.details != nil {
x.title(formatter, "details:")
x.newline(formatter)
x.Format(formatter, 'd')
}
if x.reason != nil {
x.title(formatter, "cause:")
x.Format(formatter, 'r')
}
if x.stack != nil && state.Flag('+') {
x.title(formatter, "stack:")
x.newline(formatter)
x.Format(formatter, 's')
}
}
}
// MarshalJSON marshals the error as json
func (x *Error) MarshalJSON() ([]byte, error) {
data := x.data(keyStack)
if x.reason != nil {
if _, ok := x.reason.(json.Marshaler); ok {
data[keyCause] = x.reason
}
}
return json.Marshal(data)
}
// MarshalXML marshals the error as xml
func (x *Error) MarshalXML(encoder *xml.Encoder, start xml.StartElement) error {
data := x.data(keyStack)
if x.reason != nil {
if _, ok := x.reason.(xml.Marshaler); ok {
data[keyCause] = x.reason
}
}
return data.MarshalXML(encoder, start)
}
func (x *Error) data(keys ...string) dictionary {
m := dictionary{}
set := func(field string, value interface{}) {
for _, key := range keys {
if strings.EqualFold(key, field) {
return
}
}
m[field] = value
}
if x.code > 0 {
set(keyCode, x.code)
}
if x.msg != "" {
set(keyMessage, x.msg)
}
if len(x.details) > 0 {
set(keyDetails, x.details)
}
if x.reason != nil {
set(keyCause, x.reason.Error())
}
if x.stack != nil {
set(keyStack, x.stack)
}
for k, v := range x.context {
set(k, v)
}
return m
}
func (x *Error) title(formatter *format.State, text string) {
if formatter.Size() > 0 {
if formatter.Flag('+') {
fmt.Fprint(formatter, "\n")
} else {
fmt.Fprint(formatter, " ")
}
}
fmt.Fprint(formatter, text)
if formatter.Flag('+') {
fmt.Fprint(formatter, "\t")
}
fmt.Fprint(formatter, " ")
}
func (x *Error) newline(formatter *format.State) {
if formatter.Flag('+') {
fmt.Fprint(formatter, "\n")
}
}
var _ error = ErrorCollector{}
// ErrorCollector is a slice of errors
type ErrorCollector []error
// Error returns the error message
func (errs ErrorCollector) Error() string {
return fmt.Sprintf("%v", errs)
}
// MarshalJSON marshals the error as json
func (errs ErrorCollector) MarshalJSON() ([]byte, error) {
input := make([]interface{}, len(errs))
for index, err := range errs {
if _, ok := err.(json.Marshaler); ok {
input[index] = err
} else {
input[index] = err.Error()
}
}
return json.Marshal(input)
}
// Format the error as string
func (errs ErrorCollector) Format(state fmt.State, verb rune) {
switch verb {
case 's':
fallthrough
case 'v':
switch {
case state.Flag('+'):
errs.formatBullet(state, verb)
case state.Flag('#'):
items := make([]error, len(errs))
for index, err := range errs {
items[index] = ErrorConstant(err.Error())
}
fmt.Fprintf(state, "%#v", []error(items))
default:
errs.formatSlice(state, verb)
}
}
}
func (errs ErrorCollector) formatBullet(state fmt.State, verb rune) {
count := len(errs)
for index, err := range errs {
fmt.Fprint(state, " --- ")
fmt.Fprintf(state, "%v", err)
if index < count-1 {
fmt.Fprint(state, "\n")
}
}
}
func (errs ErrorCollector) formatSlice(state fmt.State, verb rune) {
fmt.Fprint(state, "[")
for index, err := range errs {
if index > 0 {
fmt.Fprint(state, ", ")
}
fmt.Fprintf(state, "%v", err)
}
fmt.Fprint(state, "]")
}
// Is reports whether any error in err's chain matches target.
//
// The chain consists of err itself followed by the sequence of errors obtained by
// repeatedly calling Unwrap.
//
// An error is considered to match a target if it is equal to that target or if
// it implements a method Is(error) bool such that Is(target) returns true.
func (errs ErrorCollector) Is(target error) bool {
items, ok := target.(ErrorCollector)
if !ok {
items = ErrorCollector{target}
}
if len(errs) != len(items) {
return false
}
for index, child := range errs {
if !errors.Is(child, items[index]) {
return false
}
}
return true
}
// As finds the first error in err's chain that matches target, and if so, sets
// target to that error value and returns true.
//
// The chain consists of err itself followed by the sequence of errors obtained by
// repeatedly calling Unwrap.
//
// An error matches target if the error's concrete value is assignable to the value
// pointed to by target, or if the error has a method As(interface{}) bool such that
// As(target) returns true. In the latter case, the As method is responsible for
// setting target.
//
// As will panic if target is not a non-nil pointer to either a type that implements
// error, or to any interface type. As returns false if err is nil.
func (errs ErrorCollector) As(err interface{}) bool {
for _, child := range errs {
if errors.As(child, err) {
return true
}
}
return false
}
// Wrap appends an error to the slice
func (errs *ErrorCollector) Wrap(err error) {
*errs = append(*errs, err)
}
// Unwrap unwraps the underlying error it's only one
func (errs ErrorCollector) Unwrap() error {
count := len(errs)
switch {
case count == 1:
return errs[0]
default:
return nil
}
}
// Code returns the code from an error
func Code(err error) int {
type Coder interface {
Code() int
}
if coder, ok := err.(Coder); ok {
return coder.Code()
}
return 0
}
// Status returns the status from an error
func Status(err error) int {
type Statuser interface {
Status() int
}
if status, ok := err.(Statuser); ok {
return status.Status()
}
return 0
}
// Cause returns the error's cause
func Cause(err error) error {
type Causer interface {
Cause() error
}
if causer, ok := err.(Causer); ok {
return causer.Cause()
}
return err
}
// Message returns the error's message
func Message(err error) string {
type Messanger interface {
Message() string
}
if messanger, ok := err.(Messanger); ok {
return messanger.Message()
}
return ""
}
// Details returns the error's details
func Details(err error) []string {
type Detailer interface {
Details() []string
}
if detailer, ok := err.(Detailer); ok {
return detailer.Details()
}
return []string{}
}
// Context returns the error's context
func Context(err error) Map {
type Contexter interface {
Context() Map
}
if contexter, ok := err.(Contexter); ok {
return contexter.Context()
}
return Map{}
}