-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy patherrors_test.go
539 lines (466 loc) · 14.8 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
package merry
import (
"errors"
"fmt"
"reflect"
"runtime"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
v2 "github.com/ansel1/merry/v2"
)
func TestNew(t *testing.T) {
_, _, rl, _ := runtime.Caller(0)
err := New("bang")
if HTTPCode(err) != 500 {
t.Errorf("http code should have been 500, was %v", HTTPCode(err))
}
if err.Error() != "bang" {
t.Errorf("error message should have been bang, was %v", err.Error())
}
f, l := Location(err)
if !strings.Contains(f, "errors_test.go") {
t.Errorf("error message should have contained errors_test.go, was %s", f)
}
if l != rl+1 {
t.Errorf("error line should have been %d, was %d", rl+1, 8)
}
// accepts wrappers vararg
err = New("bang", v2.WithHTTPCode(45))
assert.Equal(t, 45, HTTPCode(err))
}
func TestErrorf(t *testing.T) {
_, _, rl, _ := runtime.Caller(0)
err := Errorf("chitty chitty %v %v", "bang", "bang")
if HTTPCode(err) != 500 {
t.Errorf("http code should have been 500, was %v", HTTPCode(err))
}
if err.Error() != "chitty chitty bang bang" {
t.Errorf("error message should have been chitty chitty bang bang, was %v", err.Error())
}
f, l := Location(err)
if !strings.Contains(f, "errors_test.go") {
t.Errorf("error message should have contained errors_test.go, was %s", f)
}
if l != rl+1 {
t.Errorf("error line should have been %d, was %d", rl+1, 8)
}
// accepts wrappers mixed in with the args
err = Errorf("something %s, something %s", "borrowed", v2.WithHTTPCode(55), "blue")
assert.EqualError(t, err, "something borrowed, something blue")
assert.Equal(t, 55, HTTPCode(err))
// Printing errors wrapped by fmt.Print should include stacktrace (https://github.com/ansel1/merry/issues/26)
s := fmt.Sprintf("%+v", Errorf("boom: %w", New("bang")))
assert.Contains(t, s, "errors_test.go")
}
func TestUserError(t *testing.T) {
_, _, rl, _ := runtime.Caller(0)
err := UserError("bang")
assert.Equal(t, "bang", UserMessage(err))
assert.Equal(t, "bang", Message(err))
_, l := Location(err)
assert.Equal(t, rl+1, l)
}
func TestUserErrorf(t *testing.T) {
_, _, rl, _ := runtime.Caller(0)
err := UserErrorf("bang %v", "bang")
assert.Equal(t, "bang bang", UserMessage(err))
assert.Equal(t, "bang bang", Message(err))
_, l := Location(err)
assert.Equal(t, rl+1, l)
}
func TestDetails(t *testing.T) {
var err error = New("bang")
deets := Details(err)
t.Log(deets)
lines := strings.Split(deets, "\n")
if lines[0] != "bang" {
t.Errorf("first line should have been bang: %v", lines[0])
}
if !strings.Contains(deets, Stacktrace(err)) {
t.Error("should have contained the error stacktrace")
}
err = WithUserMessage(err, "stay calm")
deets = Details(err)
t.Log(deets)
assert.Contains(t, deets, "User Message: stay calm")
// Allow nil error
assert.Empty(t, Details(nil))
}
func TestStacktrace(t *testing.T) {
_, _, rl, _ := runtime.Caller(0)
var err error = New("bang")
assert.NotEmpty(t, Stack(err))
s := Stacktrace(err)
t.Log(s)
lines := strings.Split(s, "\n")
assert.NotEmpty(t, lines)
assert.Equal(t, "github.com/ansel1/merry.TestStacktrace", lines[0])
assert.Contains(t, lines[1], fmt.Sprintf("errors_test.go:%d", rl+1))
// Allow nil error
assert.Empty(t, Stacktrace(nil))
}
func TestWrap(t *testing.T) {
err := errors.New("simple")
_, _, rl, _ := runtime.Caller(0)
wrapped := WrapSkipping(err, 0)
f, l := Location(wrapped)
if !strings.Contains(f, "errors_test.go") {
t.Errorf("error message should have contained errors_test.go, was %s", f)
}
if l != rl+1 {
t.Errorf("error line should have been %d, was %d", rl+1, l)
}
rich2 := WrapSkipping(wrapped, 0)
if wrapped != rich2 {
t.Error("rich and rich2 are not the same. Wrap should have been no-op if rich was already a RichError")
}
if !reflect.DeepEqual(Stack(wrapped), Stack(rich2)) {
t.Log(Details(rich2))
t.Error("wrap should have left the stacktrace alone if the original error already had a stack")
}
// wrapping nil -> nil
assert.Nil(t, Wrap(nil))
assert.Nil(t, WrapSkipping(nil, 1))
}
func TestHere(t *testing.T) {
parseError := New("Parse error")
invalidCharSet := WithMessage(parseError, "Invalid charset").WithHTTPCode(400)
invalidSyntax := parseError.WithMessage("Syntax error")
if !Is(invalidCharSet, parseError) {
t.Error("invalidCharSet should be a parseError")
}
_, _, rl, _ := runtime.Caller(0)
pe := Here(parseError)
_, l := Location(pe)
if l != rl+1 {
t.Errorf("Here should capture a new stack. Expected %d, got %d", rl+1, l)
}
if !Is(pe, parseError) {
t.Error("pe should be a parseError")
}
if Is(pe, invalidCharSet) {
t.Error("pe should not be an invalidCharSet")
}
if pe.Error() != "Parse error" {
t.Errorf("child error's message is wrong, expected: Parse error, got %v", pe.Error())
}
icse := Here(invalidCharSet)
if !Is(icse, parseError) {
t.Error("icse should be a parseError")
}
if !Is(icse, invalidCharSet) {
t.Error("icse should be an invalidCharSet")
}
if Is(icse, invalidSyntax) {
t.Error("icse should not be an invalidSyntax")
}
if icse.Error() != "Invalid charset" {
t.Errorf("child's message is wrong. Expected: Invalid charset, got: %v", icse.Error())
}
if HTTPCode(icse) != 400 {
t.Errorf("child's http code is wrong. Expected 400, got %v", HTTPCode(icse))
}
// nil -> nil
assert.Nil(t, Here(nil))
}
func TestHereSkipping(t *testing.T) {
var e error = New("boom")
f := func() error {
return HereSkipping(e, 1)
}
_, _, rl, _ := runtime.Caller(0)
e = f()
_, l := Location(e)
require.Equal(t, rl+1, l)
}
func TestUnwrap(t *testing.T) {
inner := errors.New("bing")
wrapper := WrapSkipping(inner, 0)
if Unwrap(wrapper) != inner {
t.Errorf("unwrapped error should have been the inner err, was %#v", inner)
}
doubleWrap := wrapper.WithMessage("blag")
if Unwrap(doubleWrap) != inner {
t.Errorf("unwrapped should recurse to inner, but got %#v", inner)
}
// nil -> nil
assert.Nil(t, Unwrap(nil))
}
func TestNilValues(t *testing.T) {
// Quirk of go
// http://devs.cloudimmunity.com/gotchas-and-common-mistakes-in-go-golang/index.html#nil_in_nil_in_vals
// an interface value isn't nil unless both the type *and* the value are nil
// make sure we aren't accidentally returning nil values but non-nil types
//
// type e struct{}
// var anE *e
// type f interface{}
// var anF f
// if anF != nil {
// t.Error("anF should have been nil here, because it doesn't have a concrete type yet")
// }
// anF = anE
// if anF == nil {
// t.Error("anF should have been not nil here, because it now has a concrete type")
// }
if WithMessage(WithHTTPCode(Wrap(nil), 400), "hey") != nil {
t.Error("by using interfaces in all the returns, this should have remained a true nil value")
}
}
func TestIs(t *testing.T) {
ParseError := errors.New("blag")
cp := Here(ParseError)
if !Is(cp, ParseError) {
t.Error("Is(child, parent) should be true")
}
if Is(ParseError, cp) {
t.Error("Is(parent, child) should not be true")
}
if !Is(ParseError, ParseError) {
t.Error("errors are always themselves")
}
if !Is(cp, cp) {
t.Error("should work when comparing rich error to itself")
}
if Is(Here(ParseError), cp) {
t.Error("Is(sibling, sibling) should not be true")
}
err2 := errors.New("blag")
if Is(ParseError, err2) {
t.Error("These should not have been equal")
}
if Is(Here(err2), cp) {
t.Error("these were not copies of the same error")
}
if Is(Here(err2), ParseError) {
t.Error("underlying errors were not equal")
}
nilTests := []struct {
arg1, arg2 error
expect bool
msg string
}{
{nil, New("t"), false, "nil is not any concrete error"},
{New("t"), nil, false, "no concrete error is nil"},
{nil, nil, true, "nil is nil"},
}
for _, tst := range nilTests {
assert.Equal(t, tst.expect, Is(tst.arg1, tst.arg2), tst.msg)
}
}
func TestHTTPCode(t *testing.T) {
basicErr := errors.New("blag")
if c := HTTPCode(basicErr); c != 500 {
t.Errorf("default code should be 500, was %d", c)
}
err := New("blug")
if c := HTTPCode(err); c != 500 {
t.Errorf("default code for rich errors should be 500, was %d", c)
}
errWCode := err.WithHTTPCode(404)
if c := HTTPCode(errWCode); c != 404 {
t.Errorf("the code should be set to 404, was %d", c)
}
if HTTPCode(err) != 500 {
t.Error("original error should not have been modified")
}
// nil -> nil
assert.Nil(t, WithHTTPCode(nil, 404))
assert.Equal(t, 200, HTTPCode(nil), "The code for nil is 200 (ok)")
}
func TestImplicitWrapping(t *testing.T) {
// WithXXX functions will implicitly wrap non-merry errors
// but if they do so, they should skip a frame, so the merry error's stack
// appears to start wherever the WithXXX function was called
_, _, rl, _ := runtime.Caller(0)
tests := []struct {
f func() error
fname string
}{
{fname: "WithHTTPCode", f: func() error { return WithHTTPCode(errors.New("bug"), 404) }},
{fname: "WithUserMessage", f: func() error { return WithUserMessage(errors.New("bug"), "asdf") }},
{fname: "WithUserMessages", f: func() error { return WithUserMessagef(errors.New("bug"), "asdf") }},
{fname: "WithMessage", f: func() error { return WithMessage(errors.New("bug"), "asdf") }},
{fname: "WithMessagef", f: func() error { return WithMessagef(errors.New("bug"), "asdf") }},
{fname: "WithValue", f: func() error { return WithValue(errors.New("bug"), "asdf", "asdf") }},
{fname: "Append", f: func() error { return Append(errors.New("bug"), "asdf") }},
{fname: "Appendf", f: func() error { return Appendf(errors.New("bug"), "asdf") }},
{fname: "Prepend", f: func() error { return Prepend(errors.New("bug"), "asdf") }},
{fname: "Prependf", f: func() error { return Prependf(errors.New("bug"), "asdf") }},
}
for i, test := range tests {
t.Log("Testing ", test.fname)
err := test.f()
f, l := Location(err)
assert.Contains(t, f, "errors_test.go", "error message should have contained errors_test.go")
assert.Equal(t, rl+5+i, l, "error line number was incorrect")
}
}
func TestWithMessage(t *testing.T) {
err1 := New("blug")
err2 := err1.WithMessage("blee")
err3 := err2.WithMessage("red")
assert.EqualError(t, err1, "blug")
assert.EqualError(t, err2, "blee", "should have overridden the underlying message")
assert.EqualError(t, err3, "red")
assert.Equal(t, Stack(err1), Stack(err2), "stack should not have been altered")
// nil -> nil
assert.Nil(t, WithMessage(nil, ""))
}
func TestWithMessagef(t *testing.T) {
err1 := New("blug")
err2 := err1.WithMessagef("super %v", "stew")
err3 := err1.WithMessagef("blue %v", "red")
assert.EqualError(t, err1, "blug")
assert.EqualError(t, err2, "super stew")
assert.EqualError(t, err3, "blue red")
assert.Equal(t, Stack(err1), Stack(err2), "stack should not have been altered")
// nil -> nil
assert.Nil(t, WithMessagef(nil, "%s", ""))
}
func TestMessage(t *testing.T) {
tests := []error{
errors.New("one"),
WithMessage(errors.New("blue"), "one"),
New("one"),
}
for _, test := range tests {
assert.Equal(t, "one", test.Error())
assert.Equal(t, "one", Message(test))
}
// when error is nil, return ""
assert.Empty(t, Message(nil))
}
func TestWithUserMessage(t *testing.T) {
fault := New("seg fault")
e := WithUserMessage(fault, "a glitch")
assert.Equal(t, "seg fault", e.Error())
assert.Equal(t, "a glitch", UserMessage(e))
e = WithUserMessagef(e, "not a %s deal", "huge")
assert.Equal(t, "not a huge deal", UserMessage(e))
}
func TestAppend(t *testing.T) {
blug := New("blug")
err := blug.Append("blog")
assert.Equal(t, "blug: blog", err.Error())
err = Append(err, "blig")
assert.Equal(t, "blug: blog: blig", err.Error())
err = blug.Appendf("%s", "blog")
assert.Equal(t, "blug: blog", err.Error())
err = Appendf(err, "%s", "blig")
assert.Equal(t, "blug: blog: blig", err.Error())
// nil -> nil
assert.Nil(t, Append(nil, ""))
assert.Nil(t, Appendf(nil, "%s", ""))
}
func TestPrepend(t *testing.T) {
blug := New("blug")
err := blug.Prepend("blog")
assert.Equal(t, err.Error(), "blog: blug")
err = Prepend(err, "blig")
assert.Equal(t, err.Error(), "blig: blog: blug")
err = blug.Prependf("%s", "blog")
assert.Equal(t, err.Error(), "blog: blug")
err = Prependf(err, "%s", "blig")
assert.Equal(t, err.Error(), "blig: blog: blug")
// nil -> nil
assert.Nil(t, Prepend(nil, ""))
assert.Nil(t, Prependf(nil, "%s", ""))
}
func TestLocation(t *testing.T) {
// nil -> nil
f, l := Location(nil)
assert.Equal(t, "", f)
assert.Equal(t, 0, l)
}
func TestSourceLine(t *testing.T) {
source := SourceLine(nil)
assert.Equal(t, source, "")
_, _, rl, _ := runtime.Caller(0)
var err error = New("foo")
source = SourceLine(err)
t.Log(source)
assert.NotEqual(t, source, "")
assert.Equal(t, fmt.Sprintf("github.com/ansel1/merry.TestSourceLine (errors_test.go:%v)", rl+1), source)
}
func TestValue(t *testing.T) {
// nil -> nil
assert.Nil(t, WithValue(nil, "", ""))
assert.Nil(t, Value(nil, ""))
}
func TestValues(t *testing.T) {
// nil -> nil
values := Values(nil)
assert.Nil(t, values)
var e error
e = New("bad stuff")
e = WithValue(e, "key1", "val1")
e = WithValue(e, "key2", "val2")
values = Values(e)
assert.NotNil(t, values)
assert.Equal(t, values["key1"], "val1")
assert.Equal(t, values["key2"], "val2")
// make sure the last value attached is returned
e = WithValue(e, "key3", "val3")
e = WithValue(e, "key3", "val4")
values = Values(e)
assert.Equal(t, values["key3"], "val4")
}
func TestStackCaptureEnabled(t *testing.T) {
// on by default
assert.True(t, StackCaptureEnabled())
SetStackCaptureEnabled(false)
assert.False(t, StackCaptureEnabled())
e := New("yikes")
assert.Empty(t, Stack(e))
// let's just make sure none of the print functions bomb when there's no stack
assert.Empty(t, SourceLine(e))
f, l := Location(e)
assert.Empty(t, f)
assert.Equal(t, 0, l)
assert.Empty(t, Stacktrace(e))
assert.NotPanics(t, func() { Details(e) })
// turn it back on
SetStackCaptureEnabled(true)
assert.True(t, StackCaptureEnabled())
e = New("mommy")
assert.NotEmpty(t, Stack(e))
}
func TestCause(t *testing.T) {
e1 := New("low level error")
e2 := New("high level error")
e3 := WithCause(e2, e1)
e4 := New("top level error")
e5 := e4.WithCause(e3)
assert.True(t, Is(e3, e1))
assert.True(t, Is(e3, e2))
assert.Equal(t, e1, Cause(e3))
assert.Equal(t, e1, e3.(Error).Cause())
assert.Nil(t, Cause(e2))
assert.Nil(t, Cause(e1))
assert.Nil(t, e1.(Error).Cause())
assert.Nil(t, e2.(Error).Cause())
assert.Equal(t, fmt.Sprintf("%v", e3), e2.Error()+": "+e1.Error())
assert.True(t, Is(e5, e4))
assert.True(t, Is(e5, e3))
assert.True(t, Is(e5, e2))
assert.True(t, Is(e5, e1))
assert.Equal(t, e3, Cause(e5))
assert.NotEqual(t, e3, RootCause(e5))
assert.Equal(t, e1, RootCause(e5))
// ensure cause message isn't double appended
assert.Equal(t, "red: high level error: low level error", fmt.Sprintf("%v", Prepend(e3, "red")))
}
func BenchmarkNew_withStackCapture(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = New("boom")
}
}
func BenchmarkNew_withoutStackCapture(b *testing.B) {
SetStackCaptureEnabled(false)
for i := 0; i < b.N; i++ {
_ = New("boom")
}
}