-
Notifications
You must be signed in to change notification settings - Fork 5
/
errors.go
604 lines (513 loc) · 12.2 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
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
602
603
604
package errors
import (
"fmt"
"reflect"
"strings"
)
// TODO is a non-nil error intended to act as a placeholder during development
// when writing the structure of the code but the implementation is still left
// to be written.
var TODO error
// New returns an error that formats as the given message. The returned error
// carries a capture of the stack trace.
//
// err = errors.New("something went wrong")
//
func New(msg string) error {
return &baseError{
msg: msg,
stack: CaptureStackTrace(1),
}
}
// Errorf returns an error that formats as fmt.Sprintf(msg, args...).
// The returned error carries a capture of the stack trace.
//
// err = errors.Errorf("unexpected answer: %d", 42)
//
func Errorf(msg string, args ...interface{}) error {
return &baseError{
msg: fmt.Sprintf(msg, args...),
stack: CaptureStackTrace(1),
}
}
// WithMessage returns an error that wraps err and prefix its original error
// error message with msg. If err is nil, WithMessage returns nil.
//
// err = errors.WithMessage(err, "something went wrong")
//
func WithMessage(err error, msg string) error {
if err == nil {
return nil
}
return &errorWithMessage{
cause: Adapt(err),
msg: msg,
}
}
// WithStack returns an error that wraps err with a capture of the stack trace
// at the time the function is called. If err is nil, WithStack returns nil.
//
// err = errors.WithStack(err)
//
// The error is adapted before the stack trace is added.
func WithStack(err error) error {
return WithStackTrace(err, CaptureStackTrace(1))
}
// WithStackTrace returns an error that wraps err with the given stack trace.
// If err is nil, WithStackTrace returns nil.
//
// err = errors.WithStackTrace(err, errors.CaptureStackTrace(1))
//
// The error is adapted before the stack trace is added.
func WithStackTrace(err error, stack StackTrace) error {
if err == nil {
return nil
}
return &errorWithStack{
cause: Adapt(err),
stack: stack,
}
}
// WithTypes returns an error that wraps err and implements the given types so
// that calling errors.Is on the returned error with one of the given types will
// return true.
//
// The error is adapted before types are added.
func WithTypes(err error, types ...string) error {
if err == nil {
return nil
}
return &errorWithTypes{
cause: Adapt(err),
types: copyTypes(types),
}
}
// WithTags returns an error that wraps err and tags it with the given key/value
// pairs. If err is nil the function returns nil.
//
// The error is adapted before tags are added.
func WithTags(err error, tags ...Tag) error {
if err == nil {
return nil
}
return &errorWithTags{
cause: Adapt(err),
tags: makeTags(tags...),
}
}
// Wrap returns an error that wraps err with msg as prefix to its original
// message and a capture of the stack trace at the time the function is called.
// If err is nil, Wrap returns nil.
//
// err = errors.Wrap(err, "something went wrong")
//
// The error is adapted before being wrapped with a message and stack trace.
func Wrap(err error, msg string) error {
return wrap(err, 1, msg)
}
// Wrapf returns an error that wraps err with fmt.Sprintf(msg, args...) as
// prefix to its original message and a capture of the stack trace at the time
// the function is called. If err is nil, Wrap returns nil.
//
// err = errors.Wrapf(err, "unexpected answer: %d", 42)
//
// The error is adapted before being wrapped with a message and stack trace.
func Wrapf(err error, msg string, args ...interface{}) error {
return wrap(err, 1, fmt.Sprintf(msg, args...))
}
func wrap(err error, depth int, msg string) error {
if err == nil {
return nil
}
return &errorWithMessage{
cause: &errorWithStack{
cause: Adapt(err),
stack: CaptureStackTrace(depth + 1),
},
msg: msg,
}
}
// Join composes an error from the list of errors passed as argument.
//
// The function strips all nil errors from the input argument list. The returned
// error has a Causes method which returns the list of non-nil errors that were
// given to the function.
//
// err = errors.Join(err1, err2, err3)
//
// All errors passed to the function are adapted.
func Join(errs ...error) error {
n := 0
for _, e := range errs {
if e != nil {
n++
}
}
if n == 0 {
return nil
}
e := &multiError{
errors: make([]error, 0, n),
}
for _, err := range errs {
if err != nil {
e.errors = append(e.errors, Adapt(err))
}
}
return e
}
// Recv reads all errors from the given channel and returns one that combines
// them. All nil error are ignored.
//
// ch := make(chan error)
// wg := sync.WaitGroup{}
//
// for _, t := range tasks {
// wg.Add(1)
// go func(t task) { ch <- t(); wg.Done() }
// }
//
// go func() { wg.Wait(); close(ch) }
//
// err := errors.Recv(errch)
//
// All errors received on the channel are adapted.
func Recv(ch <-chan error) error {
var errs []error
for err := range ch {
if err != nil {
errs = append(errs, Adapt(err))
}
}
if len(errs) == 0 {
return nil
}
return &multiError{
errors: errs,
}
}
// Err constructs an error from a value of arbitrary type, using the following
// rules:
//
// - if v is nil, the function simply returns nil
//
// - if v is a string type, the function behaves like calling New
//
// - if v is already an error it is returned unchanged
//
// - if v has an Err() method returning an error, the function returns the
// result of calling it.
//
// - if v doesn't fall into any of those categories, the function behaves like
// calling Errorf("%+v", v).
//
// A common use case for this function is to implement internal error reporting
// based on raising panics (within a package), here is an example:
//
// func F() (err error) {
// defer func() { err = errors.Err(recover()) }()
// // ...
// }
//
func Err(v interface{}) error {
switch value := v.(type) {
case nil:
return nil
case string:
return &baseError{
msg: value,
stack: CaptureStackTrace(1),
}
case error:
return value
case interface {
Err() error
}:
return value.Err()
default:
return &baseError{
msg: fmt.Sprintf("%+v", value),
stack: CaptureStackTrace(1),
}
}
}
// Cause returns the cause of err, which may be err if it had no cause.
func Cause(err error) error {
for {
if e, ok := err.(errorCause); ok {
if cause := e.Cause(); cause != nil {
err = cause
continue
}
}
return err
}
}
// Causes returns the list of causes of err, which may be an empty slice if err
// is nil or had no causes.
func Causes(err error) []error {
original := err
for {
if e, ok := err.(errorCauses); ok {
return e.Causes()
}
if e, ok := err.(errorCause); ok {
if cause := e.Cause(); cause != nil {
err = cause
continue
}
}
if err != original {
return []error{err}
}
return nil
}
}
// Is tests whether err is of type typ. Errors may implement types by defining
// methods that take no arguments and return a boolean value. Passing the name
// of those methods to Is tests for their existence and calls them to validate
// the type of the error.
//
// This model has been used in the standard library where some errors implement
// the Temporary and Timeout methods to give the program more details about the
// reason the error occurred and the way it should be handled.
//
// Here is an example of using the Is function:
//
// if errors.Is("Timeout", err) {
// // ...
// }
// if errors.Is("Temporary", err) {
// // ...
// }
//
// The function walks through the graph of causes looking for an error which may
// implement the given type.
func Is(typ string, err error) bool {
if err == nil {
return false
}
if e, ok := err.(errorTypes); ok {
for _, t := range e.Types() {
if t == typ {
return true
}
}
}
v := reflect.ValueOf(err)
m := v.MethodByName(typ)
if m.IsValid() {
if f, ok := m.Interface().(func() bool); ok {
return f()
}
}
switch e := err.(type) {
case errorCause:
return Is(typ, e.Cause())
case errorCauses:
for _, cause := range e.Causes() {
if ok := Is(typ, cause); ok {
return true
}
}
}
return false
}
// Types returns a slice containing all the types implemented by err and its
// causes (if it had any).
func Types(err error) []string {
return deepAppendTypes(nil, err)
}
// Tags returns a slice containing all the tags set on err and its causes
// (it if had any).
func Tags(err error) []Tag {
return deepAppendTags(nil, err)
}
// LookupTag returns value for a given tag name. Returns empty string if tag wasn't found.
// If multiple tags found by that name, the most recent value is used.
func LookupTag(err error, name string) string {
var result string
for _, tag := range deepAppendTags(nil, err) {
if tag.Name == name {
result = tag.Value
}
}
return result
}
// Inspect extract and returns properties of err.
//
// The function follows a straight path on the error graph, stopping when it
// finds an error that doesn't have a single cause (either zero or many).
func Inspect(err error) (msgs []string, types []string, tags []Tag, stacks []StackTrace, causes []error) {
for err != nil {
types = appendTypes(types, err)
tags = appendTags(tags, err)
if msg := message(err); len(msg) != 0 {
msgs = append(msgs, msg)
}
if stack := stackTrace(err); len(stack) != 0 {
stacks = append(stacks, stack)
}
switch e := err.(type) {
case errorCauses:
causes = e.Causes()
err = nil
case errorCause:
err = e.Cause()
case errorMessage:
err = nil // prevent duplicating the message with the Error call
default:
msgs = append(msgs, e.Error())
err = nil
}
}
types = dedupeTypes(types)
sortTags(tags)
return
}
func walk(err error, do func(error)) {
if err != nil {
do(err)
switch e := err.(type) {
case errorCause:
walk(e.Cause(), do)
case errorCauses:
for _, cause := range e.Causes() {
walk(cause, do)
}
}
}
}
type errorCause interface {
Cause() error
}
type errorCauses interface {
Causes() []error
}
type errorMessage interface {
Message() string
}
type errorTypes interface {
Types() []string
}
type errorTags interface {
Tags() []Tag
}
type errorStackTrace interface {
StackTrace() StackTrace
}
type baseError struct {
msg string
stack StackTrace
}
func (e *baseError) Error() string {
return e.msg
}
func (e *baseError) Message() string {
return e.msg
}
func (e *baseError) StackTrace() StackTrace {
return e.stack
}
func (e *baseError) Format(s fmt.State, v rune) {
format(s, v, e)
}
type multiError struct {
errors []error
}
func (e *multiError) Causes() []error {
return e.errors
}
func (e *multiError) Error() string {
s := make([]string, len(e.errors))
for i, e := range e.errors {
s[i] = e.Error()
}
return strings.Join(s, "; ")
}
func (e *multiError) Format(s fmt.State, v rune) {
format(s, v, e)
}
type errorWithMessage struct {
cause error
msg string
}
func (e *errorWithMessage) Cause() error {
return e.cause
}
func (e *errorWithMessage) Error() string {
return e.msg + ": " + e.cause.Error()
}
func (e *errorWithMessage) Message() string {
return e.msg
}
func (e *errorWithMessage) Format(s fmt.State, v rune) {
format(s, v, e)
}
type errorWithStack struct {
cause error
stack StackTrace
}
func (e *errorWithStack) Cause() error {
return e.cause
}
func (e *errorWithStack) Error() string {
return e.cause.Error()
}
func (e *errorWithStack) Format(s fmt.State, v rune) {
format(s, v, e)
}
func (e *errorWithStack) StackTrace() StackTrace {
return e.stack
}
type errorWithTypes struct {
cause error
types []string
}
func (e *errorWithTypes) Cause() error {
return e.cause
}
func (e *errorWithTypes) Error() string {
return e.cause.Error()
}
func (e *errorWithTypes) Format(s fmt.State, v rune) {
format(s, v, e)
}
func (e *errorWithTypes) Types() []string {
return e.types
}
type errorWithTags struct {
cause error
tags []Tag
}
func (e *errorWithTags) Cause() error {
return e.cause
}
func (e *errorWithTags) Error() string {
return e.cause.Error()
}
func (e *errorWithTags) Format(s fmt.State, v rune) {
format(s, v, e)
}
func (e *errorWithTags) Tags() []Tag {
return e.tags
}
type errorTODO struct{}
func (*errorTODO) Error() string {
return "TODO"
}
func init() {
TODO = &errorTODO{}
}
func message(err error) string {
if e, ok := err.(errorMessage); ok {
return e.Message()
}
return ""
}
func stackTrace(err error) StackTrace {
if e, ok := err.(errorStackTrace); ok {
return e.StackTrace()
}
return nil
}