-
Notifications
You must be signed in to change notification settings - Fork 52
/
eris_test.go
749 lines (703 loc) · 20.9 KB
/
eris_test.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
package eris_test
import (
"errors"
"fmt"
"reflect"
"runtime"
"strings"
"testing"
"github.com/rotisserie/eris"
)
var (
globalErr = eris.New("global error")
formattedGlobalErr = eris.Errorf("%v global error", "formatted")
)
type withMessage struct {
msg string
}
func (e withMessage) Error() string { return e.msg }
func (e withMessage) Is(target error) bool {
if err, ok := target.(withMessage); ok {
return e.msg == err.msg
}
return e.msg == target.Error()
}
type withLayer struct {
err error
msg string
}
func (e withLayer) Error() string { return e.msg + ": " + e.err.Error() }
func (e withLayer) Unwrap() error { return e.err }
func (e withLayer) Is(target error) bool {
if err, ok := target.(withLayer); ok {
return e.msg == err.msg
}
return e.msg == target.Error()
}
type withEmptyLayer struct {
err error
}
func (e withEmptyLayer) Error() string { return e.err.Error() }
func (e withEmptyLayer) Unwrap() error { return e.err }
func setupTestCase(wrapf bool, cause error, input []string) error {
err := cause
for _, str := range input {
if wrapf {
err = eris.Wrapf(err, "%v", str)
} else {
err = eris.Wrap(err, str)
}
}
return err
}
func TestErrorWrapping(t *testing.T) {
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
output string // expected output
}{
"nil root error": {
cause: nil,
input: []string{"additional context"},
},
"standard error wrapping with a global root cause": {
cause: globalErr,
input: []string{"additional context", "even more context"},
output: "even more context: additional context: global error",
},
"formatted error wrapping with a global root cause": {
cause: formattedGlobalErr,
input: []string{"additional context", "even more context"},
output: "even more context: additional context: formatted global error",
},
"standard error wrapping with a local root cause": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
output: "even more context: additional context: root error",
},
"standard error wrapping with a local root cause (eris.Errorf)": {
cause: eris.Errorf("%v root error", "formatted"),
input: []string{"additional context", "even more context"},
output: "even more context: additional context: formatted root error",
},
"no error wrapping with a local root cause (eris.Errorf)": {
cause: eris.Errorf("%v root error", "formatted"),
output: "formatted root error",
},
}
for desc, tc := range tests {
t.Run(desc, func(t *testing.T) {
err := setupTestCase(false, tc.cause, tc.input)
if err != nil && tc.cause == nil {
t.Errorf("%v: wrapping nil errors should return nil but got { %v }", desc, err)
} else if err != nil && tc.output != err.Error() {
t.Errorf("%v: expected { %v } got { %v }", desc, tc.output, err)
}
})
}
}
func TestExternalErrorWrapping(t *testing.T) {
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
output []string // expected output
}{
"no error wrapping with a third-party root cause (errors.New)": {
cause: errors.New("external error"),
output: []string{
"external error",
},
},
"standard error wrapping with a third-party root cause (errors.New)": {
cause: errors.New("external error"),
input: []string{"additional context", "even more context"},
output: []string{
"even more context: additional context: external error",
"additional context: external error",
"external error",
},
},
"wrapping a wrapped third-party root cause (errors.New and fmt.Errorf)": {
cause: fmt.Errorf("additional context: %w", errors.New("external error")),
input: []string{"even more context"},
output: []string{
"even more context: additional context: external error",
"additional context: external error",
"external error",
},
},
"wrapping a wrapped third-party root cause (multiple layers)": {
cause: fmt.Errorf("even more context: %w", fmt.Errorf("additional context: %w", errors.New("external error"))),
input: []string{"way too much context"},
output: []string{
"way too much context: even more context: additional context: external error",
"even more context: additional context: external error",
"additional context: external error",
"external error",
},
},
"wrapping a wrapped third-party root cause that contains an empty layer": {
cause: fmt.Errorf(": %w", errors.New("external error")),
input: []string{"even more context"},
output: []string{
"even more context: : external error",
": external error",
"external error",
},
},
"wrapping a wrapped third-party root cause that contains an empty layer without a delimiter": {
cause: fmt.Errorf("%w", errors.New("external error")),
input: []string{"even more context"},
output: []string{
"even more context: external error",
"external error",
"external error",
},
},
"wrapping a pkg/errors style error (contains layers without messages)": {
cause: &withLayer{ // var to mimic wrapping a pkg/errors style error
msg: "additional context",
err: &withEmptyLayer{
err: &withMessage{
msg: "external error",
},
},
},
input: []string{"even more context"},
output: []string{
"even more context: additional context: external error",
"additional context: external error",
"external error",
"external error",
},
},
}
for desc, tc := range tests {
t.Run(desc, func(t *testing.T) {
err := setupTestCase(false, tc.cause, tc.input)
// unwrap to make sure external errors are actually wrapped properly
var inputErr []string
for err != nil {
inputErr = append(inputErr, err.Error())
err = eris.Unwrap(err)
}
// compare each layer of the actual and expected output
if len(inputErr) != len(tc.output) {
t.Fatalf("%v: expected output to have '%v' layers but got '%v': { %#v } got { %#v }", desc, len(tc.output), len(inputErr), tc.output, inputErr)
}
for i := 0; i < len(inputErr); i++ {
if inputErr[i] != tc.output[i] {
t.Errorf("%v: expected { %#v } got { %#v }", desc, inputErr[i], tc.output[i])
}
}
})
}
}
func TestErrorUnwrap(t *testing.T) {
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
output []string // expected output
}{
"unwrapping error with internal root cause (eris.New)": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
output: []string{
"even more context: additional context: root error",
"additional context: root error",
"root error",
},
},
"unwrapping error with external root cause (errors.New)": {
cause: errors.New("external error"),
input: []string{"additional context", "even more context"},
output: []string{
"even more context: additional context: external error",
"additional context: external error",
"external error",
},
},
"unwrapping error with external root cause (custom type)": {
cause: &withMessage{
msg: "external error",
},
input: []string{"additional context", "even more context"},
output: []string{
"even more context: additional context: external error",
"additional context: external error",
"external error",
},
},
}
for desc, tc := range tests {
t.Run(desc, func(t *testing.T) {
err := setupTestCase(true, tc.cause, tc.input)
for _, out := range tc.output {
if err == nil {
t.Errorf("%v: unwrapping error returned nil but expected { %v }", desc, out)
} else if out != err.Error() {
t.Errorf("%v: expected { %v } got { %v }", desc, out, err)
}
err = eris.Unwrap(err)
}
})
}
}
func TestErrorIs(t *testing.T) {
externalErr := errors.New("external error")
customErr := withLayer{
msg: "additional context",
err: withEmptyLayer{
err: withMessage{
msg: "external error",
},
},
}
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
compare error // errors for comparison
output bool // expected comparison result
}{
"root error (internal)": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
compare: eris.New("root error"),
output: true,
},
"error not in chain": {
cause: eris.New("root error"),
compare: eris.New("other error"),
output: false,
},
"middle of chain (internal)": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
compare: eris.New("additional context"),
output: true,
},
"another in middle of chain (internal)": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
compare: eris.New("even more context"),
output: true,
},
"root error (external)": {
cause: externalErr,
input: []string{"additional context", "even more context"},
compare: externalErr,
output: true,
},
"wrapped error from global root error": {
cause: globalErr,
input: []string{"additional context", "even more context"},
compare: eris.Wrap(globalErr, "additional context"),
output: true,
},
"comparing against external error": {
cause: externalErr,
input: []string{"additional context", "even more context"},
compare: externalErr,
output: true,
},
"comparing against custom error type": {
cause: customErr,
input: []string{"even more context"},
compare: customErr,
output: true,
},
"comparing against custom error type (copied error)": {
cause: customErr,
input: []string{"even more context"},
compare: &withMessage{
msg: "external error",
},
output: true,
},
"comparing against nil error": {
cause: eris.New("root error"),
compare: nil,
output: false,
},
"comparing error against itself": {
cause: globalErr,
compare: globalErr,
output: true,
},
"comparing two nil errors": {
cause: nil,
compare: nil,
output: true,
},
}
for desc, tc := range tests {
t.Run(desc, func(t *testing.T) {
err := setupTestCase(false, tc.cause, tc.input)
if tc.output && !eris.Is(err, tc.compare) {
t.Errorf("%v: expected eris.Is('%v', '%v') to return true but got false", desc, err, tc.compare)
} else if !tc.output && eris.Is(err, tc.compare) {
t.Errorf("%v: expected eris.Is('%v', '%v') to return false but got true", desc, err, tc.compare)
}
})
}
}
func TestErrorAs(t *testing.T) {
externalError := errors.New("external error")
rootErr := eris.New("root error")
wrappedErr := eris.Wrap(rootErr, "additional context")
customErr := withLayer{
msg: "additional context",
err: withEmptyLayer{
err: withMessage{
msg: "external error",
},
},
}
tests := map[string]struct {
cause error // root error
target interface{} // errors for comparison
match bool // expected comparison result
output interface{} // value of target on match
}{
"nil error against nil target": {
cause: nil,
target: nil,
match: false,
output: nil,
},
"error against nil target": {
cause: rootErr,
target: nil,
match: false,
output: nil,
},
"error against non pointer interface": {
cause: rootErr,
target: rootErr,
match: false,
output: nil,
},
"error against non error interface": {
cause: rootErr,
target: &withMessage{"test"},
match: false,
output: nil,
},
"error against non pointer": {
cause: rootErr,
target: "test",
match: false,
output: nil,
},
"nil error against external target": {
cause: nil,
target: &externalError,
match: false,
output: nil,
},
"root error against external target": {
cause: eris.New("root error"),
target: &externalError,
match: false,
output: nil,
},
"wrapped error against external target": {
cause: wrappedErr,
target: &externalError,
match: false,
output: nil,
},
"nil wrapped error against root error target": {
cause: eris.Wrap(nil, "additional context"),
target: &rootErr,
match: false,
output: nil,
},
"wrapped error against its own root error target": {
cause: wrappedErr,
target: &rootErr,
match: true,
output: rootErr,
},
"root error against itself": {
cause: rootErr,
target: &rootErr,
match: true,
output: rootErr,
},
"root error against different root error": {
cause: eris.New("other root error"),
target: &rootErr,
match: false,
output: nil,
},
"wrapped error against same wrapped error": {
cause: wrappedErr,
target: &wrappedErr,
match: true,
output: wrappedErr,
},
"wrapped error against different wrapped error": {
cause: eris.Wrap(nil, "some other error"),
target: &wrappedErr,
match: false,
output: nil,
},
"custom error against similar type error": {
cause: customErr,
target: &withLayer{msg: "additional layer", err: externalError},
match: true,
output: customErr,
},
}
for desc, tc := range tests {
rtarget := reflect.ValueOf(tc.target)
t.Run(desc, func(t *testing.T) {
match := eris.As(tc.cause, tc.target)
if tc.match != match {
t.Fatalf("%v: expected eris.As('%v', '%v') to return %v but got %v", desc, tc.cause, reflect.ValueOf(tc.target).Elem(), tc.match, match)
}
if !match {
return
}
if got := rtarget.Elem().Interface(); got != tc.output {
t.Fatalf("%v: expected eris.As('%v', '%v') target interface value to be %#v, but got %#v", desc, tc.cause, reflect.ValueOf(tc.target).Elem(), tc.output, got)
}
})
}
}
func TestErrorCause(t *testing.T) {
globalErr := eris.New("global error")
extErr := errors.New("external error")
customErr := withMessage{
msg: "external error",
}
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
output error // expected output
}{
"internal root error": {
cause: globalErr,
input: []string{"additional context", "even more context"},
output: globalErr,
},
"external error": {
cause: extErr,
input: []string{"additional context", "even more context"},
output: extErr,
},
"external error (custom type)": {
cause: customErr,
input: []string{"additional context", "even more context"},
output: customErr,
},
"nil error": {
cause: nil,
output: nil,
},
}
for desc, tc := range tests {
t.Run(desc, func(t *testing.T) {
err := setupTestCase(false, tc.cause, tc.input)
cause := eris.Cause(err)
if tc.output != eris.Cause(err) {
t.Errorf("%v: expected { %v } got { %v }", desc, tc.output, cause)
}
})
}
}
func TestExternalErrorAs(t *testing.T) {
cause := withMessage{
msg: "external error",
}
empty := withEmptyLayer{
err: cause,
}
layer := withLayer{
msg: "additional context",
err: empty,
}
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
results []bool // comparison results
targets []error // output results
}{
"external cause": {
cause: cause,
input: []string{"even more context"},
results: []bool{true, false, false},
targets: []error{cause, nil, nil},
},
"external error with empty layer": {
cause: empty,
input: []string{"even more context"},
results: []bool{true, true, false},
targets: []error{cause, empty, nil},
},
"external error with multiple layers": {
cause: layer,
input: []string{"even more context"},
results: []bool{true, true, true},
targets: []error{cause, empty, layer},
},
}
for desc, tc := range tests {
t.Run(desc, func(t *testing.T) {
err := setupTestCase(false, tc.cause, tc.input)
msgTarget := withMessage{}
msgResult := errors.As(err, &msgTarget)
if tc.results[0] != msgResult {
t.Errorf("%v: expected errors.As('%v', &withMessage{}) to return {'%v', '%v'} but got {'%v', '%v'}",
desc, err, tc.results[0], tc.targets[0], msgResult, msgTarget)
} else if msgResult == true && tc.targets[0] != msgTarget {
t.Errorf("%v: expected errors.As('%v', &withMessage{}) to return {'%v', '%v'} but got {'%v', '%v'}",
desc, err, tc.results[0], tc.targets[0], msgResult, msgTarget)
}
emptyTarget := withEmptyLayer{}
emptyResult := errors.As(err, &emptyTarget)
if tc.results[1] != emptyResult {
t.Errorf("%v: expected errors.As('%v', &withEmptyLayer{}) to return {'%v', '%v'} but got {'%v', '%v'}",
desc, err, tc.results[1], tc.targets[1], emptyResult, emptyTarget)
} else if emptyResult == true && tc.targets[1] != emptyTarget {
t.Errorf("%v: expected errors.As('%v', &withEmptyLayer{}) to return {'%v', '%v'} but got {'%v', '%v'}",
desc, err, tc.results[1], tc.targets[1], emptyResult, emptyTarget)
}
layerTarget := withLayer{}
layerResult := errors.As(err, &layerTarget)
if tc.results[2] != layerResult {
t.Errorf("%v: expected errors.As('%v', &withLayer{}) to return {'%v', '%v'} but got {'%v', '%v'}",
desc, err, tc.results[2], tc.targets[2], layerResult, layerTarget)
} else if layerResult == true && tc.targets[2] != layerTarget {
t.Errorf("%v: expected errors.As('%v', &withLayer{}) to return {'%v', '%v'} but got {'%v', '%v'}",
desc, err, tc.results[2], tc.targets[2], layerResult, layerTarget)
}
})
}
}
type CustomErr struct{}
func (CustomErr) Error() string {
return "custom error"
}
func TestCustomErrorAs(t *testing.T) {
original := CustomErr{}
wrap1 := eris.Wrap(original, "wrap1")
wrap2 := eris.Wrap(wrap1, "wrap2")
var customErr CustomErr
if !eris.As(wrap1, &customErr) {
t.Errorf("expected errors.As(wrap1, &customErr) to return true but got false")
}
if !eris.As(wrap2, &customErr) {
t.Errorf("expected errors.As(wrap2, &customErr) to return true but got false")
}
}
func TestErrorFormatting(t *testing.T) {
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
output string // expected output
}{
"standard error wrapping with internal root cause (eris.New)": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
output: "even more context: additional context: root error",
},
"standard error wrapping with external root cause (errors.New)": {
cause: errors.New("external error"),
input: []string{"additional context", "even more context"},
output: "even more context: additional context: external error",
},
}
for desc, tc := range tests {
t.Run(desc, func(t *testing.T) {
err := setupTestCase(false, tc.cause, tc.input)
if err != nil && tc.cause == nil {
t.Errorf("%v: wrapping nil errors should return nil but got { %v }", desc, err)
} else if err != nil && tc.output != err.Error() {
t.Errorf("%v: expected { %v } got { %v }", desc, tc.output, err)
}
_ = fmt.Sprintf("error formatting results (%v):\n", desc)
_ = fmt.Sprintf("%v\n", err)
_ = fmt.Sprintf("%+v", err)
})
}
}
func getFrames(pc []uintptr) []eris.StackFrame {
var stackFrames []eris.StackFrame
if len(pc) == 0 {
return stackFrames
}
frames := runtime.CallersFrames(pc)
for {
frame, more := frames.Next()
i := strings.LastIndex(frame.Function, "/")
name := frame.Function[i+1:]
stackFrames = append(stackFrames, eris.StackFrame{
Name: name,
File: frame.File,
Line: frame.Line,
})
if !more {
break
}
}
return stackFrames
}
func getFrameFromLink(link eris.ErrLink) eris.Stack {
var stackFrames []eris.StackFrame
stackFrames = append(stackFrames, link.Frame)
return eris.Stack(stackFrames)
}
func TestStackFrames(t *testing.T) {
tests := map[string]struct {
cause error // root error
input []string // input for error wrapping
isWrapErr bool // flag for wrap error
}{
"root error": {
cause: eris.New("root error"),
isWrapErr: false,
},
"wrapped error": {
cause: eris.New("root error"),
input: []string{"additional context", "even more context"},
isWrapErr: true,
},
"external error": {
cause: errors.New("external error"),
isWrapErr: false,
},
"wrapped external error": {
cause: errors.New("external error"),
input: []string{"additional context", "even more context"},
isWrapErr: true,
},
"global root error": {
cause: globalErr,
isWrapErr: false,
},
"wrapped error from global root error": {
cause: globalErr,
input: []string{"additional context", "even more context"},
isWrapErr: true,
},
"nil error": {
cause: nil,
isWrapErr: false,
},
}
for desc, tc := range tests {
t.Run(desc, func(t *testing.T) {
err := setupTestCase(false, tc.cause, tc.input)
uErr := eris.Unpack(err)
sFrames := eris.Stack(getFrames(eris.StackFrames(err)))
if !tc.isWrapErr && !reflect.DeepEqual(uErr.ErrRoot.Stack, sFrames) {
t.Errorf("%v: expected { %v } got { %v }", desc, uErr.ErrRoot.Stack, sFrames)
}
if tc.isWrapErr && !reflect.DeepEqual(getFrameFromLink(uErr.ErrChain[0]), sFrames) {
t.Errorf("%v: expected { %v } got { %v }", desc, getFrameFromLink(uErr.ErrChain[0]), sFrames)
}
})
}
}