-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
errors_test.go
778 lines (629 loc) · 33.4 KB
/
errors_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
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
package errors_test
import (
"encoding/json"
stderrors "errors"
"fmt"
"io"
"runtime"
"strings"
"sync"
"testing"
pkgerrors "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/tozd/go/errors"
)
func callers() []uintptr {
const depth = 32
var pcs [depth]uintptr
n := runtime.Callers(3, pcs[:])
return pcs[0:n]
}
type stackTracer interface {
StackTrace() []uintptr
}
func w() string {
return "%w"
}
type errorWithFormat struct {
vMsg string
}
func (*errorWithFormat) Error() string {
return "foobar"
}
func (e *errorWithFormat) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
io.WriteString(s, e.vMsg)
return
}
fallthrough
case 's':
io.WriteString(s, "foobar1")
case 'q':
fmt.Fprintf(s, "%q", "foobar2")
}
}
type errorWithFormatAndStack struct {
vMsg string
}
func (*errorWithFormatAndStack) Error() string {
return "foobar"
}
func (e *errorWithFormatAndStack) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
io.WriteString(s, e.vMsg)
return
}
fallthrough
case 's':
io.WriteString(s, "foobar1")
case 'q':
fmt.Fprintf(s, "%q", "foobar2")
}
}
func (e *errorWithFormatAndStack) StackTrace() []uintptr {
return nil
}
type errorWithCauseAndWrap struct {
msg string
cause error
wrap error
}
func (e *errorWithCauseAndWrap) Error() string {
return e.msg
}
func (e *errorWithCauseAndWrap) Cause() error {
return e.cause
}
func (e *errorWithCauseAndWrap) Unwrap() error {
return e.wrap
}
func copyThroughJSON(t *testing.T, e interface{}) error {
t.Helper()
jsonError, err := json.Marshal(e)
require.NoError(t, err)
e2, errE := errors.UnmarshalJSON(jsonError)
require.NoError(t, errE, "% -+#.1v", errE)
jsonError2, err := json.Marshal(e2)
require.NoError(t, err)
assert.Equal(t, jsonError, jsonError2)
return e2 //nolint:wrapcheck
}
func stackOffset(t *testing.T) int {
t.Helper()
var pcs [1]uintptr
n := runtime.Callers(2, pcs[:])
frames := runtime.CallersFrames(pcs[:n])
f, _ := frames.Next()
return f.Line
}
type testStruct struct {
err error
want string
format string
stack int
extra int
}
var tests []testStruct
func init() {
// We make errors inside a function so that the stack trace is
// different from errors made through errors.* call.
parentErr, noMsgErr := func() (errors.E, errors.E) {
return errors.New("parent"), errors.New("")
}()
parentWithFormat1Err, parentWithFormat2Err := func() (errors.E, errors.E) {
return errors.WithStack(&errorWithFormat{"foobar\nmore data"}), errors.WithStack(&errorWithFormat{"foobar\nmore data\n"})
}()
parentPkgError := func() error {
return pkgerrors.New("parent")
}()
parentNoStackErr := stderrors.New("parent")
noMsgNoStackErr := stderrors.New("")
// Current stack plus call to errors.*.
currentStackSize := len(callers()) + 1
parentErrStackSize := len(parentErr.StackTrace())
tests = append(tests, []testStruct{
// errors.New
{errors.New(""), "", "% +-.1v", currentStackSize, 0},
{errors.New("foo"), "foo", "% +-.1v", currentStackSize, 1},
{errors.New("string with format specifiers: %v"), "string with format specifiers: %v", "% +-.1v", currentStackSize, 1},
{errors.New("foo with newline\n"), "foo with newline\n", "% +-.1v", currentStackSize, 1},
// errors.Errorf without %w
{errors.Errorf(""), "", "% +-.1v", currentStackSize, 0},
{errors.Errorf("read error without format specifiers"), "read error without format specifiers", "% +-.1v", currentStackSize, 1},
{errors.Errorf("read error with %d format specifier", 1), "read error with 1 format specifier", "% +-.1v", currentStackSize, 1},
{errors.Errorf("read error with newline\n"), "read error with newline\n", "% +-.1v", currentStackSize, 1},
// errors.Errorf with %w and parent with stack
{errors.Errorf("read error with parent: %w", parentErr), "read error with parent: parent", "% +-.1v", parentErrStackSize, 1},
{errors.Errorf(`read error (parent "%w")`, parentErr), `read error (parent "parent")`, "% +-.1v", parentErrStackSize, 1},
{errors.Errorf("read error (%w) and newline\n", parentErr), "read error (parent) and newline\n", "% +-.1v", parentErrStackSize, 1},
{errors.Errorf("%w", noMsgErr), "", "% +-.1v", parentErrStackSize, 0},
// errors.Errorf with %w and parent without stack
{errors.Errorf("read error with parent: %w", parentNoStackErr), "read error with parent: parent", "% +-.1v", currentStackSize, 1},
{errors.Errorf(`read error (parent "%w")`, parentNoStackErr), `read error (parent "parent")`, "% +-.1v", currentStackSize, 1},
{errors.Errorf("read error (%w) and newline\n", parentNoStackErr), "read error (parent) and newline\n", "% +-.1v", currentStackSize, 1},
{errors.Errorf("%w", noMsgNoStackErr), "", "% +-.1v", currentStackSize, 0},
// errors.WithStack and parent without stack
{errors.WithStack(io.EOF), "EOF", "% +-.1v", currentStackSize, 1},
{errors.WithStack(errors.Base("EOF")), "EOF", "% +-.1v", currentStackSize, 1},
{errors.WithStack(errors.Base("")), "", "% +-.1v", currentStackSize, 0},
{errors.WithStack(errors.Base("foobar\n")), "foobar\n", "% +-.1v", currentStackSize, 1},
// errors.WithStack and parent with stack
{errors.WithStack(parentErr), "parent", "% +-.1v", parentErrStackSize, 1},
{errors.WithStack(noMsgErr), "", "% +-.1v", parentErrStackSize, 0},
// errors.WithStack and parent with custom %+v which is ignored
{errors.WithStack(&errorWithFormat{"foobar\nmore data"}), "foobar", "% +-.1v", currentStackSize, 1},
{errors.WithStack(&errorWithFormat{"foobar\nmore data\n"}), "foobar", "% +-.1v", currentStackSize, 1},
// errors.WithDetails and parent without stack
{errors.WithDetails(io.EOF), "EOF", "% +-.1v", currentStackSize, 1},
{errors.WithDetails(errors.Base("EOF")), "EOF", "% +-.1v", currentStackSize, 1},
{errors.WithDetails(errors.Base("")), "", "% +-.1v", currentStackSize, 0},
{errors.WithDetails(errors.Base("foobar\n")), "foobar\n", "% +-.1v", currentStackSize, 1},
// errors.WithDetails and parent with stack
{errors.WithDetails(parentErr), "parent", "% +-.1v", parentErrStackSize, 1},
{errors.WithDetails(noMsgErr), "", "% +-.1v", parentErrStackSize, 0},
// errors.WithDetails and parent with custom %+v which is ignored
{errors.WithDetails(&errorWithFormat{"foobar\nmore data"}), "foobar", "% +-.1v", currentStackSize, 1},
{errors.WithDetails(&errorWithFormat{"foobar\nmore data\n"}), "foobar", "% +-.1v", currentStackSize, 1},
// errors.WithMessage and parent without stack
{errors.WithMessage(parentNoStackErr, "read error"), "read error: parent", "% +-.1v", currentStackSize, 1},
{errors.WithMessage(parentNoStackErr, ""), "parent", "% +-.1v", currentStackSize, 1},
{errors.WithMessage(parentNoStackErr, "read error\n"), "read error\nparent", "% +-.1v", currentStackSize, 2},
{errors.WithMessage(noMsgNoStackErr, "read error"), "read error", "% +-.1v", currentStackSize, 1},
{errors.WithMessage(noMsgNoStackErr, ""), "", "% +-.1v", currentStackSize, 0},
{errors.WithMessage(io.EOF, "read error"), "read error: EOF", "% +-.1v", currentStackSize, 1},
// errors.WithMessage twice
{errors.WithMessage(errors.WithMessage(io.EOF, "read error"), "client error"), "client error: read error: EOF", "% +-.1v", currentStackSize, 1},
// errors.WithMessage and parent with stack
{errors.WithMessage(parentErr, "read error"), "read error: parent", "% +-.1v", parentErrStackSize, 1},
{errors.WithMessage(parentErr, ""), "parent", "% +-.1v", parentErrStackSize, 1},
{errors.WithMessage(parentErr, "read error\n"), "read error\nparent", "% +-.1v", parentErrStackSize, 2},
{errors.WithMessage(noMsgErr, ""), "", "% +-.1v", parentErrStackSize, 0},
{errors.WithMessage(noMsgErr, "read error"), "read error", "% +-.1v", parentErrStackSize, 1},
// errors.WithMessage and parent with custom %+v which is ignored and no stack
{errors.WithMessage(&errorWithFormat{"foobar\nmore data"}, "read error"), "read error: foobar", "% +-.1v", currentStackSize, 1},
{errors.WithMessage(&errorWithFormat{"foobar\nmore data\n"}, "read error"), "read error: foobar", "% +-.1v", currentStackSize, 1},
// errors.WithMessage and parent with custom %+v which is ignored and stack
{errors.WithMessage(parentWithFormat1Err, "read error"), "read error: foobar", "% +-.1v", parentErrStackSize, 1},
{errors.WithMessage(parentWithFormat2Err, "read error"), "read error: foobar", "% +-.1v", parentErrStackSize, 1},
// errors.WithMessagef
{errors.WithMessagef(parentNoStackErr, "read error %d", 1), "read error 1: parent", "% +-.1v", currentStackSize, 1},
// We use w() to prevent static analysis.
{errors.WithMessagef(parentNoStackErr, "read error ("+w()+")", noMsgNoStackErr), "read error (%!w(*errors.errorString=&{})): parent", "% +-.1v", currentStackSize, 1},
// errors.Wrap and parent without stack, there are three lines extra for
// "the above error was caused by the following error" + lines for error messages
{errors.Wrap(parentNoStackErr, "read error"), "read error", "% +-.1v", currentStackSize, 3 + 2},
{errors.Wrap(parentNoStackErr, ""), "", "% +-.1v", currentStackSize, 3 + 1},
{errors.Wrap(parentNoStackErr, "read error\n"), "read error\n", "% +-.1v", currentStackSize, 3 + 2},
{errors.Wrap(io.EOF, "read error"), "read error", "% +-.1v", currentStackSize, 3 + 2},
// There is no "the above error was caused by the following error" message.
{errors.Wrap(noMsgNoStackErr, "read error"), "read error", "% +-.1v", currentStackSize, 1},
{errors.Wrap(noMsgNoStackErr, ""), "", "% +-.1v", currentStackSize, 0},
// errors.Wrap and parent with stack, there are three lines extra for
// "the above error was caused by the following error" + lines for error messages
// + 1 for additional stack trace (most recent call first)" line
{errors.Wrap(parentErr, "read error"), "read error", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 2 + 1},
{errors.Wrap(parentErr, ""), "", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 1 + 1},
{errors.Wrap(parentErr, "read error\n"), "read error\n", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 2 + 1},
{errors.Wrap(noMsgErr, "read error"), "read error", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 1 + 1},
{errors.Wrap(noMsgErr, ""), "", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 0 + 1},
// errors.Wrap and parent with custom %+v and no stack, there are three lines extra for
// "the above error was caused by the following error" + lines for error messages
{errors.Wrap(&errorWithFormat{"foobar\nmore data"}, "read error"), "read error", "% +-.1v", currentStackSize, 3 + 3},
{errors.Wrap(&errorWithFormat{"foobar\nmore data\n"}, "read error"), "read error", "% +-.1v", currentStackSize, 3 + 3},
// errors.Wrap and parent with custom %+v and no stack, there are three lines extra for
// "the above error was caused by the following error" + lines for error messages
{errors.Wrap(&errorWithFormat{"foobar\nmore data"}, "read error"), "read error", "% +-.3v", currentStackSize, 3 + 3},
{errors.Wrap(&errorWithFormat{"foobar\nmore data\n"}, "read error"), "read error", "% +-.3v", currentStackSize, 3 + 3},
// errors.Wrap and parent with custom %+v (which is ignored) and stack there are three lines extra for
// "the above error was caused by the following error" + lines for error messages
// + 1 for additional stack trace (most recent call first)" line
{errors.Wrap(parentWithFormat1Err, "read error"), "read error", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 2 + 1},
{errors.Wrap(parentWithFormat2Err, "read error"), "read error", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 2 + 1},
// errors.Wrap and parent with custom %+v (which is ignored) and stack there are three lines extra for
// "the above error was caused by the following error" + lines for error messages
// + 1 for additional stack trace (most recent call first)" line
{errors.Wrap(parentWithFormat1Err, "read error"), "read error", "% +-.3v", currentStackSize + parentErrStackSize, 3 + 2 + 1},
{errors.Wrap(parentWithFormat2Err, "read error"), "read error", "% +-.3v", currentStackSize + parentErrStackSize, 3 + 2 + 1},
// errors.Wrapf
{errors.Wrapf(parentNoStackErr, "read error %d", 1), "read error 1", "% +-.1v", currentStackSize, 3 + 2},
// We use w() to prevent static analysis.
{errors.Wrapf(parentNoStackErr, "read error ("+w()+")", noMsgNoStackErr), "read error (%!w(*errors.errorString=&{}))", "% +-.1v", currentStackSize, 3 + 2},
// errors.Errorf with %w and github.com/pkg/errors parent,
// we format the stack trace in this case
{errors.Errorf("read error with parent: %w", parentPkgError), "read error with parent: parent", "% +-.1v", parentErrStackSize, 1},
// errors.WithStack and github.com/pkg/errors parent,
// we format the stack trace in this case
{errors.WithStack(parentPkgError), "parent", "% +-.1v", parentErrStackSize, 1},
// errors.WithStack and github.com/pkg/errors parent,
// we format the stack trace in this case
{errors.WithStack(parentPkgError), "parent", "% +-.1v", parentErrStackSize, 1},
// errors.WithDetails and github.com/pkg/errors parent,
// we format the stack trace in this case
{errors.WithDetails(parentPkgError), "parent", "% +-.1v", parentErrStackSize, 1},
// errors.WithDetails and github.com/pkg/errors parent,
// we format the stack trace in this case
{errors.WithDetails(parentPkgError), "parent", "% +-.1v", parentErrStackSize, 1},
// errors.Wrap and github.com/pkg/errors parent,
// we format the stack trace in this case
{errors.Wrap(parentPkgError, "read error"), "read error", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 3},
// errors.Wrap and github.com/pkg/errors parent,
// formatting of the cause is fully done by parentPkgError in this case,
// there are still three lines extra for "The above error was caused by the
// following error" + lines for error messages, but
// there is no second "stack trace (most recent call first)" line,
// a final newline is still added
{errors.Wrap(parentPkgError, "read error"), "read error", "% +-.3v", currentStackSize + parentErrStackSize, 3 + 2},
// errors.WithMessage and github.com/pkg/errors parent,
// we format the stack trace in this case
{errors.WithMessage(parentPkgError, "read error"), "read error: parent", "% +-.1v", parentErrStackSize, 1},
// errors.WithMessage and github.com/pkg/errors parent,
// we format the stack trace in this case
{errors.WithMessage(parentPkgError, "read error"), "read error: parent", "% +-.3v", parentErrStackSize, 1},
// errors.Join.
{errors.Join(errors.Base("foo1"), errors.Base("foo2")), "foo1\nfoo2", "% +-.1v", currentStackSize, 2 + 3 + 1 + 1 + 1},
{errors.Join(errors.New("foo1"), errors.New("foo2")), "foo1\nfoo2", "% +-.1v", 3 * currentStackSize, 2 + 3 + 2 + 3},
// errors.WrapWith and parent without stack, there are three lines extra for
// "the above error was caused by the following error" + lines for error messages
{errors.WrapWith(parentNoStackErr, errors.Base("read error")), "read error", "% +-.1v", currentStackSize, 3 + 2},
{errors.WrapWith(parentNoStackErr, errors.Base("")), "", "% +-.1v", currentStackSize, 3 + 1},
{errors.WrapWith(parentNoStackErr, errors.Base("read error\n")), "read error\n", "% +-.1v", currentStackSize, 3 + 2},
{errors.WrapWith(io.EOF, errors.Base("read error")), "read error", "% +-.1v", currentStackSize, 3 + 2},
// There is no "the above error was caused by the following error" message.
{errors.WrapWith(noMsgNoStackErr, errors.Base("read error")), "read error", "% +-.1v", currentStackSize, 1},
{errors.WrapWith(noMsgNoStackErr, errors.Base("")), "", "% +-.1v", currentStackSize, 0},
// errors.WrapWith and parent with stack, there are three lines extra for
// "the above error was caused by the following error" + lines for error messages
// + 1 for additional stack trace (most recent call first)" line
{errors.WrapWith(parentErr, errors.Base("read error")), "read error", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 2 + 1},
{errors.WrapWith(parentErr, errors.Base("")), "", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 1 + 1},
{errors.WrapWith(parentErr, errors.Base("read error\n")), "read error\n", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 2 + 1},
{errors.WrapWith(noMsgErr, errors.Base("read error")), "read error", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 1 + 1},
{errors.WrapWith(noMsgErr, errors.Base("")), "", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 0 + 1},
// errors.WrapWith and parent with custom %+v and no stack, there are three lines extra for
// "the above error was caused by the following error" + lines for error messages
{errors.WrapWith(&errorWithFormat{"foobar\nmore data"}, errors.Base("read error")), "read error", "% +-.1v", currentStackSize, 3 + 3},
{errors.WrapWith(&errorWithFormat{"foobar\nmore data\n"}, errors.Base("read error")), "read error", "% +-.1v", currentStackSize, 3 + 3},
// errors.WrapWith and parent with custom %+v and no stack, there are three lines extra for
// "the above error was caused by the following error" + lines for error messages
{errors.WrapWith(&errorWithFormat{"foobar\nmore data"}, errors.Base("read error")), "read error", "% +-.3v", currentStackSize, 3 + 3},
{errors.WrapWith(&errorWithFormat{"foobar\nmore data\n"}, errors.Base("read error")), "read error", "% +-.3v", currentStackSize, 3 + 3},
// errors.WrapWith and parent with custom %+v (which is ignored) and stack there are three lines extra for
// "the above error was caused by the following error" + lines for error messages
// + 1 for additional stack trace (most recent call first)" line
{errors.WrapWith(parentWithFormat1Err, errors.Base("read error")), "read error", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 2 + 1},
{errors.WrapWith(parentWithFormat2Err, errors.Base("read error")), "read error", "% +-.1v", currentStackSize + parentErrStackSize, 3 + 2 + 1},
// errors.WrapWith and parent with custom %+v (which is ignored) and stack there are three lines extra for
// "the above error was caused by the following error" + lines for error messages
// + 1 for additional stack trace (most recent call first)" line
{errors.WrapWith(parentWithFormat1Err, errors.Base("read error")), "read error", "% +-.3v", currentStackSize + parentErrStackSize, 3 + 2 + 1},
{errors.WrapWith(parentWithFormat2Err, errors.Base("read error")), "read error", "% +-.3v", currentStackSize + parentErrStackSize, 3 + 2 + 1},
// errors.WrapWith and github.com/pkg/errors parent,
// formatting of the cause is fully done by parentPkgError in this case,
// there are still three lines extra for "The above error was caused by the
// following error" + lines for error messages, but
// there is no second "stack trace (most recent call first)" line,
// a final newline is still added
{errors.WrapWith(parentPkgError, errors.Base("read error")), "read error", "% +-.3v", currentStackSize + parentErrStackSize, 3 + 2},
// errors.Prefix and parent without stack
{errors.Prefix(parentNoStackErr, errors.Base("read error")), "read error: parent", "% +-.1v", currentStackSize, 1 + 2 + 2 + 2},
{errors.Prefix(parentNoStackErr, errors.Base("")), "parent", "% +-.1v", currentStackSize, 1},
{errors.Prefix(parentNoStackErr, errors.Base("read error\n")), "read error\nparent", "% +-.1v", currentStackSize, 2 + 2 + 2 + 2},
{errors.Prefix(noMsgNoStackErr, errors.Base("read error")), "read error", "% +-.1v", currentStackSize, 1},
{errors.Prefix(noMsgNoStackErr, errors.Base("")), "", "% +-.1v", currentStackSize, 0},
{errors.Prefix(io.EOF, errors.Base("read error")), "read error: EOF", "% +-.1v", currentStackSize, 1 + 2 + 2 + 2},
// errors.Prefix twice
{errors.Prefix(errors.Prefix(io.EOF, errors.Base("read error")), errors.Base("client error")), "client error: read error: EOF", "% +-.1v", currentStackSize + currentStackSize, 1 + 2 + 2 + 3 + 2 + 2 + 2},
// errors.Prefix and parent with stack
{errors.Prefix(parentErr, errors.Base("read error")), "read error: parent", "% +-.1v", parentErrStackSize + parentErrStackSize, 1 + 2 + 2 + 3},
{errors.Prefix(parentErr, errors.Base("")), "parent", "% +-.1v", parentErrStackSize, 1},
{errors.Prefix(parentErr, errors.Base("read error\n")), "read error\nparent", "% +-.1v", parentErrStackSize + parentErrStackSize, 2 + 2 + 2 + 3},
{errors.Prefix(noMsgErr, errors.Base("")), "", "% +-.1v", parentErrStackSize, 0},
{errors.Prefix(noMsgErr, errors.Base("read error")), "read error", "% +-.1v", parentErrStackSize + parentErrStackSize, 1 + 2 + 2},
}...)
}
func TestErrors(t *testing.T) {
t.Parallel()
for k, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
t.Parallel()
assert.EqualError(t, tt.err, tt.want)
assert.Implements(t, (*stackTracer)(nil), tt.err)
assert.Equal(t, tt.want, fmt.Sprintf("%s", tt.err))
assert.Equal(t, tt.want, fmt.Sprintf("%v", tt.err))
assert.Equal(t, fmt.Sprintf("%q", tt.want), fmt.Sprintf("%q", tt.err))
stackTrace := fmt.Sprintf(tt.format, tt.err)
// Expected stack size (2 lines per frame), plus "stack trace
// (most recent call first)" line, plus extra lines.
assert.Equal(t, tt.stack*2+1+tt.extra, strings.Count(stackTrace, "\n"), stackTrace)
})
}
}
func TestWithStackNil(t *testing.T) {
t.Parallel()
assert.Nil(t, errors.WithStack(nil))
assert.NoError(t, copyThroughJSON(t, errors.WithStack(nil)))
}
func TestWrapNil(t *testing.T) {
t.Parallel()
assert.Nil(t, errors.Wrap(nil, "x"))
assert.NoError(t, copyThroughJSON(t, errors.Wrap(nil, "x")))
}
func TestWrapfNil(t *testing.T) {
t.Parallel()
assert.Nil(t, errors.Wrapf(nil, "x"))
assert.NoError(t, copyThroughJSON(t, errors.Wrapf(nil, "x")))
}
func TestWithDetailsNil(t *testing.T) {
t.Parallel()
assert.Nil(t, errors.WithDetails(nil))
assert.NoError(t, copyThroughJSON(t, errors.WithDetails(nil)))
}
func TestWithMessageNil(t *testing.T) {
t.Parallel()
assert.Nil(t, errors.WithMessage(nil, "no error"))
assert.NoError(t, copyThroughJSON(t, errors.WithMessage(nil, "no error")))
}
func TestWithMessagefNil(t *testing.T) {
t.Parallel()
assert.Nil(t, errors.WithMessagef(nil, "no error"))
assert.NoError(t, copyThroughJSON(t, errors.WithMessagef(nil, "no error")))
}
func TestJoinNil(t *testing.T) {
t.Parallel()
assert.Nil(t, errors.Join(nil))
assert.Nil(t, errors.Join(nil, nil))
assert.NoError(t, copyThroughJSON(t, errors.Join(nil)))
assert.NoError(t, copyThroughJSON(t, errors.Join(nil, nil)))
}
func TestWrapWithNil(t *testing.T) {
t.Parallel()
assert.Nil(t, errors.WrapWith(nil, errors.Base("x")))
assert.NoError(t, copyThroughJSON(t, errors.WrapWith(nil, errors.Base("x"))))
}
// stderrors.New, etc values are not expected to be compared by value.
// Assert that various kinds of errors have a functional equality operator,
// even if the result of that equality is always false.
func TestErrorEquality(t *testing.T) {
t.Parallel()
vals := []error{
nil,
io.EOF,
stderrors.New("EOF"),
errors.New("EOF"),
errors.Errorf("EOF"),
errors.Wrap(io.EOF, "EOF"),
errors.Wrapf(io.EOF, "EOF%d", 2),
errors.WithMessage(nil, "whoops"),
errors.WithMessage(io.EOF, "whoops"),
errors.WithStack(io.EOF),
errors.WithStack(nil),
}
for i := range vals {
for j := range vals {
// Must not panic.
_ = vals[i] == vals[j] //nolint:errorlint
}
}
}
func TestBases(t *testing.T) {
t.Parallel()
// Current stack plus call to errors.*.
currentStackSize := len(callers()) + 1
grandparent := errors.Base("grandparent")
parent := errors.BaseWrap(grandparent, "parent")
err := errors.WithStack(parent)
assert.EqualError(t, err, "parent")
assert.Implements(t, (*stackTracer)(nil), err)
stackTrace := fmt.Sprintf("% +-v", err)
// Expected stack size (2 lines per frame), plus "stack trace
// (most recent call first)" line, plus extra lines.
assert.Equal(t, currentStackSize*2+1+1, strings.Count(stackTrace, "\n"), stackTrace)
assert.ErrorIs(t, err, parent)
assert.ErrorIs(t, err, grandparent)
}
func TestCause(t *testing.T) {
t.Parallel()
assert.NoError(t, errors.Cause(errors.Base("foo")))
assert.NoError(t, errors.Cause(errors.New("foo")))
assert.NoError(t, errors.Cause(errors.WithMessage(errors.Base("foo"), "bar")))
err := errors.Base("foo")
assert.Equal(t, err, errors.Cause(errors.Wrap(err, "bar")))
assert.Equal(t, err, errors.Cause(errors.WithMessage(errors.Wrap(err, "bar"), "zar")))
wrap := &errorWithCauseAndWrap{"test", nil, nil}
assert.NoError(t, errors.Cause(wrap))
wrap.wrap = err
assert.NoError(t, errors.Cause(wrap))
wrap.wrap = errors.Wrap(err, "bar")
assert.Equal(t, err, errors.Cause(wrap))
}
func TestDetails(t *testing.T) {
t.Parallel()
err := errors.New("test")
errors.Details(err)["zoo"] = "base"
errors.Details(err)["foo"] = "bar"
assert.Equal(t, map[string]interface{}{"zoo": "base", "foo": "bar"}, errors.Details(err))
assert.Equal(t, map[string]interface{}{"zoo": "base", "foo": "bar"}, errors.AllDetails(err))
assert.Equal(t, map[string]interface{}{"zoo": "base", "foo": "bar"}, errors.AllDetails(copyThroughJSON(t, err)))
err2 := errors.WithDetails(err)
errors.Details(err2)["foo"] = "baz"
errors.Details(err2)["foo2"] = "bar2"
assert.Equal(t, map[string]interface{}{"zoo": "base", "foo": "bar"}, errors.Details(err))
assert.Equal(t, map[string]interface{}{"zoo": "base", "foo": "bar"}, errors.AllDetails(err))
assert.Equal(t, map[string]interface{}{"foo2": "bar2", "foo": "baz"}, errors.Details(err2))
assert.Equal(t, map[string]interface{}{"foo2": "bar2", "foo": "baz", "zoo": "base"}, errors.AllDetails(err2))
assert.Equal(t, map[string]interface{}{"foo2": "bar2", "foo": "baz", "zoo": "base"}, errors.AllDetails(copyThroughJSON(t, err2)))
err3 := errors.WithDetails(err, "foo", "baz", "foo2", "bar2")
assert.Equal(t, map[string]interface{}{"foo2": "bar2", "foo": "baz", "zoo": "base"}, errors.AllDetails(err3))
assert.Equal(t, map[string]interface{}{"foo2": "bar2", "foo": "baz", "zoo": "base"}, errors.AllDetails(copyThroughJSON(t, err3)))
err4 := errors.Wrap(err3, "cause")
errors.Details(err4)["foo"] = "baz2"
errors.Details(err4)["foo2"] = "bar3"
assert.Equal(t, map[string]interface{}{"foo2": "bar3", "foo": "baz2"}, errors.AllDetails(err4))
assert.Equal(t, map[string]interface{}{"foo2": "bar3", "foo": "baz2"}, errors.AllDetails(copyThroughJSON(t, err4)))
}
type testStructDetails struct {
msg string
details map[string]interface{}
detailsMu *sync.Mutex
parent error
}
func (e *testStructDetails) Error() string {
return e.msg
}
func (e *testStructDetails) Unwrap() error {
return e.parent
}
func (e *testStructDetails) 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 TestWrapWith(t *testing.T) {
t.Parallel()
ioBaseErr := errors.Base("IO error")
eofBaseErr := errors.BaseWrap(ioBaseErr, "IO error: EOF")
testErr := errors.WrapWith(io.EOF, eofBaseErr)
errors.Details(testErr)["foo"] = "baz"
errors.Details(testErr)["foo2"] = "bar2"
assert.True(t, errors.Is(testErr, io.EOF))
assert.True(t, errors.Is(testErr, ioBaseErr))
assert.True(t, errors.Is(testErr, eofBaseErr))
assert.Equal(t, map[string]interface{}{"foo2": "bar2", "foo": "baz"}, errors.Details(testErr))
assert.Equal(t, map[string]interface{}{"foo2": "bar2", "foo": "baz"}, errors.AllDetails(testErr))
assert.Equal(t, map[string]interface{}{"foo2": "bar2", "foo": "baz"}, errors.AllDetails(copyThroughJSON(t, testErr)))
assert.Equal(t, []error{eofBaseErr, io.EOF}, errors.Unjoin(testErr))
assert.NoError(t, errors.Unwrap(testErr)) //nolint:testifylint
assert.Equal(t, io.EOF, errors.Cause(testErr))
jsonError, err := json.Marshal(testErr)
require.NoError(t, err)
jsonEqual(t, `{"cause":{"error":"EOF"},"error":"IO error: EOF","foo":"baz","foo2":"bar2","stack":[]}`, string(jsonError))
err2, errE := errors.UnmarshalJSON(jsonError)
require.NoError(t, errE, "% -+#.1v", errE)
jsonError2, err := json.Marshal(err2)
require.NoError(t, err)
assert.Equal(t, string(jsonError), string(jsonError2))
ioDetailsBaseErr := &testStructDetails{"IO error", nil, new(sync.Mutex), nil}
errors.Details(ioDetailsBaseErr)["type"] = "io"
eofDetailsBaseErr := &testStructDetails{"IO error: EOF", nil, new(sync.Mutex), ioDetailsBaseErr}
errors.Details(eofDetailsBaseErr)["type"] = "eof"
testErr = errors.WrapWith(io.EOF, eofDetailsBaseErr)
errors.Details(testErr)["foo"] = "baz"
errors.Details(testErr)["foo2"] = "bar2"
assert.True(t, errors.Is(testErr, io.EOF))
assert.True(t, errors.Is(testErr, ioDetailsBaseErr))
assert.True(t, errors.Is(testErr, eofDetailsBaseErr))
assert.Equal(t, map[string]interface{}{"foo2": "bar2", "foo": "baz"}, errors.Details(testErr))
assert.Equal(t, map[string]interface{}{"foo2": "bar2", "foo": "baz"}, errors.AllDetails(testErr))
assert.Equal(t, map[string]interface{}{"foo2": "bar2", "foo": "baz"}, errors.AllDetails(copyThroughJSON(t, testErr)))
assert.Equal(t, []error{eofDetailsBaseErr, io.EOF}, errors.Unjoin(testErr))
assert.NoError(t, errors.Unwrap(testErr)) //nolint:testifylint
assert.Equal(t, io.EOF, errors.Cause(testErr))
jsonError, err = json.Marshal(testErr)
require.NoError(t, err)
jsonEqual(t, `{"cause":{"error":"EOF"},"error":"IO error: EOF","errors":[{"error":"IO error: EOF","type":"eof"}],"foo":"baz","foo2":"bar2","stack":[]}`, string(jsonError))
err2, errE = errors.UnmarshalJSON(jsonError)
require.NoError(t, errE, "% -+#.1v", errE)
jsonError2, err = json.Marshal(err2)
require.NoError(t, err)
assert.Equal(t, string(jsonError), string(jsonError2))
}
type testStructJSON struct{}
func (s testStructJSON) MarshalJSON() ([]byte, error) {
err := errors.New("error")
errors.Details(err)["foo"] = "bar"
return nil, err
}
func TestMarshalerError(t *testing.T) {
t.Parallel()
_, err := json.Marshal(testStructJSON{})
require.Error(t, err)
var marshalerError *json.MarshalerError
require.ErrorAs(t, err, &marshalerError)
var stackTrace stackTracer
require.ErrorAs(t, err, &stackTrace)
assert.Equal(t, "testStructJSON.MarshalJSON\n", fmt.Sprintf("%n", errors.StackFormatter{stackTrace.StackTrace()[0:1]}))
assert.Regexp(t, "^json: error calling MarshalJSON for type errors_test.testStructJSON: error\n"+
"foo=bar\n"+
"gitlab.com/tozd/go/errors_test.testStructJSON.MarshalJSON\n"+
"\t.+/errors_test.go:\\d+\n"+
"(.+\n\t.+:\\d+\n)+$", fmt.Sprintf("%#+v", errors.Formatter{Error: err}))
data, err2 := json.Marshal(errors.Formatter{Error: err})
require.NoError(t, err2)
jsonEqual(t, `{"error":"json: error calling MarshalJSON for type errors_test.testStructJSON: error","foo":"bar","stack":[]}`, string(data))
errWithStack := errors.WithStack(err)
assert.Equal(t, "testStructJSON.MarshalJSON\n", fmt.Sprintf("%n", errors.StackFormatter{errWithStack.StackTrace()[0:1]}))
assert.Regexp(t, "^json: error calling MarshalJSON for type errors_test.testStructJSON: error\n"+
"foo=bar\n"+
"gitlab.com/tozd/go/errors_test.testStructJSON.MarshalJSON\n"+
"\t.+/errors_test.go:\\d+\n"+
"(.+\n\t.+:\\d+\n)+$", fmt.Sprintf("%#+v", errWithStack))
data, err2 = json.Marshal(errWithStack)
require.NoError(t, err2)
jsonEqual(t, `{"error":"json: error calling MarshalJSON for type errors_test.testStructJSON: error","foo":"bar","stack":[]}`, string(data))
}
func getTestNewError() errors.E {
err := errors.New("error")
errors.Details(err)["foo"] = "bar"
return err
}
func TestFmtErrorf(t *testing.T) {
t.Parallel()
err := fmt.Errorf("test: %w", getTestNewError())
assert.Error(t, err)
var stackTrace stackTracer
require.ErrorAs(t, err, &stackTrace)
assert.Equal(t, "getTestNewError\n", fmt.Sprintf("%n", errors.StackFormatter{stackTrace.StackTrace()[0:1]}))
assert.Regexp(t, "^test: error\n"+
"foo=bar\n"+
"gitlab.com/tozd/go/errors_test.getTestNewError\n"+
"\t.+/errors_test.go:\\d+\n"+
"(.+\n\t.+:\\d+\n)+$", fmt.Sprintf("%#+v", errors.Formatter{Error: err}))
data, err2 := json.Marshal(errors.Formatter{Error: err})
require.NoError(t, err2)
jsonEqual(t, `{"error":"test: error","foo":"bar","stack":[]}`, string(data))
errWithStack := errors.WithStack(err)
assert.Equal(t, "getTestNewError\n", fmt.Sprintf("%n", errors.StackFormatter{errWithStack.StackTrace()[0:1]}))
assert.Regexp(t, "^test: error\n"+
"foo=bar\n"+
"gitlab.com/tozd/go/errors_test.getTestNewError\n"+
"\t.+/errors_test.go:\\d+\n"+
"(.+\n\t.+:\\d+\n)+$", fmt.Sprintf("%#+v", errWithStack))
data, err2 = json.Marshal(errWithStack)
require.NoError(t, err2)
jsonEqual(t, `{"error":"test: error","foo":"bar","stack":[]}`, string(data))
}
func TestUnjoin(t *testing.T) {
t.Parallel()
err := errors.New("1")
errors.Details(err)["level1"] = 1
err2 := errors.Wrap(err, "2")
errors.Details(err2)["level2"] = 2
right := errors.New("right")
joined := errors.Join(err2, right)
errors.Details(joined)["level3"] = 3
err3 := errors.WithDetails(joined, "level4", 4)
err4 := errors.Wrap(err3, "5")
errors.Details(err4)["level5"] = 5
assert.Equal(t, map[string]interface{}{"level1": 1}, errors.AllDetails(err))
assert.Equal(t, map[string]interface{}{"level2": 2}, errors.AllDetails(err2))
assert.Equal(t, map[string]interface{}{"level3": 3}, errors.AllDetails(joined))
assert.Equal(t, map[string]interface{}{"level3": 3, "level4": 4}, errors.AllDetails(err3))
assert.Equal(t, map[string]interface{}{"level5": 5}, errors.AllDetails(err4))
assert.Equal(t, err3, errors.Cause(err4))
assert.NoError(t, errors.Cause(err3)) //nolint:testifylint
assert.Equal(t, joined, errors.Unwrap(err3))
assert.NoError(t, errors.Unwrap(joined)) //nolint:testifylint
assert.Nil(t, errors.Unjoin(err4))
assert.Equal(t, []error{err2, right}, errors.Unjoin(err3))
assert.Equal(t, []error{err2, right}, errors.Unjoin(joined))
assert.Nil(t, errors.Unjoin(err2))
}