-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors.go
1138 lines (1027 loc) · 30.5 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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Package errors provides errors with a recorded stack trace and optional
// structured details.
//
// The traditional error handling idiom in Go is roughly akin to
//
// if err != nil {
// return err
// }
//
// which when applied recursively up the call stack results in error reports
// without a stack trace or context.
// The errors package provides error handling primitives to annotate
// errors along the failure path in a way that does not destroy the
// original error.
//
// # Adding a stack trace to an error
//
// When interacting with code which returns errors without a stack trace,
// you can upgrade that error to one with a stack trace using errors.WithStack.
// For example:
//
// func readAll(r io.Reader) ([]byte, errors.E) {
// data, err := ioutil.ReadAll(r)
// if err != nil {
// return nil, errors.WithStack(err)
// }
// return data, nil
// }
//
// errors.WithStack records the stack trace at the point where it was called, so
// use it as close to where the error originated as you can get so that the
// recorded stack trace is more precise.
//
// The example above uses errors.E for the returned error type instead of the
// standard error type. This is not required, but it tells Go that you expect
// that the function returns only errors with a stack trace and Go type system
// then helps you find any cases where this is not so.
//
// errors.WithStack does not record the stack trace if it is already present in
// the error so it is safe to call it if you are unsure if the error contains
// a stack trace.
//
// Errors with a stack trace implement the following interface, returning program
// counters of function invocations:
//
// type stackTracer interface {
// StackTrace() []uintptr
// }
//
// You can use standard runtime.CallersFrames to obtain stack trace frame
// information (e.g., function name, source code file and line).
// You can also use errors.StackFormatter to format the stack trace.
//
// Although the stackTracer interface is not exported by this package, it is
// considered a part of its stable public interface.
//
// # Adding context to an error
//
// Sometimes an error occurs in a low-level function and the error messages
// returned from it are too low-level, too.
// You can use errors.Wrap to construct a new higher-level error while
// recording the original error as a cause.
//
// image, err := readAll(imageFile)
// if err != nil {
// return nil, errors.Wrap(err, "reading image failed")
// }
//
// In the example above we returned a new error with a new message,
// hiding the low-level details. The returned error implements the
// following interface:
//
// type causer interface {
// Cause() error
// }
//
// which enables access to the underlying low-level error. You can also
// use errors.Cause to obtain the cause.
//
// Although the causer interface is not exported by this package, it is
// considered a part of its stable public interface.
//
// Sometimes you do not want to hide the error message but just add to it.
// You can use errors.WithMessage, which adds a prefix to the existing message,
// or errors.Errorf, which gives you more control over the new message.
//
// errors.WithMessage(err, "reading image failed")
// errors.Errorf("reading image failed (%w)", err)
//
// Example new messages could then be, respectively:
//
// "reading image failed: connection error"
// "reading image failed (connection error)"
//
// # Adding details to an error
//
// Errors returned by this package implement the detailer interface:
//
// type detailer interface {
// Details() map[string]interface{}
// }
//
// which enables access to a map with optional additional details about
// the error. Returned map can be modified in-place. You can also use
// errors.Details and errors.AllDetails to access details:
//
// errors.Details(err)["url"] = "http://example.com"
//
// You can also use errors.WithDetails as an alternative to errors.WithStack
// if you also want to add details while recording the stack trace:
//
// func readAll(r io.Reader, filename string) ([]byte, errors.E) {
// data, err := ioutil.ReadAll(r)
// if err != nil {
// return nil, errors.WithDetails(err, "filename", filename)
// }
// return data, nil
// }
//
// # Working with the tree of errors
//
// Errors which implement the following standard unwrapper interfaces:
//
// type unwrapper interface {
// Unwrap() error
// }
//
// type unwrapper interface {
// Unwrap() error[]
// }
//
// form a tree of errors where a wrapping error points its parent,
// wrapped, error(s). Errors returned from this package implement this
// interface to return the original error or errors, when they exist.
// This enables us to have constant base errors which we annotate
// with a stack trace before we return them:
//
// var ErrAuthentication = errors.Base("authentication error")
// var ErrMissingPassphrase = errors.BaseWrap(ErrAuthentication, "missing passphrase")
// var ErrInvalidPassphrase = errors.BaseWrap(ErrAuthentication, "invalid passphrase")
//
// func authenticate(passphrase string) errors.E {
// if passphrase == "" {
// return errors.WithStack(ErrMissingPassphrase)
// } else if passphrase != "open sesame" {
// return errors.WithStack(ErrInvalidPassphrase)
// }
// return nil
// }
//
// Or with details:
//
// func authenticate(username, passphrase string) errors.E {
// if passphrase == "" {
// return errors.WithDetails(ErrMissingPassphrase, "username", username)
// } else if passphrase != "open sesame" {
// return errors.WithDetails(ErrInvalidPassphrase, "username", username)
// }
// return nil
// }
//
// We can use errors.Is to determine which error has been returned:
//
// if errors.Is(err, ErrMissingPassphrase) {
// fmt.Println("Please provide a passphrase to unlock the doors.")
// }
//
// Works across the tree, too:
//
// if errors.Is(err, ErrAuthentication) {
// fmt.Println("Failed to unlock the doors.")
// }
//
// To access details, use:
//
// errors.AllDetails(err)["username"]
//
// You can join multiple errors into one error by calling errors.Join.
// Join also records the stack trace at the point it was called.
//
// # Formatting and JSON marshaling errors
//
// All errors with a stack trace returned from this package implement fmt.Formatter
// interface and can be formatted by the fmt package. They also support marshaling
// to JSON. Same formatting and JSON marshaling for errors coming outside of
// this package can be done by wrapping them into errors.Formatter.
package errors
import (
"fmt"
"strings"
"sync"
"unsafe"
pkgerrors "github.com/pkg/errors"
)
type stackTracer interface {
StackTrace() []uintptr
}
type pkgStackTracer interface {
StackTrace() pkgerrors.StackTrace
}
type goErrorsStackTracer interface {
Callers() []uintptr
}
type erisStackTracer interface {
StackFrames() []uintptr
}
type causer interface {
Cause() error
}
type unwrapper interface {
Unwrap() error
}
type unwrapperJoined interface {
Unwrap() []error
}
type detailer interface {
Details() map[string]interface{}
}
func getExistingStackTrace(err error) []uintptr {
for err != nil {
switch e := err.(type) { //nolint:errorlint
case stackTracer:
return e.StackTrace()
case pkgStackTracer:
st := e.StackTrace()
return *(*[]uintptr)(unsafe.Pointer(&st))
case goErrorsStackTracer:
return e.Callers()
case erisStackTracer:
return e.StackFrames()
}
c, ok := err.(causer)
if ok && c.Cause() != nil {
return nil
}
e, ok := err.(unwrapperJoined)
if ok && len(e.Unwrap()) > 0 {
return nil
}
err = Unwrap(err)
}
return nil
}
// prefixMessage eagerly builds a new message with the provided prefixes.
// This is a trade-off which consumes more memory but allows one to cheaply
// call Error multiple times.
func prefixMessage(msg string, prefixes ...string) string {
message := strings.Builder{}
for i, prefix := range prefixes {
if len(prefix) > 0 {
message.WriteString(prefix)
if prefix[len(prefix)-1] != '\n' && (i < len(prefixes)-1 || len(msg) > 0) {
message.WriteString(": ")
}
}
}
if len(msg) > 0 {
message.WriteString(msg)
}
return message.String()
}
// This is a trade-off which consumes more memory but allows one to cheaply
// call Error multiple times.
func joinMessages(errs []error) string {
// Same implementation as standard library's joinError's Error.
var b []byte
for i, err := range errs {
if i > 0 {
b = append(b, '\n')
}
b = append(b, err.Error()...)
}
return string(b)
}
// E interface can be used in as a return type instead of the standard error
// interface to annotate which functions return an error with a stack trace
// and details.
// This is useful so that you know when you should use WithStack or WithDetails
// (for functions which do not return E) and when not (for functions which do
// return E).
//
// If you call WithStack on an error with a stack trace nothing bad happens
// (same error is simply returned), it just pollutes the code. So this
// interface is defined to help. (Calling WithDetails on an error with details
// adds an additional and independent layer of details on
// top of any existing details.)
type E interface {
error
stackTracer
detailer
}
// New returns an error with the supplied message.
// New also records the stack trace at the point it was called.
func New(message string) E {
return &fundamentalError{
msg: message,
stack: callers(0),
details: nil,
detailsMu: new(sync.Mutex),
}
}
// Errorf return an error with the supplied message
// formatted according to a format specifier.
// It supports %w format verb to wrap an existing error.
// Errorf also records the stack trace at the point it was called,
// unless wrapped error already have a stack trace.
// If %w is provided multiple times, then a stack trace is always recorded.
func Errorf(format string, args ...interface{}) E {
err := fmt.Errorf(format, args...) //nolint:err113
var errs []error
// Errorf itself maybe wrapped an error or errors so we can use a type switch here
// and do not need to (and should not) use As to determine if that happened.
switch u := err.(type) { //nolint:errorlint
case unwrapperJoined:
errs = u.Unwrap()
case unwrapper:
errs = []error{u.Unwrap()}
}
if len(errs) > 1 {
return &msgJoinedError{
errs: errs,
msg: err.Error(),
stack: callers(0),
details: nil,
detailsMu: new(sync.Mutex),
}
} else if len(errs) == 1 {
unwrap := errs[0]
st := getExistingStackTrace(unwrap)
if len(st) == 0 {
st = callers(0)
}
return &msgError{
err: unwrap,
msg: err.Error(),
stack: st,
details: nil,
detailsMu: new(sync.Mutex),
}
}
return &fundamentalError{
msg: err.Error(),
stack: callers(0),
details: nil,
detailsMu: new(sync.Mutex),
}
}
// fundamentalError is an error that has a message and a stack,
// but does not wrap another error.
type fundamentalError struct {
msg string
stack []uintptr
details map[string]interface{}
detailsMu *sync.Mutex
}
func (e *fundamentalError) Error() string {
if isCalledFromRuntimePanic() {
return fmt.Sprintf("% -+#.1v", Formatter{Error: e})
}
return e.msg
}
func (e *fundamentalError) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, formatString(s, verb), Formatter{Error: e})
}
func (e fundamentalError) MarshalJSON() ([]byte, error) {
return marshalJSONError(&e)
}
func (e *fundamentalError) StackTrace() []uintptr {
return e.stack
}
func (e *fundamentalError) Details() map[string]interface{} {
e.detailsMu.Lock()
defer e.detailsMu.Unlock()
if e.details == nil {
e.details = make(map[string]interface{})
}
return e.details
}
// msgError wraps another error and has its own stack and msg.
type msgError struct {
err error
msg string
stack []uintptr
details map[string]interface{}
detailsMu *sync.Mutex
}
func (e *msgError) Error() string {
if isCalledFromRuntimePanic() {
return fmt.Sprintf("% -+#.1v", Formatter{Error: e})
}
return e.msg
}
func (e *msgError) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, formatString(s, verb), Formatter{Error: e})
}
func (e msgError) MarshalJSON() ([]byte, error) {
return marshalJSONError(&e)
}
func (e *msgError) Unwrap() error {
return e.err
}
func (e *msgError) StackTrace() []uintptr {
return e.stack
}
func (e *msgError) Details() map[string]interface{} {
e.detailsMu.Lock()
defer e.detailsMu.Unlock()
if e.details == nil {
e.details = make(map[string]interface{})
}
return e.details
}
// msgJoinedError wraps multiple errors
// and has its own stack and msg.
type msgJoinedError struct {
errs []error
msg string
stack []uintptr
details map[string]interface{}
detailsMu *sync.Mutex
}
func (e *msgJoinedError) Error() string {
if isCalledFromRuntimePanic() {
return fmt.Sprintf("% -+#.1v", Formatter{Error: e})
}
return e.msg
}
func (e *msgJoinedError) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, formatString(s, verb), Formatter{Error: e})
}
func (e msgJoinedError) MarshalJSON() ([]byte, error) {
return marshalJSONError(&e)
}
func (e *msgJoinedError) Unwrap() []error {
return e.errs
}
func (e *msgJoinedError) StackTrace() []uintptr {
return e.stack
}
func (e *msgJoinedError) Details() map[string]interface{} {
e.detailsMu.Lock()
defer e.detailsMu.Unlock()
if e.details == nil {
e.details = make(map[string]interface{})
}
return e.details
}
func withStack(err error) E {
e, ok := err.(E) //nolint:errorlint
if ok {
if len(e.StackTrace()) == 0 {
return &noMsgError{
err: err,
stack: callers(1),
details: nil,
detailsMu: new(sync.Mutex),
}
}
return e
}
st := getExistingStackTrace(err)
if len(st) == 0 {
st = callers(1)
}
return &noMsgError{
err: err,
stack: st,
details: nil,
detailsMu: new(sync.Mutex),
}
}
// WithStack annotates err with a stack trace at the point WithStack was called,
// if err does not already have a stack trace.
// If err is nil, WithStack returns nil.
//
// Use WithStack instead of Wrap when you just want to convert an existing error
// into one with a stack trace. Use it as close to where the error originated
// as you can get.
//
// You can also use WithStack when you have an err which implements stackTracer
// interface but does not implement detailer interface as well, but you cannot
// provide initial details like you can with WithDetails.
//
// WithStack is similar to Errorf("%w", err), but returns err as-is if err
// already satisfies interface E and has a stack trace,
// and it returns nil if err is nil.
func WithStack(err error) E {
if err == nil {
return nil
}
return withStack(err)
}
// noMsgError wraps another error and has its
// own stack and but does not have its own msg.
type noMsgError struct {
err error
stack []uintptr
details map[string]interface{}
detailsMu *sync.Mutex
}
func (e *noMsgError) Error() string {
if isCalledFromRuntimePanic() {
return fmt.Sprintf("% -+#.1v", Formatter{Error: e})
}
return e.err.Error()
}
func (e *noMsgError) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, formatString(s, verb), Formatter{Error: e})
}
func (e noMsgError) MarshalJSON() ([]byte, error) {
return marshalJSONError(&e)
}
func (e *noMsgError) Unwrap() error {
return e.err
}
func (e *noMsgError) StackTrace() []uintptr {
return e.stack
}
func (e *noMsgError) Details() map[string]interface{} {
e.detailsMu.Lock()
defer e.detailsMu.Unlock()
if e.details == nil {
e.details = make(map[string]interface{})
}
return e.details
}
// Wrap returns an error annotating err with a stack trace
// at the point Wrap is called, and the supplied message.
// Wrapping is done even if err already has a stack trace.
// It records the original error as a cause.
// If err is nil, Wrap returns nil.
//
// Use Wrap when you want to make a new error with a different error message,
// while preserving the cause of the new error.
// If you want to reuse the err error message use WithMessage
// or Errorf instead.
func Wrap(err error, message string) E {
if err == nil {
return nil
}
return &causeError{
err: err,
msg: message,
stack: callers(0),
details: nil,
detailsMu: new(sync.Mutex),
}
}
// Wrapf returns an error annotating err with a stack trace
// at the point Wrapf is called, and the supplied message
// formatted according to a format specifier.
// Wrapping is done even if err already has a stack trace.
// It records the original error as a cause.
// It does not support %w format verb (use %s instead if you
// need to incorporate cause's error message).
// If err is nil, Wrapf returns nil.
//
// Use Wrapf when you want to make a new error with a different error message,
// preserving the cause of the new error.
// If you want to reuse the err error message use WithMessage
// or Errorf instead.
func Wrapf(err error, format string, args ...interface{}) E {
if err == nil {
return nil
}
return &causeError{
err: err,
msg: fmt.Sprintf(format, args...),
stack: callers(0),
details: nil,
detailsMu: new(sync.Mutex),
}
}
// causeError records another error as a causeError
// and has its own stack and msg.
type causeError struct {
err error
msg string
stack []uintptr
details map[string]interface{}
detailsMu *sync.Mutex
}
func (e *causeError) Error() string {
if isCalledFromRuntimePanic() {
return fmt.Sprintf("% -+#.1v", Formatter{Error: e})
}
return e.msg
}
func (e *causeError) Format(s fmt.State, verb rune) {
fmt.Fprintf(s, formatString(s, verb), Formatter{Error: e})
}
func (e causeError) MarshalJSON() ([]byte, error) {
return marshalJSONError(&e)
}
func (e *causeError) Unwrap() error {
return e.err
}
func (e *causeError) Cause() error {
return e.err
}
func (e *causeError) StackTrace() []uintptr {
return e.stack
}
func (e *causeError) Details() map[string]interface{} {
e.detailsMu.Lock()
defer e.detailsMu.Unlock()
if e.details == nil {
e.details = make(map[string]interface{})
}
return e.details
}
func withMessage(err error, prefix ...string) E {
st := getExistingStackTrace(err)
if len(st) == 0 {
st = callers(1)
}
return &msgError{
err: err,
msg: prefixMessage(err.Error(), prefix...),
stack: st,
details: nil,
detailsMu: new(sync.Mutex),
}
}
// WithMessage annotates err with a prefix message or messages.
// If err does not have a stack trace, stack strace is recorded as well.
//
// It does not support controlling the delimiter. Use Errorf if you need that.
//
// If err is nil, WithMessage returns nil.
//
// WithMessage is similar to Errorf("%s: %w", prefix, err), but supports
// dynamic number of prefixes, and it returns nil if err is nil.
func WithMessage(err error, prefix ...string) E {
if err == nil {
return nil
}
return withMessage(err, prefix...)
}
// WithMessagef annotates err with a prefix message
// formatted according to a format specifier.
// If err does not have a stack trace, stack strace is recorded as well.
//
// It does not support %w format verb or controlling the delimiter.
// Use Errorf if you need that.
//
// If err is nil, WithMessagef returns nil.
//
// WithMessagef is similar to Errorf(format + ": %w", args..., err), but
// it returns nil if err is nil.
func WithMessagef(err error, format string, args ...interface{}) E {
if err == nil {
return nil
}
return withMessage(err, fmt.Sprintf(format, args...))
}
// Cause returns the result of calling the Cause method on err, if err's
// type contains a Cause method returning error.
// Otherwise, the err is unwrapped and the process is repeated.
// If unwrapping is not possible, Cause returns nil.
// Unwrapping stops if it encounters an error with
// Unwrap() method returning multiple errors.
func Cause(err error) error {
for err != nil {
c, ok := err.(causer)
if ok {
cause := c.Cause()
if cause != nil {
return cause //nolint:wrapcheck
}
}
e, ok := err.(unwrapperJoined)
if ok && len(e.Unwrap()) > 0 {
return nil
}
err = Unwrap(err)
}
return err
}
// Unjoin returns the result of calling the Unwrap method on err, if err's
// type contains an Unwrap method returning multiple errors.
// Otherwise, the err is unwrapped and the process is repeated.
// If unwrapping is not possible, Unjoin returns nil.
// Unwrapping stops if it encounters an error with the Cause
// method returning error.
func Unjoin(err error) []error {
for err != nil {
e, ok := err.(unwrapperJoined)
if ok {
errs := e.Unwrap()
if len(errs) > 0 {
return errs
}
}
c, ok := err.(causer)
if ok && c.Cause() != nil {
return nil
}
err = Unwrap(err)
}
return nil
}
// Details returns the result of calling the Details method on err,
// if err's type contains a Details method returning initialized map.
// Otherwise, the err is unwrapped and the process is repeated.
// If unwrapping is not possible, Details returns nil.
// Unwrapping stops if it encounters an error with the Cause
// method returning error, or Unwrap() method returning
// multiple errors.
//
// You can modify returned map to modify err's details.
func Details(err error) map[string]interface{} {
for err != nil {
dd := detailsOf(err)
if dd != nil {
return dd
}
c, ok := err.(causer)
if ok && c.Cause() != nil {
return nil
}
e, ok := err.(unwrapperJoined)
if ok && len(e.Unwrap()) > 0 {
return nil
}
err = Unwrap(err)
}
return nil
}
// Returns details of the err if it implements detailer interface.
// It does not unwrap and recurse.
func detailsOf(err error) map[string]interface{} {
if err == nil {
return nil
}
d, ok := err.(detailer)
if ok {
return d.Details()
}
return nil
}
// AllDetails returns a map build from calling the Details method on err
// and populating the map with key/value pairs which are not yet
// present. Afterwards, the err is unwrapped and the process is repeated.
// Unwrapping stops if it encounters an error with the Cause
// method returning error, or Unwrap() method returning
// multiple errors.
func AllDetails(err error) map[string]interface{} {
res := make(map[string]interface{})
for err != nil {
for key, value := range detailsOf(err) {
if _, ok := res[key]; !ok {
res[key] = value
}
}
c, ok := err.(causer)
if ok && c.Cause() != nil {
return res
}
e, ok := err.(unwrapperJoined)
if ok && len(e.Unwrap()) > 0 {
return res
}
err = Unwrap(err)
}
return res
}
// allDetailsUntilCauseOrJoined builds a map with details unwrapping errors
// until it hits a cause or joined errors, also returning it or them.
// This also means that it does not traverse errors returned by Join.
func allDetailsUntilCauseOrJoined(err error) (res map[string]interface{}, cause error, errs []error) { //nolint:revive,stylecheck,nonamedreturns
res = make(map[string]interface{})
cause = nil
errs = nil
for err != nil {
for key, value := range detailsOf(err) {
if _, ok := res[key]; !ok {
res[key] = value
}
}
c, ok := err.(causer)
if ok {
cause = c.Cause()
}
e, ok := err.(unwrapperJoined)
if ok {
errs = e.Unwrap()
}
if cause != nil || len(errs) > 0 {
// It is possible that both cause and errs is set. One example is wrapError.
return
}
err = Unwrap(err)
}
return
}
// causeOrJoined unwraps err repeatedly until it hits a cause or joined errors,
// returning it or them.
// This also means that it does not traverse errors returned by Join.
func causeOrJoined(err error) (cause error, errs []error) { //nolint:revive,stylecheck,nonamedreturns
cause = nil
errs = nil
for err != nil {
c, ok := err.(causer)
if ok {
cause = c.Cause()
}
e, ok := err.(unwrapperJoined)
if ok {
errs = e.Unwrap()
}
if cause != nil || len(errs) > 0 {
// It is possible that both cause and errs is set. One example is wrapError.
return
}
err = Unwrap(err)
}
return
}
// WithDetails wraps err into an error which implements the detailer interface
// to access a map with optional additional details about the error.
//
// If err does not have a stack trace, then this call is equivalent
// to calling WithStack, annotating err with a stack trace as well.
//
// Use WithDetails when you have an err which implements stackTracer interface
// but does not implement detailer interface as well. You can also use
// WithStack for that but you cannot provide initial details using WithStack
// like you can with WithDetails.
//
// It is also useful when err does implement detailer interface, but you want
// to reuse same err multiple times (e.g., pass same err to multiple
// goroutines), adding different details each time. Calling WithDetails
// always wraps err and adds an additional and independent layer of
// details on top of any existing details.
//
// You can provide initial details by providing pairs of keys (strings)
// and values (interface{}).
func WithDetails(err error, kv ...interface{}) E {
if err == nil {
return nil
}
if len(kv)%2 != 0 {
panic(New("odd number of arguments for initial details"))
}
// We always initialize map because details were explicitly asked for.
initMap := make(map[string]interface{})
for i := 0; i < len(kv); i += 2 {
key, ok := kv[i].(string)
if !ok {
panic(Errorf(`key "%v" must be a string, not %T`, kv[i], kv[i]))
}
initMap[key] = kv[i+1]
}
// Even if err is of type E, we still wrap it into another noMsgError error to
// have another layer of details. This is where it is different from WithStack.
// We do not have to check for type E explicitly because E implements stackTracer
// so getExistingStackTrace returns its stack trace.
st := getExistingStackTrace(err)
if len(st) == 0 {
st = callers(0)
}
return &noMsgError{
err: err,
stack: st,
details: initMap,
detailsMu: new(sync.Mutex),
}
}
// Join returns an error that wraps the given errors.
// Join also records the stack trace at the point it was called.
// Any nil error values are discarded.
// Join returns nil if errs contains no non-nil values.
// If there is only one non-nil value, Join behaves
// like WithStack on the non-nil value.
// The error formats as the concatenation of the strings obtained
// by calling the Error method of each element of errs, with a newline
// between each string.
//
// Join is similar to Errorf("%w\n%w\n", err1, err2), but supports
// dynamic number of errors, skips nil errors, and it returns
// the error as-is if there is only one non-nil error already
// with a stack trace.
func Join(errs ...error) E {
nonNilErrs := make([]error, 0, len(errs))
for _, err := range errs {
if err != nil {
nonNilErrs = append(nonNilErrs, err)
}
}
if len(nonNilErrs) == 0 {
return nil
} else if len(nonNilErrs) == 1 {
return withStack(nonNilErrs[0])
}
return &msgJoinedError{
errs: nonNilErrs,
msg: joinMessages(nonNilErrs),
stack: callers(0),
details: nil,
detailsMu: new(sync.Mutex),
}
}
// wrapError joins two errors (err and with), making err the cause of with.
type wrapError struct {
err error
with error