forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 39
/
errors_test.go
450 lines (399 loc) · 9.98 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
package errors
import (
"errors"
"fmt"
"io"
"reflect"
"strconv"
"testing"
"github.com/stretchr/testify/require"
)
func TestNew(t *testing.T) {
tests := []struct {
err string
want error
}{
{"", fmt.Errorf("")},
{"foo", fmt.Errorf("foo")},
{"foo", New("foo")},
{"string with format specifiers: %v", errors.New("string with format specifiers: %v")},
}
for _, tt := range tests {
got := New(tt.err)
if got.Error() != tt.want.Error() {
t.Errorf("New.Error(): got: %q, want %q", got, tt.want)
}
}
}
func TestWrapNil(t *testing.T) {
got := Annotate(nil, "no error")
if got != nil {
t.Errorf("Wrap(nil, \"no error\"): got %#v, expected nil", got)
}
}
func TestWrap(t *testing.T) {
tests := []struct {
err error
message string
want string
}{
{io.EOF, "read error", "read error: EOF"},
{Annotate(io.EOF, "read error"), "client error", "client error: read error: EOF"},
}
for _, tt := range tests {
got := Annotate(tt.err, tt.message).Error()
if got != tt.want {
t.Errorf("Wrap(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
}
}
}
type nilError struct{}
func (nilError) Error() string { return "nil error" }
func TestCause(t *testing.T) {
x := New("error")
tests := []struct {
err error
want error
}{{
// nil error is nil
err: nil,
want: nil,
}, {
// explicit nil error is nil
err: (error)(nil),
want: nil,
}, {
// typed nil is nil
err: (*nilError)(nil),
want: (*nilError)(nil),
}, {
// uncaused error is unaffected
err: io.EOF,
want: io.EOF,
}, {
// caused error returns cause
err: Annotate(io.EOF, "ignored"),
want: io.EOF,
}, {
err: x, // return from errors.New
want: x,
}, {
WithMessage(nil, "whoops"),
nil,
}, {
WithMessage(io.EOF, "whoops"),
io.EOF,
}, {
WithStack(nil),
nil,
}, {
WithStack(io.EOF),
io.EOF,
}, {
AddStack(nil),
nil,
}, {
AddStack(io.EOF),
io.EOF,
}}
for i, tt := range tests {
got := Cause(tt.err)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("test %d: got %#v, want %#v", i+1, got, tt.want)
}
}
}
func TestWrapfNil(t *testing.T) {
got := Annotate(nil, "no error")
if got != nil {
t.Errorf("Wrapf(nil, \"no error\"): got %#v, expected nil", got)
}
}
func TestWrapf(t *testing.T) {
tests := []struct {
err error
message string
want string
}{
{io.EOF, "read error", "read error: EOF"},
{Annotatef(io.EOF, "read error without format specifiers"), "client error", "client error: read error without format specifiers: EOF"},
{Annotatef(io.EOF, "read error with %d format specifier", 1), "client error", "client error: read error with 1 format specifier: EOF"},
}
for _, tt := range tests {
got := Annotatef(tt.err, tt.message).Error()
if got != tt.want {
t.Errorf("Wrapf(%v, %q): got: %v, want %v", tt.err, tt.message, got, tt.want)
}
}
}
func TestErrorf(t *testing.T) {
tests := []struct {
err error
want string
}{
{Errorf("read error without format specifiers"), "read error without format specifiers"},
{Errorf("read error with %d format specifier", 1), "read error with 1 format specifier"},
}
for _, tt := range tests {
got := tt.err.Error()
if got != tt.want {
t.Errorf("Errorf(%v): got: %q, want %q", tt.err, got, tt.want)
}
}
}
func TestWithStackNil(t *testing.T) {
got := WithStack(nil)
if got != nil {
t.Errorf("WithStack(nil): got %#v, expected nil", got)
}
got = AddStack(nil)
if got != nil {
t.Errorf("AddStack(nil): got %#v, expected nil", got)
}
}
func TestWithStack(t *testing.T) {
tests := []struct {
err error
want string
}{
{io.EOF, "EOF"},
{WithStack(io.EOF), "EOF"},
}
for _, tt := range tests {
got := WithStack(tt.err).Error()
if got != tt.want {
t.Errorf("WithStack(%v): got: %v, want %v", tt.err, got, tt.want)
}
}
}
func TestAddStack(t *testing.T) {
tests := []struct {
err error
want string
}{
{io.EOF, "EOF"},
{AddStack(io.EOF), "EOF"},
}
for _, tt := range tests {
got := AddStack(tt.err).Error()
if got != tt.want {
t.Errorf("AddStack(%v): got: %v, want %v", tt.err, got, tt.want)
}
}
}
func TestGetStackTracer(t *testing.T) {
orig := io.EOF
if GetStackTracer(orig) != nil {
t.Errorf("GetStackTracer: got: %v, want %v", GetStackTracer(orig), nil)
}
stacked := AddStack(orig)
if GetStackTracer(stacked).(error) != stacked {
t.Errorf("GetStackTracer(stacked): got: %v, want %v", GetStackTracer(stacked), stacked)
}
final := AddStack(stacked)
if GetStackTracer(final).(error) != stacked {
t.Errorf("GetStackTracer(final): got: %v, want %v", GetStackTracer(final), stacked)
}
}
func TestAddStackDedup(t *testing.T) {
stacked := WithStack(io.EOF)
err := AddStack(stacked)
if err != stacked {
t.Errorf("AddStack: got: %+v, want %+v", err, stacked)
}
err = WithStack(stacked)
if err == stacked {
t.Errorf("WithStack: got: %v, don't want %v", err, stacked)
}
}
func TestWithMessageNil(t *testing.T) {
got := WithMessage(nil, "no error")
if got != nil {
t.Errorf("WithMessage(nil, \"no error\"): got %#v, expected nil", got)
}
}
func TestWithMessage(t *testing.T) {
tests := []struct {
err error
message string
want string
}{
{io.EOF, "read error", "read error: EOF"},
{WithMessage(io.EOF, "read error"), "client error", "client error: read error: EOF"},
}
for _, tt := range tests {
got := WithMessage(tt.err, tt.message).Error()
if got != tt.want {
t.Errorf("WithMessage(%v, %q): got: %q, want %q", tt.err, tt.message, got, tt.want)
}
}
}
// errors.New, etc values are not expected to be compared by value
// but the change in errors#27 made them incomparable. 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) {
vals := []error{
nil,
io.EOF,
errors.New("EOF"),
New("EOF"),
Errorf("EOF"),
Annotate(io.EOF, "EOF"),
Annotatef(io.EOF, "EOF%d", 2),
WithMessage(nil, "whoops"),
WithMessage(io.EOF, "whoops"),
WithStack(io.EOF),
WithStack(nil),
AddStack(io.EOF),
AddStack(nil),
}
for i := range vals {
for j := range vals {
_ = vals[i] == vals[j] // mustn't panic
}
}
}
func TestFind(t *testing.T) {
eNew := errors.New("error")
wrapped := Annotate(nilError{}, "nil")
tests := []struct {
err error
finder func(error) bool
found error
}{
{io.EOF, func(_ error) bool { return true }, io.EOF},
{io.EOF, func(_ error) bool { return false }, nil},
{io.EOF, func(err error) bool { return err == io.EOF }, io.EOF},
{io.EOF, func(err error) bool { return err != io.EOF }, nil},
{eNew, func(err error) bool { return true }, eNew},
{eNew, func(err error) bool { return false }, nil},
{nilError{}, func(err error) bool { return true }, nilError{}},
{nilError{}, func(err error) bool { return false }, nil},
{nilError{}, func(err error) bool { _, ok := err.(nilError); return ok }, nilError{}},
{wrapped, func(err error) bool { return true }, wrapped},
{wrapped, func(err error) bool { return false }, nil},
{wrapped, func(err error) bool { _, ok := err.(nilError); return ok }, nilError{}},
}
for _, tt := range tests {
got := Find(tt.err, tt.finder)
if got != tt.found {
t.Errorf("WithMessage(%v): got: %q, want %q", tt.err, got, tt.found)
}
}
}
type errWalkTest struct {
cause error
sub []error
v int
}
func (e *errWalkTest) Error() string {
return strconv.Itoa(e.v)
}
func (e *errWalkTest) Cause() error {
return e.cause
}
func (e *errWalkTest) Errors() []error {
return e.sub
}
func testFind(err error, v int) bool {
return WalkDeep(err, func(err error) bool {
e := err.(*errWalkTest)
return e.v == v
})
}
func TestWalkDeep(t *testing.T) {
err := &errWalkTest{
sub: []error{
&errWalkTest{
v: 10,
cause: &errWalkTest{v: 11},
},
&errWalkTest{
v: 20,
cause: &errWalkTest{v: 21, cause: &errWalkTest{v: 22}},
},
&errWalkTest{
v: 30,
cause: &errWalkTest{v: 31},
},
},
}
if !testFind(err, 11) {
t.Errorf("not found in first cause chain")
}
if !testFind(err, 22) {
t.Errorf("not found in siblings")
}
if testFind(err, 32) {
t.Errorf("found not exists")
}
}
type fooError int
func (fooError) Error() string {
return "foo"
}
func TestWorkWithStdErrors(t *testing.T) {
e1 := fooError(100)
e2 := Normalize("e2", RFCCodeText("e2"))
e3 := Normalize("e3", RFCCodeText("e3"))
e21 := e2.Wrap(e1)
e31 := e3.Wrap(e1)
e32 := e3.Wrap(e2)
e321 := e3.Wrap(e21)
unwrapTbl := []struct {
x *Error // x.Unwrap() == y
y error
}{{e2, nil}, {e3, nil}, {e21, e1}, {e31, e1}, {e32, e2}, {e321, e21}}
for _, c := range unwrapTbl {
if c.x.Unwrap() != c.y {
t.Errorf("`%s`.Unwrap() != `%s`", c.x, c.y)
}
}
isTbl := []struct {
x, y error // errors.Is(x, y) == b
b bool
}{
{e1, e1, true}, {e2, e1, false}, {e3, e1, false}, {e21, e1, true}, {e321, e1, true},
{e1, e2, false}, {e2, e2, true}, {e3, e2, false}, {e21, e2, true}, {e31, e2, false}, {e321, e2, true},
{e2, e21, true}, {e21, e21, true}, {e31, e21, false}, {e321, e21, true},
{e321, e321, true}, {e3, e321, true}, {e21, e321, false},
}
for _, c := range isTbl {
if c.b && !errors.Is(c.x, c.y) {
t.Errorf("`%s` is not `%s`", c.x, c.y)
}
if !c.b && errors.Is(c.x, c.y) {
t.Errorf("`%s` is `%s`", c.x, c.y)
}
}
var e1x fooError
if ok := errors.As(e21, &e1x); !ok {
t.Error("e21 cannot convert to e1")
}
if int(e1x) != 100 {
t.Error("e1x is not 100")
}
var e2x *Error
if ok := errors.As(e21, &e2x); !ok {
t.Error("e21 cannot convert to e2")
}
if e2x.ID() != "e2" {
t.Error("err is not e2")
}
e3x := e3.Wrap(e1)
if ok := errors.As(e21, &e3x); !ok {
t.Error("e21 cannot convert to e3")
}
if e3x.ID() != "e2" {
t.Error("err is not e2")
}
}
func TestHasTrace(t *testing.T) {
targetErr := Normalize("test err")
require.False(t, HasStack(targetErr))
require.False(t, HasStack(targetErr.FastGen("fast gen")))
require.False(t, HasStack(targetErr.FastGenByArgs("fast gen arg")))
require.True(t, HasStack(Trace(targetErr.FastGen("fast gen"))))
require.True(t, HasStack(targetErr.GenWithStack("gen")))
}