Debian Codegraph @

errors_test.go

raw
1 package raven
2
3 import (
4 "fmt"
5 "reflect"
6 "testing"
7
8 pkgErrors "github.com/pkg/errors"
9 )
10
11 func TestWrapWithExtraGeneratesProperErrWithExtra(t *testing.T) {
12 errMsg := "This is bad"
13 baseErr := fmt.Errorf(errMsg)
14 extraInfo := map[string]interface{}{
15 "string": "string",
16 "int": 1,
17 "float": 1.001,
18 "bool": false,
19 }
20
21 testErr := WrapWithExtra(baseErr, extraInfo)
22 wrapped, ok := testErr.(ErrWithExtra)
23 if !ok {
24 t.Errorf("Wrapped error does not conform to expected protocol.")
25 }
26
27 if !reflect.DeepEqual(wrapped.Cause(), baseErr) {
28 t.Errorf("Failed to unwrap error, got %+v, expected %+v", wrapped.Cause(), baseErr)
29 }
30
31 returnedExtra := wrapped.ExtraInfo()
32 for expectedKey, expectedVal := range extraInfo {
33 val, ok := returnedExtra[expectedKey]
34 if !ok {
35 t.Errorf("Extra data missing key: %s", expectedKey)
36 }
37 if val != expectedVal {
38 t.Errorf("Extra data [%s]: Got: %+v, expected: %+v", expectedKey, val, expectedVal)
39 }
40 }
41
42 if wrapped.Error() != errMsg {
43 t.Errorf("Wrong error message, got: %q, expected: %q", wrapped.Error(), errMsg)
44 }
45 }
46
47 func TestWrapWithExtraGeneratesCausableError(t *testing.T) {
48 baseErr := fmt.Errorf("this is bad")
49 testErr := WrapWithExtra(baseErr, nil)
50 cause := pkgErrors.Cause(testErr)
51
52 if !reflect.DeepEqual(cause, baseErr) {
53 t.Errorf("Failed to unwrap error, got %+v, expected %+v", cause, baseErr)
54 }
55 }
56
57 func TestExtractErrorPullsExtraData(t *testing.T) {
58 extraInfo := map[string]interface{}{
59 "string": "string",
60 "int": 1,
61 "float": 1.001,
62 "bool": false,
63 }
64 emptyInfo := map[string]interface{}{}
65
66 testCases := []struct {
67 Error error
68 Expected map[string]interface{}
69 }{
70 // Unwrapped error shouldn't include anything
71 {
72 Error: fmt.Errorf("This is bad"),
73 Expected: emptyInfo,
74 },
75 // Wrapped error with nil map should extract as empty info
76 {
77 Error: WrapWithExtra(fmt.Errorf("This is bad"), nil),
78 Expected: emptyInfo,
79 },
80 // Wrapped error with empty map should extract as empty info
81 {
82 Error: WrapWithExtra(fmt.Errorf("This is bad"), emptyInfo),
83 Expected: emptyInfo,
84 },
85 // Wrapped error with extra info should extract with all data
86 {
87 Error: WrapWithExtra(fmt.Errorf("This is bad"), extraInfo),
88 Expected: extraInfo,
89 },
90 // Nested wrapped error should extract all the info
91 {
92 Error: WrapWithExtra(
93 WrapWithExtra(fmt.Errorf("This is bad"),
94 map[string]interface{}{
95 "inner": "123",
96 }),
97 map[string]interface{}{
98 "outer": "456",
99 },
100 ),
101 Expected: map[string]interface{}{
102 "inner": "123",
103 "outer": "456",
104 },
105 },
106 // Futher wrapping of errors shouldn't allow for value override
107 {
108 Error: WrapWithExtra(
109 WrapWithExtra(fmt.Errorf("This is bad"),
110 map[string]interface{}{
111 "dontoverride": "123",
112 }),
113 map[string]interface{}{
114 "dontoverride": "456",
115 },
116 ),
117 Expected: map[string]interface{}{
118 "dontoverride": "123",
119 },
120 },
121 }
122
123 for i, test := range testCases {
124 extracted := extractExtra(test.Error)
125 if len(test.Expected) != len(extracted) {
126 t.Errorf(
127 "Case [%d]: Mismatched amount of data between provided and extracted extra. Got: %+v Expected: %+v",
128 i,
129 extracted,
130 test.Expected,
131 )
132 }
133
134 for expectedKey, expectedVal := range test.Expected {
135 val, ok := extracted[expectedKey]
136 if !ok {
137 t.Errorf("Case [%d]: Extra data missing key: %s", i, expectedKey)
138 }
139 if val != expectedVal {
140 t.Errorf("Case [%d]: Wrong extra data for %q. Got: %+v, expected: %+v", i, expectedKey, val, expectedVal)
141 }
142 }
143 }
144 }