-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
377 lines (332 loc) · 12.6 KB
/
error_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
package xerror_test
import (
"fmt"
"strings"
"testing"
"github.com/matryer/is"
"github.com/pkg/errors"
"github.com/tauraamui/xerror"
)
type tableTest interface {
preTest(*testing.T)
run(*testing.T)
}
type xerrorWrappedErrorIsTest struct {
skip bool
title string
parentError error
wrappedError error
shouldNotMatch bool
}
func (x xerrorWrappedErrorIsTest) preTest(t *testing.T) {
if len(x.title) == 0 {
t.Error("table tests must all have titles")
}
if x.skip {
t.Skip()
}
}
func (x xerrorWrappedErrorIsTest) run(t *testing.T) {
t.Run(x.title, func(t *testing.T) {
is := is.NewRelaxed(t)
r := errors.Is(x.parentError, x.wrappedError)
is.True((x.shouldNotMatch && !r) || (!x.shouldNotMatch && r))
})
}
var (
nativeErrorType = errors.New("native error type")
customErrorType = xerror.New("custom error type")
deepChildErrorType = xerror.New("deep error type")
)
func TestWrappedErrorsFoundWithIsError(t *testing.T) {
tests := []xerrorWrappedErrorIsTest{
{
title: "error is resolves native error wrapped by native error type",
parentError: fmt.Errorf("parent error: %w", nativeErrorType),
wrappedError: nativeErrorType,
},
{
title: "error is resolves custom error wrapped by native error type",
parentError: fmt.Errorf("parent error: %w", customErrorType),
wrappedError: customErrorType,
},
{
title: "error is resolves custom error with params wrapped by native error type",
parentError: fmt.Errorf("parent error: %w", customErrorType.WithParam("music", "heavy-metal")),
wrappedError: customErrorType,
},
{
title: "error is resolves custom error with stack trace wrapped by native error type",
parentError: fmt.Errorf("parent error: %w", customErrorType.WithStackTrace()),
wrappedError: customErrorType,
},
{
title: "error is resolves deeply buried error in custom error tree wrapped by native error type",
parentError: fmt.Errorf(
"parent error: %w", xerror.Errorf(
"sub-1 parent error: %w", xerror.Errorf(
"sub-2 parent error: %w", deepChildErrorType,
)),
),
wrappedError: deepChildErrorType,
},
{
title: "error is resolves custom error with params wrapped in custom error",
parentError: xerror.Errorf("parent error: %w", customErrorType.WithParam("music", "heavy-metal")),
wrappedError: customErrorType,
},
{
title: "error is resolves custom error wrapped in custom error with params",
parentError: xerror.Errorf("parent error: %w", customErrorType).WithParam("holiday", "spain"),
wrappedError: customErrorType,
},
{
title: "error is resolves custom error wrapped in custom error with stack trace",
parentError: xerror.Errorf("parent error: %w", customErrorType).WithStackTrace(),
wrappedError: customErrorType,
},
{
title: "error is resolves custom error with stack trace wrapped in custom error",
parentError: xerror.Errorf("parent error: %w", customErrorType.WithStackTrace()),
wrappedError: customErrorType,
},
{
title: "error is resolves deeply buried error wrapped in custom errors tree",
parentError: xerror.Errorf(
"parent error: %w", xerror.Errorf(
"sub-1 parent error: %w", xerror.Errorf(
"sub-2 parent error: %w", deepChildErrorType,
)),
),
wrappedError: deepChildErrorType,
},
{
title: "custom error with no wrapped error",
parentError: xerror.Errorf("no wrapped errors here"),
wrappedError: customErrorType,
shouldNotMatch: true,
},
{
title: "custom error with different wrapped error",
parentError: xerror.Errorf("native error right?: %w", nativeErrorType),
wrappedError: customErrorType,
shouldNotMatch: true,
},
}
for _, tt := range tests {
runTest(t, tt)
}
}
func TestNewErrorGivesErrInstance(t *testing.T) {
is := is.New(t)
err := xerror.New("")
is.True(err != nil)
}
func TestIsKind(t *testing.T) {
is := is.New(t)
err := xerror.New("test error").AsKind("RANDOM")
is.True(err.IsKind("RANDOM"))
}
func TestIsKindAssertsChildInstance(t *testing.T) {
is := is.New(t)
err := xerror.Errorf("wrapped custom err: %w", xerror.NewWithKind("WRAPPED_ERROR", "not enough rocks"))
is.True(err.IsKind("WRAPPED_ERROR"))
is.Equal(err.Error(), "wrapped custom err: Kind: WRAPPED_ERROR | not enough rocks")
}
func TestIsKindAssertsChildInstanceFromAsKindSuffix(t *testing.T) {
is := is.New(t)
err := xerror.Errorf("wrapped custom err: %w", xerror.New("not enough rocks").AsKind("WRAPPED_ERROR"))
is.True(err.IsKind("WRAPPED_ERROR"))
is.Equal(err.Error(), "wrapped custom err: Kind: WRAPPED_ERROR | not enough rocks")
}
func TestIsKindAssertsMultipleDescendantChildInstanceFromAsKindSuffix(t *testing.T) {
is := is.New(t)
err := xerror.Errorf("grandparent wrapped custom err: %w", xerror.Errorf("parent wrapped custom err: %w", xerror.Errorf("child wrapped custom err: %w", xerror.New("sea depth too low").AsKind("DEEP_WRAPPED_ERROR"))))
is.True(err.IsKind("DEEP_WRAPPED_ERROR"))
is.Equal(err.Error(), "grandparent wrapped custom err: parent wrapped custom err: child wrapped custom err: Kind: DEEP_WRAPPED_ERROR | sea depth too low")
}
const (
TestError = xerror.Kind("test_error")
TestParamsError = xerror.Kind("test_params_error")
)
func TestErrorMsgMatchesGivenErrorMsg(t *testing.T) {
t.Run("error msg matches given initial msg but doesn't include context", func(t *testing.T) {
is := is.New(t)
err := xerror.NewWithKind("MUTABLE_ERR", "test error message")
is.Equal(err.ErrorMsg(), "test error message")
})
t.Run("error msg matches msg which replaces initial msg", func(t *testing.T) {
is := is.New(t)
err := xerror.NewWithKind("MUTABLE_ERR", "test error message").Msg("replaced message!")
is.Equal(err.ErrorMsg(), "replaced message!")
is.Equal(err.Error(), "Kind: MUTABLE_ERR | replaced message!")
})
}
func TestErrorToError(t *testing.T) {
is := is.New(t)
nativeErr := xerror.NewWithKind("NATIVE_ERR", "native error").ToError()
is.True(nativeErr != nil)
is.Equal(nativeErr.Error(), "Kind: NATIVE_ERR | native error")
}
func TestErrorMethodWithoutPinnedDescendant(t *testing.T) {
is := is.New(t)
err := xerror.Errorf("wrapped custom err: %w", xerror.Errorf("not enough rocks").AsKind("WRAPPED_ERROR"))
is.True(err.IsKind("WRAPPED_ERROR"))
is.Equal(err.Error(), "wrapped custom err: Kind: WRAPPED_ERROR | not enough rocks")
}
func TestErrorMethodWithPinnedDescendant(t *testing.T) {
is := is.New(t)
err := xerror.Errorf("wrapped custom err: %w", xerror.Errorf("not enough rocks").AsKind("WRAPPED_ERROR").Pin())
is.True(err.IsKind("WRAPPED_ERROR"))
is.Equal(err.Error(), "Kind: WRAPPED_ERROR | wrapped custom err: not enough rocks")
}
type xerrorExpectedStringTest struct {
skip bool
title string
err error
expected string
customEval func(string) error
}
func (x xerrorExpectedStringTest) preTest(t *testing.T) {
if len(x.title) == 0 {
t.Error("table tests must all have titles")
}
if x.skip {
t.Skip()
}
}
func (x xerrorExpectedStringTest) run(t *testing.T) {
t.Run(x.title, func(t *testing.T) {
is := is.NewRelaxed(t)
if x.customEval != nil {
is.NoErr(x.customEval(x.err.Error()))
return
}
is.Equal(x.err.Error(), x.expected)
})
}
func TestNewErrorAndErrorfsOutputsExpectedString(t *testing.T) {
tests := []xerrorExpectedStringTest{
{
title: "simple new error just prints out msg string",
err: xerror.New("fake db update failed"),
expected: "fake db update failed",
},
{
title: "new error with param prints out msg string with not assigned kind and with param",
err: xerror.New("fake db update failed").WithParam("trace-request-id", "efw4fv32f"),
expected: "Kind: N/A | fake db update failed, Params: [trace-request-id: {efw4fv32f}]",
},
{
title: "new error with kind prints out msg string with assigned kind",
err: xerror.NewWithKind(TestError, "fake db update failed"),
expected: "Kind: TEST_ERROR | fake db update failed",
},
{
title: "new error with kind and param prints out msg string with assigned kind and with param",
err: xerror.NewWithKind(TestParamsError, "fake db update failed").WithParam("trace-request-id", "wdgrte4fg"),
expected: "Kind: TEST_PARAMS_ERROR | fake db update failed, Params: [trace-request-id: {wdgrte4fg}]",
},
{
title: "new error with kind but then has kind changed prints out msg string with new assigned kind",
err: xerror.NewWithKind(TestParamsError, "fake db update failed").AsKind("CHANGED_KIND"),
expected: "Kind: CHANGED_KIND | fake db update failed",
},
{
title: "new error with not assigned kind but then has kind changed prints out msg string with new assigned kind",
err: xerror.New("fake db update failed").AsKind("FROM_NONE_TO_CHANGED_KIND"),
expected: "Kind: FROM_NONE_TO_CHANGED_KIND | fake db update failed",
},
{
title: "new error with msg but has msg updated prints out msg string with new content",
err: xerror.New("fake db update failed").Msg("err msg content replaced with this!"),
expected: "err msg content replaced with this!",
},
{
title: "new error with not assigned kind and with params prints out msg string with not assigned kind and with params",
err: xerror.New("fake db update failed").WithParams(
map[string]interface{}{
"trace-request-id": "fr495fre",
"request-ip": "39.49.13.45",
},
),
expected: "Kind: N/A | fake db update failed, Params: [trace-request-id: {fr495fre} | request-ip: {39.49.13.45}]",
customEval: func(s string) error {
if !strings.Contains(s, "Kind: N/A | fake db update failed, Params: [") {
return errors.New("error msg does not include header section")
}
if !strings.Contains(s, "trace-request-id: {fr495fre}") {
return errors.New("error msg params do not contain trace request id entry")
}
if !strings.Contains(s, "request-ip: {39.49.13.45}") {
return errors.New("error msg params do not contain request ip entry")
}
return nil
},
},
{
title: " new error with not assigned kind has param and then with params and prints out expected string",
err: xerror.New("fake db update failed").WithParam("fruit-type", "peach").WithParams(
map[string]interface{}{
"trace-request-id": "fr495fre",
"request-ip": "39.49.13.45",
},
),
// keeping unused param here to be clear what we expect
// the msg to look like, if we pretend that maps are always
// in key insertion order, which they are not.
expected: "Kind: N/A | fake db update failed, Params: [fruit-type: {peach} | trace-request-id: {fr495fre} | request-ip: {39.49.13.45}]",
customEval: func(s string) error {
if !strings.Contains(s, "Kind: N/A | fake db update failed, Params: [") {
return errors.New("error msg does not include header section")
}
if !strings.Contains(s, "fruit-type: {peach}") {
return errors.New("error msg params do not contain peach entry")
}
if !strings.Contains(s, "trace-request-id: {fr495fre}") {
return errors.New("error msg params do not contain trace request id entry")
}
if !strings.Contains(s, "request-ip: {39.49.13.45}") {
return errors.New("error msg params do not contain request ip entry")
}
return nil
},
},
{
title: "errorf prints out msg string",
err: xerror.Errorf("too many seconds %d/60 elapsed", 112),
expected: "too many seconds 112/60 elapsed",
},
{
title: "errorf with stack trace prints out msg string",
err: xerror.Errorf("too many seconds %d/60 elapsed", 112).WithStackTrace(),
expected: "too many seconds 112/60 elapsed",
customEval: func(s string) error {
if !strings.Contains(s, "too many seconds 112/60 elapsed") {
return errors.New("error msg does not include header section")
}
if !strings.Contains(s, "/xerror.(*x).format") {
return errors.New("error msg does probably not contain stack trace")
}
return nil
},
},
{
title: "errorf wrapped in native error includes xerr's context information",
err: fmt.Errorf("wrapped err: %w", xerror.Errorf("not enough rocks %d/30 in bucket", 19).AsKind("CUSTOM_ERROR")),
expected: "wrapped err: Kind: CUSTOM_ERROR | not enough rocks 19/30 in bucket",
},
{
title: "custom error wrapped in custom error includes each one's context information",
err: xerror.Errorf("wrapped custom err: %w", xerror.NewWithKind("WRAPPED_ERROR", "not enough rocks")),
expected: "wrapped custom err: Kind: WRAPPED_ERROR | not enough rocks",
},
}
for _, tt := range tests {
runTest(t, tt)
}
}
func runTest(t *testing.T, tt tableTest) {
tt.preTest(t)
tt.run(t)
}