-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
347 lines (299 loc) · 10.7 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
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
package errors
import (
stderrors "errors"
"fmt"
"testing"
pkgerrors "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
func TestIs(t *testing.T) {
originalErr := stderrors.New("original") // go/errors
wrappedErr1 := pkgerrors.Wrap(originalErr, "wrapped 1") // pkg/errors
wrappedErr2 := Wrap(wrappedErr1, "wrapped 2")
wrappedErr3 := fmt.Errorf("wrapped 3: %w", wrappedErr2)
wrappedErr4 := Wrap(wrappedErr3, "wrapped 4")
sentinel1 := Validation.New("sentinel1")
sentinel2 := WithCause(sentinel1, wrappedErr4)
wrappedSentinel := Wrap(sentinel2, "wrapped sentinel")
assert.True(t, stderrors.Is(wrappedSentinel, sentinel2))
assert.True(t, stderrors.Is(wrappedSentinel, sentinel1))
assert.True(t, stderrors.Is(wrappedSentinel, wrappedErr4))
assert.True(t, stderrors.Is(wrappedSentinel, wrappedErr3))
assert.True(t, stderrors.Is(wrappedSentinel, wrappedErr2))
assert.True(t, stderrors.Is(wrappedSentinel, wrappedErr1))
assert.True(t, stderrors.Is(wrappedSentinel, originalErr))
assert.False(t, stderrors.Is(sentinel1, nil))
assert.False(t, stderrors.Is(sentinel1, wrappedErr4))
assert.True(t, stderrors.Is(wrappedErr4, wrappedErr3))
assert.True(t, stderrors.Is(wrappedErr4, wrappedErr2))
assert.True(t, stderrors.Is(wrappedErr4, wrappedErr1))
assert.True(t, stderrors.Is(wrappedErr4, originalErr))
assert.True(t, stderrors.Is(wrappedErr3, wrappedErr2))
assert.True(t, stderrors.Is(wrappedErr3, wrappedErr1))
assert.True(t, stderrors.Is(wrappedErr3, originalErr))
assert.True(t, stderrors.Is(wrappedErr2, wrappedErr1))
assert.True(t, stderrors.Is(wrappedErr2, originalErr))
assert.True(t, stderrors.Is(wrappedErr1, originalErr))
}
func TestUnwrap(t *testing.T) {
originalErr := stderrors.New("original") // go/errors
wrappedErr1 := Wrap(originalErr, "wrapped 1")
wrappedErr2 := fmt.Errorf("wrapped 2: %w", wrappedErr1)
wrappedErr3 := Wrap(wrappedErr2, "wrapped 3")
// Unwrap must be tested with Is
assert.True(t, stderrors.Is(wrappedErr3, wrappedErr2))
assert.True(t, stderrors.Is(wrappedErr2, wrappedErr1))
assert.True(t, stderrors.Is(wrappedErr1, originalErr))
}
func TestError_Error(t *testing.T) {
tests := []struct {
name string
err error
want string
}{
{
"standardError",
New("standard error"),
"standard error",
},
{
"wrappedError",
Wrap(fmt.Errorf("standard error"), "wrapped error"),
"wrapped error: standard error",
},
{
"wrappedErrorWithDetail",
WithDetail(
Wrap(fmt.Errorf("standard error"), "wrapped error"),
"Error Details",
),
"Error Details: wrapped error: standard error",
},
{
"wrappedErrorWithNestedDetail",
WithDetail(
Wrap(
BadRequest.NewWithDetail("standard error"),
"wrapped error",
),
"Error Details",
),
"Error Details: wrapped error: standard error",
},
{
"wrappedErrorWithKeyAndDetail",
WithKey(
WithDetail(
Wrap(
BadRequest.NewWithDetail("standard error"),
"wrapped error",
),
"Error Details",
),
"ERR_KEY",
),
"ERR_KEY: Error Details: wrapped error: standard error",
},
{
"causeErrorWithKeyAndDetail",
Wrap(
WithCause(
BadRequest.NewWithKeyAndDetail("ERR_SENTINEL", "Sentinel error detail"),
fmt.Errorf("standard error cause"),
),
"wrapped error",
),
"wrapped error: ERR_SENTINEL: Sentinel error detail: standard error cause",
},
{
"newWithKeyAndDetail",
BadRequest.NewWithKeyAndDetail("ERR_KEY", "Error Details"),
"ERR_KEY: Error Details",
},
{
"random",
WithKeyAndDetail(
Wrapf(
pkgerrors.Wrap(fmt.Errorf("fmt error"), "wrapped pkgerror"),
"wrapped error",
),
"ERR_KEY",
"Error Details",
),
"ERR_KEY: Error Details: wrapped error: wrapped pkgerror: fmt error",
},
}
for _, tt := range tests {
got := tt.err.Error()
assert.Equal(t, tt.want, got)
}
}
func TestStackTrace(t *testing.T) {
err := New("Inner error")
wrappedErr := Wrap(err, "Outer error")
trace := fmt.Sprintf("%+v", wrappedErr)
assert.Contains(t, trace, "gitlab.com/gamestopcorp/platform/blockchain/nft-lib-errors%2egit.New")
assert.Contains(t, trace, "gitlab.com/gamestopcorp/platform/blockchain/nft-lib-errors%2egit.Wrapf")
}
func TestErrorType_Wrapf(t *testing.T) {
origErr := New("an_error")
origErr = AddErrorContext(origErr, "field", "value")
wrappedErr := BadRequest.Wrapf(origErr, "error %s", "1")
wrappedErr = Wrapf(wrappedErr, "outer wrapped err %s", "1")
wantContext := map[string]string{
"field": "value",
}
assert.Equal(t, wantContext, GetErrorContext(wrappedErr))
assert.Equal(t, BadRequest, GetType(wrappedErr))
assert.EqualError(t, wrappedErr, "outer wrapped err 1: error 1: an_error")
}
func TestErrorType_NewWithDetail(t *testing.T) {
err := Public.NewWithDetail("This is public error detail")
wrappedErr := Wrap(err, "wrapped error")
assert.Equal(t, Public, GetType(wrappedErr))
assert.Equal(t, "This is public error detail", Detail(wrappedErr))
assert.EqualError(t, wrappedErr, "wrapped error: This is public error detail")
}
func TestErrorType_NewWithKeyAndDetail(t *testing.T) {
err := Public.NewWithKeyAndDetail("ERROR_KEY", "This is public error message")
assert.Equal(t, "This is public error message", Detail(err))
assert.Equal(t, "ERROR_KEY", Key(err))
}
func TestErrorType_NewWithDetailf(t *testing.T) {
err := Public.NewWithDetailf("This is public %s detail", "error")
wrappedErr := Wrap(err, "wrapped error")
assert.Equal(t, Public, GetType(wrappedErr))
assert.Equal(t, "This is public error detail", Detail(wrappedErr))
assert.EqualError(t, wrappedErr, "wrapped error: This is public error detail")
}
func TestWrapf(t *testing.T) {
err := New("an_error")
err = AddErrorContext(err, "field", "value")
wrappedErr := BadRequest.Wrapf(err, "error %s", "1")
wantContext := map[string]string{
"field": "value",
}
assert.Equal(t, BadRequest, GetType(wrappedErr))
assert.Equal(t, wantContext, GetErrorContext(wrappedErr))
assert.EqualError(t, wrappedErr, "error 1: an_error")
}
func TestWrapf_NoTypeError(t *testing.T) {
err := Newf("an_error %s", "2")
wrappedErr := Wrapf(err, "error %s", "1")
assert.Equal(t, NoType, GetType(wrappedErr))
assert.EqualError(t, wrappedErr, "error 1: an_error 2")
}
func TestGetErrorContext(t *testing.T) {
err := BadRequest.New("an_error")
errWithContext := AddErrorContext(err, "field1", "the field is empty")
errWithContext = AddErrorContext(errWithContext, "field2", "the field is empty")
wantContext := map[string]string{
"field1": "the field is empty",
"field2": "the field is empty",
}
assert.Equal(t, BadRequest, GetType(errWithContext))
assert.Equal(t, wantContext, GetErrorContext(errWithContext))
assert.Equal(t, err.Error(), errWithContext.Error())
assert.Equal(t, "the field is empty", GetErrorContextValue(errWithContext, "field2"))
}
func TestGetErrorContext_standardError(t *testing.T) {
err := fmt.Errorf("this is a standard error")
assert.Equal(t, map[string]string(nil), GetErrorContext(err))
}
func TestGetErrorContext_NoTypeError(t *testing.T) {
err := fmt.Errorf("this is a standard error")
errWithContext := AddErrorContext(err, "field1", "the field is empty")
errWithContext = AddErrorContext(errWithContext, "field2", "the field is empty")
wantContext := map[string]string{
"field1": "the field is empty",
"field2": "the field is empty",
}
assert.Equal(t, NoType, GetType(errWithContext))
assert.Equal(t, wantContext, GetErrorContext(errWithContext))
assert.Equal(t, err.Error(), errWithContext.Error())
}
type causeTestError struct{}
func (e causeTestError) Error() string {
return "Cause test error"
}
func TestCause(t *testing.T) {
originalErr := causeTestError{}
wrappedErr := Wrap(originalErr, "wrapped causeTestError")
wrappedErr = Wrap(wrappedErr, "outer wrapped causeTestError")
wrappedErr = Wrap(wrappedErr, "outer outer wrapped causeTestError")
causeErr := Cause(wrappedErr)
originalErr, ok := causeErr.(causeTestError)
assert.Equal(t, true, ok)
assert.Equal(t, causeErr, originalErr)
}
func TestGetType(t *testing.T) {
originalErr := New("original error with no type")
wrappedErr := Validation.Wrap(originalErr, "validation wrapped err")
outerWrappedErr := Wrap(wrappedErr, "outer wrapped error")
errType := GetType(outerWrappedErr)
assert.Equal(t, Validation, errType)
errType = GetType(stderrors.New("hi"))
assert.Equal(t, NoType, errType)
}
func Test_WithCause(t *testing.T) {
causeErr := New("an_error")
causeErr = AddErrorContext(causeErr, "field", "value")
causeErr = BadRequest.Wrapf(causeErr, "error %s", "1")
causeErr = Wrapf(causeErr, "outer wrapped err %s", "1")
sentinelErr := Validation.NewWithDetail("sentinelErr with detail")
gotErr := WithCause(sentinelErr, causeErr)
assert.Equal(
t,
map[string]string{
"field": "value",
"detail": "sentinelErr with detail",
},
GetErrorContext(gotErr),
)
assert.Equal(t, Validation, GetType(gotErr))
assert.EqualError(t, gotErr, "sentinelErr with detail: outer wrapped err 1: error 1: an_error")
assert.True(t, stderrors.Is(gotErr, sentinelErr))
standardSentinelErr := fmt.Errorf("sentinelErr")
gotErr = WithCause(standardSentinelErr, causeErr)
assert.Equal(t, map[string]string{"field": "value"}, GetErrorContext(gotErr))
assert.Equal(t, BadRequest, GetType(gotErr))
assert.EqualError(t, gotErr, "sentinelErr: outer wrapped err 1: error 1: an_error")
assert.True(t, stderrors.Is(gotErr, standardSentinelErr))
standardCauseErr := fmt.Errorf("an_error")
gotErr = WithCause(sentinelErr, standardCauseErr)
assert.Equal(t, map[string]string{"detail": "sentinelErr with detail"}, GetErrorContext(gotErr))
assert.Equal(t, Validation, GetType(gotErr))
assert.EqualError(t, gotErr, "sentinelErr with detail: an_error")
assert.True(t, stderrors.Is(gotErr, sentinelErr))
sentinel1 := fmt.Errorf("sentinel1")
sentinel2 := pkgerrors.Wrap(sentinel1, "sentinel2")
sentinel3 := fmt.Errorf("sentinel2")
gotErr = WithCause(sentinel3, sentinel2)
assert.True(t, stderrors.Is(gotErr, sentinel1))
assert.True(t, stderrors.Is(gotErr, sentinel2))
assert.True(t, stderrors.Is(gotErr, sentinel3))
}
func TestPointer(t *testing.T) {
err := fmt.Errorf("this is an error")
assert.Equal(t, "", Pointer(err))
err = WithPointer(err, "thefield")
assert.Equal(t, "thefield", Pointer(err))
}
func TestDetail(t *testing.T) {
err := fmt.Errorf("this is an error")
assert.Equal(t, "", Detail(err))
err = WithDetail(err, "the detail")
assert.Equal(t, "the detail", Detail(err))
}
func TestKey(t *testing.T) {
err := fmt.Errorf("this is an error")
assert.Equal(t, "", Key(err))
err = WithKey(err, "ERROR_NAME")
assert.Equal(t, "ERROR_NAME", Key(err))
}
func TestFailFast(t *testing.T) {
err := fmt.Errorf("this is an error")
assert.False(t, IsFailFast(err))
err = WithFailFast(err)
assert.True(t, IsFailFast(err))
}