-
Notifications
You must be signed in to change notification settings - Fork 52
/
format_test.go
303 lines (289 loc) · 8.81 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
package eris_test
import (
"encoding/json"
"errors"
"reflect"
"testing"
"github.com/rotisserie/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"),
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"),
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"),
output: "root error",
},
"basic wrapped error": {
input: eris.Wrap(eris.Wrap(eris.New("root error"), "additional context"), "even more context"),
output: "even more context: additional context: root error",
},
"external wrapped error": {
input: eris.Wrap(errors.New("external error"), "additional context"),
output: "additional context: external error",
},
"external error": {
input: errors.New("external error"),
output: "external error",
},
"empty error": {
input: eris.New(""),
output: "",
},
"empty wrapped external error": {
input: eris.Wrap(errors.New(""), "additional context"),
output: "additional context: ",
},
"empty wrapped error": {
input: eris.Wrap(eris.New(""), "additional context"),
output: "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.Wrap(eris.Wrap(eris.New("root error"), "additional context"), "even more context"),
output: "root error: additional context: even more context",
},
"external wrapped error": {
input: eris.Wrap(errors.New("external error"), "additional context"),
output: "external error: additional context",
},
"external error": {
input: errors.New("external error"),
output: "external error",
},
"empty wrapped external error": {
input: eris.Wrap(errors.New(""), "additional context"),
output: ": additional context",
},
"empty wrapped error": {
input: eris.Wrap(eris.New(""), "additional context"),
output: ": 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 TestFormatJSON(t *testing.T) {
tests := map[string]struct {
input error
output string
}{
"basic root error": {
input: eris.New("root error"),
output: `{"root":{"message":"root error"}}`,
},
"basic wrapped error": {
input: eris.Wrap(eris.Wrap(eris.New("root error"), "additional context"), "even more context"),
output: `{"root":{"message":"root error"},"wrap":[{"message":"even more context"},{"message":"additional context"}]}`,
},
"external error": {
input: eris.Wrap(errors.New("external error"), "additional context"),
output: `{"external":"external error","root":{"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.Wrap(eris.Wrap(eris.New("root error"), "additional context"), "even more context"),
output: `{"root":{"message":"root error"},"wrap":[{"message":"additional context"},{"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]interface{}
wrapOutput []map[string]interface{}
}{
"basic wrapped error": {
input: eris.Wrap(eris.Wrap(eris.New("root error"), "additional context"), "even more context"),
rootOutput: map[string]interface{}{
"message": "root error",
},
wrapOutput: []map[string]interface{}{
{"message": "even more context"},
{"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]interface{}); 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, rootMap["message"], tt.rootOutput["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]interface{}); 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, wrapMap[i]["message"], tt.wrapOutput[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)
}
})
}
}