-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_test.go
584 lines (559 loc) · 19 KB
/
format_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
package eris_test
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"regexp"
"testing"
"github.com/risingwavelabs/eris"
)
func TestUnpack(t *testing.T) {
tests := map[string]struct {
cause error
input []string
output eris.UnpackedError
}{
"nil error": {
cause: nil,
input: nil,
output: eris.UnpackedError{},
},
"nil root error": {
cause: nil,
input: []string{"additional context"},
output: eris.UnpackedError{},
},
"standard error wrapping with internal root cause (eris.New)": {
cause: eris.New("root error").WithCode(eris.CodeUnknown),
input: []string{"additional context", "even more context"},
output: eris.UnpackedError{
ErrRoot: eris.ErrRoot{
Msg: "root error",
},
ErrChain: []eris.ErrLink{
{
Msg: "additional context",
},
{
Msg: "even more context",
},
},
},
},
"standard error wrapping with external root cause (errors.New)": {
cause: errors.New("external error"),
input: []string{"additional context", "even more context"},
output: eris.UnpackedError{
ErrExternal: errors.New("external error"),
ErrRoot: eris.ErrRoot{
Msg: "additional context",
},
ErrChain: []eris.ErrLink{
{
Msg: "even more context",
},
},
},
},
"no error wrapping with internal root cause (eris.Errorf)": {
cause: eris.Errorf("%v", "root error").WithCode(eris.CodeUnknown),
output: eris.UnpackedError{
ErrRoot: eris.ErrRoot{
Msg: "root error",
},
},
},
"no error wrapping with external root cause (errors.New)": {
cause: errors.New("external error"),
output: eris.UnpackedError{
ErrExternal: errors.New("external error"),
},
},
}
for desc, tt := range tests {
t.Run(desc, func(t *testing.T) {
err := setupTestCase(false, tt.cause, tt.input)
if got := eris.Unpack(err); got.ErrChain != nil && tt.output.ErrChain != nil && !errChainsEqual(got.ErrChain, tt.output.ErrChain) {
t.Errorf("Unpack() ErrorChain = %v, want %v", got.ErrChain, tt.output.ErrChain)
}
if got := eris.Unpack(err); !reflect.DeepEqual(got.ErrRoot.Msg, tt.output.ErrRoot.Msg) {
t.Errorf("Unpack() ErrorRoot = %v, want %v", got.ErrRoot.Msg, tt.output.ErrRoot.Msg)
}
})
}
}
func errChainsEqual(a []eris.ErrLink, b []eris.ErrLink) bool {
// If one is nil, the other must also be nil.
if (a == nil) != (b == nil) {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i].Msg != b[i].Msg {
return false
}
}
return true
}
func TestFormatStr(t *testing.T) {
tests := map[string]struct {
input error
output string
}{
"basic root error": {
input: eris.New("root error").WithCode(eris.CodeUnknown),
output: "code(unknown) root error",
},
"basic wrapped error": {
input: eris.WithCode(eris.Wrap(
eris.WithCode(eris.Wrap(
eris.WithCode(eris.New("root error"), eris.CodeAlreadyExists),
"additional context"), eris.CodeInvalidArgument),
"even more context"), eris.CodeInvalidArgument),
output: "code(invalid argument) even more context: code(invalid argument) additional context: code(already exists) root error",
},
"external wrapped error": {
input: eris.WithCode(eris.Wrap(errors.New("external error"), "additional context"), eris.CodeUnknown),
output: "code(unknown) additional context: external error",
},
"external error": {
input: errors.New("external error"),
output: "code(unknown) external error",
},
// This is the expected behavior, since this error does not hold any information
"empty error": {
input: eris.New("").WithCode(eris.CodeUnknown),
output: "",
},
"empty wrapped external error": {
input: eris.WithCode(eris.Wrap(errors.New(""), "additional context"), eris.CodeUnknown),
output: "code(unknown) additional context: ",
},
"empty wrapped error": {
input: eris.WithCode(eris.Wrap(
eris.WithCode(eris.New(""), eris.CodeUnknown),
"additional context"), eris.CodeUnknown),
output: "code(unknown) additional context: ",
},
}
for desc, tt := range tests {
// without trace
t.Run(desc, func(t *testing.T) {
if got := eris.ToString(tt.input, false); !reflect.DeepEqual(got, tt.output) {
t.Errorf("ToString() got\n'%v'\nwant\n'%v'", got, tt.output)
}
})
}
}
func TestInvertedFormatStr(t *testing.T) {
tests := map[string]struct {
input error
output string
}{
"basic wrapped error": {
input: eris.WithCode(eris.Wrap(
eris.WithCode(eris.Wrap(
eris.WithCode(eris.New("root error"), eris.CodeUnknown),
"additional context"), eris.CodeUnknown),
"even more context"), eris.CodeUnknown),
output: "code(unknown) root error: code(unknown) additional context: code(unknown) even more context",
},
"external wrapped error": {
input: eris.WithCode(eris.Wrap(errors.New("external error"), "additional context"), eris.CodeUnknown),
output: "external error: code(unknown) additional context",
},
"external error": {
input: errors.New("external error"),
output: "external error code(unknown) ",
},
"empty wrapped external error": {
input: eris.WithCode(eris.Wrap(errors.New("some err"), "additional context"), eris.CodeUnknown),
output: "some err: code(unknown) additional context",
},
"empty wrapped error": {
input: eris.WithCode(eris.Wrap(
eris.WithCode(eris.New("err"), eris.CodeUnknown),
"additional context"), eris.CodeUnknown),
output: "code(unknown) err: code(unknown) additional context",
},
}
for desc, tt := range tests {
// without trace
t.Run(desc, func(t *testing.T) {
format := eris.NewDefaultStringFormat(eris.FormatOptions{
InvertOutput: true,
WithExternal: true,
})
if got := eris.ToCustomString(tt.input, format); !reflect.DeepEqual(got, tt.output) {
t.Errorf("ToString() got\n'%v'\nwant\n'%v'", got, tt.output)
}
})
}
}
func TestFormatJSONwithKVs(t *testing.T) {
nonSerializableObj := struct {
a string
b int
}{
a: "a",
b: 1,
}
serializableObj := struct {
A string `json:"a"`
B int `json:"b"`
}{
A: "aVal",
B: 1,
}
tests := map[string]struct {
input error
output string
}{
"basic root error + simple kvs": {
input: eris.New("root error").WithCode(eris.CodeCanceled).WithProperty("key", "value"),
output: `{"root":{"KVs":{"key":"value"},"code":"canceled","message":"root error"}}`,
},
"basic wrapped error + kvs with objects": {
input: eris.Wrap(
eris.With(eris.Wrap(
eris.With(eris.New("root error"),
eris.Codes(eris.CodeNotFound), eris.KVs("obj", nonSerializableObj),
),
"additional context"), eris.Codes(eris.CodeAlreadyExists), eris.KVs("obj", serializableObj),
),
"outer error"),
output: `{"root":{"KVs":{"obj":{}},"code":"not found","message":"root error"},"wrap":[{"code":"internal","message":"outer error"},{"KVs":{"obj":{"a":"aVal","b":1}},"code":"already exists","message":"additional context"}]}`,
},
"basic wrapped error + kvs with objects 2": {
input: eris.Wrap(eris.Wrap(eris.New("root error").WithCode(eris.CodeNotFound).WithProperty("obj", serializableObj), "additional context"), "outer error"),
output: `{"root":{"KVs":{"obj":{"a":"aVal","b":1}},"code":"not found","message":"root error"},"wrap":[{"code":"internal","message":"outer error"},{"code":"internal","message":"additional context"}]}`,
},
"basic wrapped error + ptr kvs": {
input: eris.Wrap(eris.Wrap(eris.New("root error").WithProperty("ptr", nil), "additional context"), "even more context"),
output: `{"root":{"KVs":{"ptr":null},"code":"unknown","message":"root error"},"wrap":[{"code":"internal","message":"even more context"},{"code":"internal","message":"additional context"}]}`,
},
"external error + valid kvs": {
input: eris.WithCode(eris.Wrap(
errors.New("external error"),
"additional context"), eris.CodeNotFound),
output: `{"external":"external error","root":{"code":"not found","message":"additional context"}}`,
},
}
for desc, tt := range tests {
t.Run(desc, func(t *testing.T) {
j := eris.ToJSON(tt.input, false)
result, _ := json.Marshal(j)
if got := string(result); !reflect.DeepEqual(got, tt.output) {
t.Errorf("ToJSON() = %v, want %v", got, tt.output)
}
})
}
}
func TestFormatJSON(t *testing.T) {
tests := map[string]struct {
input error
output string
}{
"basic root error": {
input: eris.New("root error").WithCode(eris.CodeCanceled),
output: `{"root":{"code":"canceled","message":"root error"}}`,
},
"basic wrapped error": {
input: eris.WithCode(eris.Wrap(
eris.WithCode(eris.Wrap(
eris.WithCode(eris.New("root error"), eris.CodeNotFound),
"additional context"), eris.CodeAlreadyExists),
"even more context"), eris.CodeUnknown),
output: `{"root":{"code":"not found","message":"root error"},"wrap":[{"code":"unknown","message":"even more context"},{"code":"already exists","message":"additional context"}]}`,
},
"external error": {
input: eris.WithCode(eris.Wrap(
errors.New("external error"),
"additional context"), eris.CodeDataLoss),
output: `{"external":"external error","root":{"code":"data loss","message":"additional context"}}`,
},
}
for desc, tt := range tests {
t.Run(desc, func(t *testing.T) {
result, _ := json.Marshal(eris.ToJSON(tt.input, false))
if got := string(result); !reflect.DeepEqual(got, tt.output) {
t.Errorf("ToJSON() = %v, want %v", got, tt.output)
}
})
}
}
func TestInvertedFormatJSON(t *testing.T) {
tests := map[string]struct {
input error
output string
}{
"basic wrapped error": {
input: eris.WithCode(eris.Wrap(
eris.WithCode(eris.Wrap(
eris.WithCode(eris.New("root error"), eris.CodeAlreadyExists),
"additional context"), eris.CodeUnknown),
"even more context"), eris.CodeUnknown),
output: `{"root":{"code":"already exists","message":"root error"},"wrap":[{"code":"unknown","message":"additional context"},{"code":"unknown","message":"even more context"}]}`,
},
}
for desc, tt := range tests {
t.Run(desc, func(t *testing.T) {
format := eris.NewDefaultJSONFormat(eris.FormatOptions{
InvertOutput: true,
})
result, _ := json.Marshal(eris.ToCustomJSON(tt.input, format))
if got := string(result); !reflect.DeepEqual(got, tt.output) {
t.Errorf("ToJSON() = %v, want %v", got, tt.output)
}
})
}
}
func TestFormatJSONWithStack(t *testing.T) {
tests := map[string]struct {
input error
rootOutput map[string]any
wrapOutput []map[string]any
}{
"basic wrapped error": {
input: eris.WithCode(eris.Wrap(
eris.WithCode(eris.Wrap(
eris.WithCode(eris.New("root error"), eris.CodePermissionDenied),
"additional context"), eris.CodeUnavailable),
"even more context"), eris.CodeUnknown),
rootOutput: map[string]any{
"code": "permission denied",
"message": "root error",
},
wrapOutput: []map[string]any{
{"code": "unavailable", "message": "even more context"},
{"code": "permission denied", "message": "additional context"},
},
},
}
for desc, tt := range tests {
t.Run(desc, func(t *testing.T) {
format := eris.NewDefaultJSONFormat(eris.FormatOptions{
WithTrace: true,
InvertTrace: true,
})
errJSON := eris.ToCustomJSON(tt.input, format)
// make sure messages are correct and stack elements exist (actual stack validation is in stack_test.go)
if rootMap, ok := errJSON["root"].(map[string]any); ok {
if _, exists := rootMap["message"]; !exists {
t.Fatalf("%v: expected a 'message' field in the output but didn't find one { %v }", desc, errJSON)
}
if rootMap["message"] != tt.rootOutput["message"] {
t.Errorf("%v: expected { %v } got { %v }", desc, tt.rootOutput["message"], rootMap["message"])
}
if _, exists := rootMap["stack"]; !exists {
t.Fatalf("%v: expected a 'stack' field in the output but didn't find one { %v }", desc, errJSON)
}
} else {
t.Errorf("%v: expected root error is malformed { %v }", desc, errJSON)
}
// make sure messages are correct and stack elements exist (actual stack validation is in stack_test.go)
if wrapMap, ok := errJSON["wrap"].([]map[string]any); ok {
if len(tt.wrapOutput) != len(wrapMap) {
t.Fatalf("%v: expected number of wrap layers { %v } doesn't match actual { %v }", desc, len(tt.wrapOutput), len(wrapMap))
}
for i := 0; i < len(wrapMap); i++ {
if _, exists := wrapMap[i]["message"]; !exists {
t.Fatalf("%v: expected a 'message' field in the output but didn't find one { %v }", desc, errJSON)
}
if wrapMap[i]["message"] != tt.wrapOutput[i]["message"] {
t.Errorf("%v: expected { %v } got { %v }", desc, tt.wrapOutput[i]["message"], wrapMap[i]["message"])
}
if _, exists := wrapMap[i]["stack"]; !exists {
t.Fatalf("%v: expected a 'stack' field in the output but didn't find one { %v }", desc, errJSON)
}
}
} else {
t.Errorf("%v: expected wrap error is malformed { %v }", desc, errJSON)
}
})
}
}
func TestFormatJoinError(t *testing.T) {
tests := map[string]struct {
input error
withTrace bool
withExternal bool
stringOutput string
regexOutput *regexp.Regexp
rootOutput map[string]any
wrapOutput []map[string]any
externalsOutput []map[string]any
}{
"without trace and external": {
input: eris.Wrap(eris.Join(
fmt.Errorf("fmt error"),
eris.Wrap(eris.Wrap(fmt.Errorf("external"), "wrap2"), "wrap1"),
eris.New("eris error"),
), "outer wrap"),
withTrace: false,
withExternal: false,
stringOutput: "code(internal) outer wrap: code(unknown) join error",
rootOutput: map[string]any{
"message": "join error",
},
wrapOutput: []map[string]any{
{
"message": "outer wrap",
},
},
},
"without trace": {
input: eris.Wrap(eris.Join(
fmt.Errorf("fmt error"),
eris.Wrap(eris.Wrap(fmt.Errorf("external"), "wrap2"), "wrap1"),
eris.New("eris error"),
), "outer wrap"),
withTrace: false,
withExternal: true,
stringOutput: `code(internal) outer wrap: code(unknown) join error
0> fmt error
1> code(internal) wrap1: code(internal) wrap2: external
2> code(unknown) eris error`,
rootOutput: map[string]any{
"message": "join error",
},
wrapOutput: []map[string]any{
{
"message": "outer wrap",
},
},
externalsOutput: []map[string]any{
{
"external": "fmt error",
}, {
"external": "external",
"root": map[string]any{},
"wrap": []map[string]any{},
}, {
"root": map[string]any{},
},
},
},
"with trace": {
input: eris.Wrap(eris.Join(
fmt.Errorf("fmt error"),
eris.Wrap(eris.Wrap(fmt.Errorf("external"), "wrap2"), "wrap1"),
eris.New("eris error"),
), "outer wrap"),
withTrace: true,
withExternal: true,
regexOutput: regexp.MustCompile(`code\(internal\) outer wrap
eris_test\.TestFormatJoinError:\S+:\d+
code\(unknown\) join error
eris_test\.TestFormatJoinError:\S+:\d+
eris_test\.TestFormatJoinError:\S+:\d+
0> fmt error
1> code\(internal\) wrap1
eris_test\.TestFormatJoinError:\S+:\d+
code\(internal\) wrap2
eris_test\.TestFormatJoinError:\S+:\d+
eris_test\.TestFormatJoinError:\S+:\d+
external
2> code\(unknown\) eris error
eris_test\.TestFormatJoinError:\S+:\d+`),
rootOutput: map[string]any{
"message": "join error",
},
wrapOutput: []map[string]any{
{
"message": "outer wrap",
},
},
externalsOutput: []map[string]any{
{
"external": "fmt error",
}, {
"external": "external",
"root": map[string]any{},
"wrap": []map[string]any{},
}, {
"root": map[string]any{},
},
},
},
}
for desc, tt := range tests {
t.Run(desc, func(t *testing.T) {
errStr := eris.ToCustomString(tt.input, eris.NewDefaultStringFormat(eris.FormatOptions{
WithTrace: tt.withTrace,
WithExternal: tt.withExternal,
}))
if tt.stringOutput != "" && tt.stringOutput != errStr {
t.Errorf("%v: expected { %v } got { %v }", desc, tt.stringOutput, errStr)
}
if tt.regexOutput != nil && !tt.regexOutput.MatchString(errStr) {
t.Errorf("%v: expected match { %v } got { %v }", desc, tt.regexOutput.String(), errStr)
}
errJSON := eris.ToCustomJSON(tt.input, eris.NewDefaultJSONFormat(eris.FormatOptions{
WithTrace: tt.withTrace,
WithExternal: tt.withExternal,
}))
// make sure messages are correct and stack elements exist (actual stack validation is in stack_test.go)
if rootMap, ok := errJSON["root"].(map[string]any); ok {
if _, exists := rootMap["message"]; !exists {
t.Fatalf("%v: expected a 'message' field in the output but didn't find one { %v }", desc, errJSON)
}
if rootMap["message"] != tt.rootOutput["message"] {
t.Errorf("%v: expected { %v } got { %v }", desc, tt.rootOutput["message"], rootMap["message"])
}
if _, exists := rootMap["stack"]; tt.withTrace && !exists {
t.Fatalf("%v: expected a 'stack' field in the output but didn't find one { %v }", desc, errJSON)
}
} else {
t.Errorf("%v: expected root error is malformed { %v }", desc, errJSON)
}
// make sure messages are correct and stack elements exist (actual stack validation is in stack_test.go)
if wrapMap, ok := errJSON["wrap"].([]map[string]any); ok {
if len(tt.wrapOutput) != len(wrapMap) {
t.Fatalf("%v: expected number of wrap layers { %v } doesn't match actual { %v }", desc, len(tt.wrapOutput), len(wrapMap))
}
for i := 0; i < len(wrapMap); i++ {
if _, exists := wrapMap[i]["message"]; !exists {
t.Fatalf("%v: expected a 'message' field in the output but didn't find one { %v }", desc, errJSON)
}
if wrapMap[i]["message"] != tt.wrapOutput[i]["message"] {
t.Errorf("%v: expected { %v } got { %v }", desc, tt.wrapOutput[i]["message"], wrapMap[i]["message"])
}
if _, exists := wrapMap[i]["stack"]; tt.withTrace && !exists {
t.Fatalf("%v: expected a 'stack' field in the output but didn't find one { %v }", desc, errJSON)
}
}
} else {
t.Errorf("%v: expected wrap error is malformed { %v }", desc, errJSON)
}
if externalsMap, ok := errJSON["externals"].([]map[string]any); ok {
if len(tt.externalsOutput) != len(externalsMap) {
t.Fatalf("%v: expected number of externals errors { %v } doesn't match actual { %v }", desc, len(tt.externalsOutput), len(externalsMap))
}
for i, externalsOutputItem := range tt.externalsOutput {
for key, val := range externalsOutputItem {
switch val.(type) {
case string:
if externalsMap[i][key] != val {
t.Errorf("%v: expected externals[%d][%s] { %v } got { %v }", desc, i, key, val, externalsMap[i][key])
}
case map[string]any:
if _, ok := externalsMap[i][key].(map[string]any); !ok {
t.Errorf("%v: expected externals[%d][%s] is object got { %v }", desc, i, key, externalsMap[i][key])
}
case []map[string]any:
if _, ok := externalsMap[i][key].([]map[string]any); !ok {
t.Errorf("%v: expected externals[%d][%s] is object array got { %v }", desc, i, key, externalsMap[i][key])
}
}
}
}
} else if tt.withExternal {
t.Errorf("%v: expected externals error is malformed { %v }", desc, errJSON)
}
})
}
}