-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathxerrs_test.go
518 lines (462 loc) · 11.3 KB
/
xerrs_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
package xerrs
import (
"errors"
"strings"
"testing"
)
// get the last part of the path
func getLastPathPart(str string) string {
parts := strings.Split(str, "/")
if len(parts) == 0 {
return ""
}
return parts[len(parts)-1]
}
// Remove the path to the files in stack so that Testing would not be path dependent
func transformStack(stack []StackLocation) []StackLocation {
for i := range stack {
stack[i].Function = getLastPathPart(stack[i].Function)
stack[i].File = getLastPathPart(stack[i].File)
}
return stack
}
func TestNew(t *testing.T) {
in := New("ABC")
if x, ok := in.(*xerr); ok {
if x.cause.Error() != "ABC" {
t.Errorf("wrong error cause: want=%v got=%v", "ABC", x.cause.Error())
}
if x.mask != nil {
t.Errorf("expected nil mask, got=%v", x.mask)
}
if in.Error() != "ABC" {
t.Errorf("wrong error message: want=%v got=%v", "ABC", in.Error())
}
if len(x.stack) != 3 {
t.Errorf("wrong stack length: want=%v got=%v", 3, len(x.stack))
}
}
}
func TestErrorf(t *testing.T) {
in := Errorf("some error %d %v", 1, "HELLO")
if x, ok := in.(*xerr); ok {
if x.cause.Error() != "some error 1 HELLO" {
t.Errorf("wrong error cause: want=%v got=%v", "some error 1 HELLO", x.cause.Error())
}
if x.mask != nil {
t.Errorf("expected nil mask: got=%v", x.mask)
}
if in.Error() != "some error 1 HELLO" {
t.Errorf("wrong error message: want=%v got=%v", "some error 1 HELLO", in.Error())
}
if len(x.stack) != 3 {
t.Errorf("wrong stack length: want=%v got=%v", 3, len(x.stack))
}
}
}
func TestExtend(t *testing.T) {
in := Extend(errors.New("ABC"))
if x, ok := in.(*xerr); ok {
if x.cause.Error() != "ABC" {
t.Errorf("wrong error cause: want=%v got=%v", "ABC", x.cause.Error())
}
if x.mask != nil {
t.Errorf("expected nil mask, got=%v", x.mask)
}
if in.Error() != "ABC" {
t.Errorf("wrong error message: want=%v got=%v", "ABC", in.Error())
}
if len(x.stack) != 3 {
t.Errorf("wrong stack length: want=%v got=%v", 3, len(x.stack))
}
}
}
func TestMask(t *testing.T) {
err := Mask(nil, errors.New("ABC"))
if err != nil {
t.Errorf("expected nil error: got=%v", err)
}
err = Mask(errors.New("ABC"), nil)
_, ok := err.(*xerr)
if err.Error() != "ABC" {
t.Errorf("wrong error message: want=%v got=%v", "ABC", err.Error())
}
if ok != true {
t.Errorf("expected err to be xerr")
}
err = Mask(errors.New("ABC"), errors.New("XYZ"))
_, ok = err.(*xerr)
if err.Error() != "XYZ" {
t.Errorf("wrong error message: want=%v got=%v", "XYZ", err.Error())
}
if ok != true {
t.Errorf("expected err to be xerr")
}
intial := Extend(errors.New("ABC"))
err = Mask(intial, errors.New("XYZ"))
_, ok = err.(*xerr)
if err.Error() != "XYZ" {
t.Errorf("wrong error message: want=%v got=%v", "XYZ", err.Error())
}
if ok != true {
t.Errorf("expected err to be xerr")
}
intial = Mask(errors.New("ABC"), errors.New("001"))
err = Mask(intial, errors.New("XYZ"))
_, ok = err.(*xerr)
if err.Error() != "XYZ" {
t.Errorf("wrong error message: want=%v got=%v", "XYZ", err.Error())
}
if ok != true {
t.Errorf("expected err to be xerr")
}
intial = Mask(errors.New("ABC"), errors.New("001"))
err = Mask(intial, nil)
_, ok = err.(*xerr)
if err.Error() != "ABC" {
t.Errorf("wrong error message: want=%v got=%v", "ABC", err.Error())
}
if ok != true {
t.Errorf("expected err to be xerr")
}
}
func TestData(t *testing.T) {
t.Run("SetGet", func(t *testing.T) {
err := New("test")
SetData(err, "SOME_DATA", "test")
v, ok := GetData(err, "SOME_DATA")
if !ok {
t.Error("expected data")
}
if _, ok := v.(string); !ok {
t.Errorf("expected string, got %T", v)
}
})
t.Run("nil error nil data", func(t *testing.T) {
_, ok := GetData(nil, "test")
if ok {
t.Error("expected false")
}
})
t.Run("nil data", func(t *testing.T) {
err := New("test")
if _, ok := GetData(err, "test"); ok {
t.Error("expected false")
}
})
t.Run("nested error data", func(t *testing.T) {
a := New("a")
SetData(a, "foo", "bar")
b := Wrap(a, "b")
foo, ok := GetData(b, "foo")
if !ok {
t.Error("failed to get data for \"foo\"")
}
msg, ok := foo.(string)
if !ok {
t.Error("data for \"foo\" should be a string")
}
if msg != "bar" {
t.Errorf("wanted foo=%q, got foo=%q", "bar", msg)
}
})
}
func TestDetails(t *testing.T) {
type TestCase struct {
Description string
InputError error
InputMask error
InputMaxStack int
OutputPrefix string
}
testCases := []TestCase{
TestCase{
Description: "nil error",
InputError: nil,
InputMask: errors.New("MASK"),
InputMaxStack: 100,
OutputPrefix: ``,
},
TestCase{
Description: "basic",
InputError: errors.New("ERROR"),
InputMask: errors.New("MASK"),
InputMaxStack: 100,
OutputPrefix: `
[ERROR] ERROR
[MASK ERROR] MASK
[STACK]:`,
},
TestCase{
Description: "mask is the same as error",
InputError: errors.New("ERROR"),
InputMask: errors.New("ERROR"),
InputMaxStack: 100,
OutputPrefix: `
[ERROR] ERROR
[STACK]:`,
},
TestCase{
Description: "mask is nil",
InputError: errors.New("ERROR"),
InputMask: nil,
InputMaxStack: 100,
OutputPrefix: `
[ERROR] ERROR
[STACK]:`,
},
TestCase{
Description: "fewer stack lines",
InputError: errors.New("ERROR"),
InputMask: errors.New("MASK"),
InputMaxStack: 4,
OutputPrefix: `
[ERROR] ERROR
[MASK ERROR] MASK
[STACK]:`,
},
}
for _, testCase := range testCases {
var err error
if testCase.InputMask != nil {
err = Mask(testCase.InputError, testCase.InputMask)
} else {
err = Extend(testCase.InputError)
}
if x, ok := err.(*xerr); ok {
x.stack = transformStack(x.stack)
}
if !strings.HasPrefix(Details(err, testCase.InputMaxStack), testCase.OutputPrefix) {
t.Errorf("wrong output prefix: wanted prefix=%v got=%v", testCase.OutputPrefix, Details(err, testCase.InputMaxStack))
}
}
if Details(errors.New("ABC"), 5) != "ABC" {
t.Errorf("running stack on the basic go error failed: want=%v got=%v", "ABC", Details(errors.New("ABC"), 5))
}
}
func TestIsEqual(t *testing.T) {
type TestCase struct {
Description string
InputErr1 error
InputErr2 error
Output bool
}
testCases := []TestCase{
TestCase{
Description: "both errors are nil",
InputErr1: nil,
InputErr2: nil,
Output: true,
},
TestCase{
Description: "one errors is nil",
InputErr1: errors.New("ABC"),
InputErr2: nil,
Output: false,
},
TestCase{
Description: "both errors are nil",
InputErr1: nil,
InputErr2: errors.New("ABC"),
Output: false,
},
TestCase{
Description: "both errors are basic ones. equal",
InputErr1: errors.New("ABC"),
InputErr2: errors.New("ABC"),
Output: true,
},
TestCase{
Description: "both errors are basic ones. not equal",
InputErr1: errors.New("ABC"),
InputErr2: errors.New("XYZ"),
Output: false,
},
TestCase{
Description: "one errors is xerr another is basic ones. equal",
InputErr1: Extend(errors.New("ABC")),
InputErr2: errors.New("ABC"),
Output: true,
},
TestCase{
Description: "one errors is xerr another is basic ones. not equal",
InputErr1: Extend(errors.New("XYZ")),
InputErr2: errors.New("ABC"),
Output: false,
},
TestCase{
Description: "one errors is xerr another is basic ones. different mask. equal",
InputErr1: Mask(errors.New("ABC"), errors.New("XYZ")),
InputErr2: errors.New("ABC"),
Output: true,
},
TestCase{
Description: "both errors are xerr. equal",
InputErr1: Extend(errors.New("ABC")),
InputErr2: Extend(errors.New("ABC")),
Output: true,
},
TestCase{
Description: "both errors are xerr. not equal",
InputErr1: Extend(errors.New("XYZ")),
InputErr2: Extend(errors.New("ABC")),
Output: false,
},
}
for _, testCase := range testCases {
t.Run(testCase.Description, func(t *testing.T) {
if IsEqual(testCase.InputErr1, testCase.InputErr2) != testCase.Output {
t.Errorf("wrong output: want=%v got=%v", testCase.Output, IsEqual(testCase.InputErr1, testCase.InputErr2))
}
})
}
}
func TestWrap(t *testing.T) {
for _, test := range []struct {
in error
msg string
wantMsg string
isNil bool
}{
{
in: nil,
msg: "nothing",
wantMsg: "",
isNil: true,
},
{
in: New("i/o error"),
msg: "read",
wantMsg: "read: i/o error",
isNil: false,
},
} {
err := Wrap(test.in, test.msg)
if err == nil && !test.isNil {
t.Error("expected non-nil error")
} else if err != nil && test.isNil {
t.Errorf("expected nil error, got=%v", err)
}
if err != nil {
if got := err.Error(); test.wantMsg != got {
t.Errorf("wrong error message: want=%v got=%v", test.wantMsg, got)
}
}
}
}
func TestWrapf(t *testing.T) {
for _, test := range []struct {
in error
format string
args []interface{}
wantMsg string
isNil bool
}{
{
in: nil,
format: "nothing",
args: nil,
wantMsg: "",
isNil: true,
},
{
in: New("i/o error"),
format: "read %q",
args: []interface{}{"config.yaml"},
wantMsg: "read \"config.yaml\": i/o error",
isNil: false,
},
} {
err := Wrapf(test.in, test.format, test.args...)
if err == nil && !test.isNil {
t.Error("expected non-nil error")
} else if err != nil && test.isNil {
t.Errorf("expected nil error, got=%v", err)
}
if err != nil {
if got := err.Error(); test.wantMsg != got {
t.Errorf("wrong error message: want=%v got=%v", test.wantMsg, got)
}
}
}
}
func BenchmarkWrap(b *testing.B) {
b.Run("chain", func(b *testing.B) {
err := New("test")
for i := 0; i < b.N; i++ {
wrapped := &xerr{
stack: getStack(stackFunctionOffset),
cause: err,
msg: "prepend",
}
// inlined (*xerr).Error
if wrapped.mask != nil {
_ = wrapped.mask.Error()
continue
}
if wrapped.msg != "" {
_ = wrapped.msg + ": " + wrapped.cause.Error()
continue
}
_ = wrapped.cause.Error()
}
})
b.Run("prepend", func(b *testing.B) {
err := New("test")
for i := 0; i < b.N; i++ {
wrapped := &xerr{
stack: getStack(stackFunctionOffset),
cause: err,
msg: "prepend" + ": " + err.Error(),
}
// inlined (*xerr).Error
if wrapped.mask != nil {
_ = wrapped.mask.Error()
continue
}
if wrapped.msg != "" {
_ = wrapped.msg + ": " + wrapped.cause.Error()
continue
}
_ = wrapped.cause.Error()
}
})
b.Run("prepend with typecheck", func(b *testing.B) {
for name, err := range map[string]error{
"error": errors.New("test"),
"xerr": New("test"),
} {
b.Run(name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
if xe, ok := err.(*xerr); ok {
xe.msg = "prepend" + ": " + xe.msg
// modified (*xerr).Error
if xe.mask != nil {
_ = xe.mask.Error()
continue
}
if xe.msg != "" {
continue
}
_ = xe.cause.Error()
} else {
wrapped := &xerr{
stack: getStack(stackFunctionOffset),
cause: err,
msg: "prepend" + ": " + err.Error(),
}
// modified (*xerr).Error
if wrapped.mask != nil {
_ = wrapped.mask.Error()
continue
}
if wrapped.msg != "" {
continue
}
_ = wrapped.cause.Error()
}
}
})
}
})
}