This repository has been archived by the owner on Mar 26, 2019. It is now read-only.
forked from getsentry/raven-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
144 lines (131 loc) · 3.49 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
package raven
import (
"fmt"
"reflect"
"testing"
pkgErrors "github.com/pkg/errors"
)
func TestWrapWithExtraGeneratesProperErrWithExtra(t *testing.T) {
errMsg := "This is bad"
baseErr := fmt.Errorf(errMsg)
extraInfo := map[string]interface{}{
"string": "string",
"int": 1,
"float": 1.001,
"bool": false,
}
testErr := WrapWithExtra(baseErr, extraInfo)
wrapped, ok := testErr.(ErrWithExtra)
if !ok {
t.Errorf("Wrapped error does not conform to expected protocol.")
}
if !reflect.DeepEqual(wrapped.Cause(), baseErr) {
t.Errorf("Failed to unwrap error, got %+v, expected %+v", wrapped.Cause(), baseErr)
}
returnedExtra := wrapped.ExtraInfo()
for expectedKey, expectedVal := range extraInfo {
val, ok := returnedExtra[expectedKey]
if !ok {
t.Errorf("Extra data missing key: %s", expectedKey)
}
if val != expectedVal {
t.Errorf("Extra data [%s]: Got: %+v, expected: %+v", expectedKey, val, expectedVal)
}
}
if wrapped.Error() != errMsg {
t.Errorf("Wrong error message, got: %q, expected: %q", wrapped.Error(), errMsg)
}
}
func TestWrapWithExtraGeneratesCausableError(t *testing.T) {
baseErr := fmt.Errorf("this is bad")
testErr := WrapWithExtra(baseErr, nil)
cause := pkgErrors.Cause(testErr)
if !reflect.DeepEqual(cause, baseErr) {
t.Errorf("Failed to unwrap error, got %+v, expected %+v", cause, baseErr)
}
}
func TestExtractErrorPullsExtraData(t *testing.T) {
extraInfo := map[string]interface{}{
"string": "string",
"int": 1,
"float": 1.001,
"bool": false,
}
emptyInfo := map[string]interface{}{}
testCases := []struct {
Error error
Expected map[string]interface{}
}{
// Unwrapped error shouldn't include anything
{
Error: fmt.Errorf("This is bad"),
Expected: emptyInfo,
},
// Wrapped error with nil map should extract as empty info
{
Error: WrapWithExtra(fmt.Errorf("This is bad"), nil),
Expected: emptyInfo,
},
// Wrapped error with empty map should extract as empty info
{
Error: WrapWithExtra(fmt.Errorf("This is bad"), emptyInfo),
Expected: emptyInfo,
},
// Wrapped error with extra info should extract with all data
{
Error: WrapWithExtra(fmt.Errorf("This is bad"), extraInfo),
Expected: extraInfo,
},
// Nested wrapped error should extract all the info
{
Error: WrapWithExtra(
WrapWithExtra(fmt.Errorf("This is bad"),
map[string]interface{}{
"inner": "123",
}),
map[string]interface{}{
"outer": "456",
},
),
Expected: map[string]interface{}{
"inner": "123",
"outer": "456",
},
},
// Futher wrapping of errors shouldn't allow for value override
{
Error: WrapWithExtra(
WrapWithExtra(fmt.Errorf("This is bad"),
map[string]interface{}{
"dontoverride": "123",
}),
map[string]interface{}{
"dontoverride": "456",
},
),
Expected: map[string]interface{}{
"dontoverride": "123",
},
},
}
for i, test := range testCases {
extracted := extractExtra(test.Error)
if len(test.Expected) != len(extracted) {
t.Errorf(
"Case [%d]: Mismatched amount of data between provided and extracted extra. Got: %+v Expected: %+v",
i,
extracted,
test.Expected,
)
}
for expectedKey, expectedVal := range test.Expected {
val, ok := extracted[expectedKey]
if !ok {
t.Errorf("Case [%d]: Extra data missing key: %s", i, expectedKey)
}
if val != expectedVal {
t.Errorf("Case [%d]: Wrong extra data for %q. Got: %+v, expected: %+v", i, expectedKey, val, expectedVal)
}
}
}
}