-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkerrors_test.go
326 lines (279 loc) · 7.2 KB
/
kerrors_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
package kerrors
import (
"encoding/json"
"errors"
"regexp"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
type (
testErr struct{}
)
func (e testErr) Error() string {
return "test struct err"
}
func searchMapKey(data any, key string) map[string]any {
switch v := data.(type) {
case map[string]any:
if _, ok := v[key]; ok {
return v
}
for _, val := range v {
if result := searchMapKey(val, key); result != nil {
return result
}
}
case []any:
for _, val := range v {
if result := searchMapKey(val, key); result != nil {
return result
}
}
}
return nil
}
func TestError(t *testing.T) {
t.Parallel()
errorsErr := errors.New("test errors err")
nestedErr := WithKind(WithMsg(errorsErr, "another message"), testErr{}, "test error message")
for _, tc := range []struct {
Test string
Opts []ErrorOpt
Kind error
ErrMsg any
}{
{
Test: "produces an error with an errors kind and message",
Opts: []ErrorOpt{OptMsg("test message 123"), OptKind(errorsErr)},
Kind: errorsErr,
ErrMsg: map[string]any{
"msg": "test message 123",
"kind": "test errors err",
"cause": map[string]any{
"msg": "Stack trace",
},
},
},
{
Test: "produces an error with an error struct kind and message",
Opts: []ErrorOpt{OptMsg("test message 321"), OptKind(testErr{})},
Kind: testErr{},
ErrMsg: map[string]any{
"msg": "test message 321",
"kind": "test struct err",
"cause": map[string]any{
"msg": "Stack trace",
},
},
},
{
Test: "produces an error with a deeply nested error",
Opts: []ErrorOpt{OptMsg("test message 654"), OptKind(errors.New("other error")), OptCause(nestedErr)},
Kind: testErr{},
ErrMsg: map[string]any{
"msg": "test message 654",
"kind": "other error",
"cause": map[string]any{
"msg": "test error message",
"kind": "test struct err",
"cause": map[string]any{
"msg": "another message",
"cause": map[string]any{
"msg": "Stack trace",
"cause": "test errors err",
},
},
},
},
},
{
Test: "ignores kind if not provided",
Opts: []ErrorOpt{OptMsg("test message 654"), OptCause(nestedErr)},
Kind: testErr{},
ErrMsg: map[string]any{
"msg": "test message 654",
"cause": map[string]any{
"msg": "test error message",
"kind": "test struct err",
"cause": map[string]any{
"msg": "another message",
"cause": map[string]any{
"msg": "Stack trace",
"cause": "test errors err",
},
},
},
},
},
{
Test: "finds error kinds through stack traces",
Opts: []ErrorOpt{OptMsg("test message 654"), OptCause(nestedErr)},
Kind: errorsErr,
ErrMsg: map[string]any{
"msg": "test message 654",
"cause": map[string]any{
"msg": "test error message",
"kind": "test struct err",
"cause": map[string]any{
"msg": "another message",
"cause": map[string]any{
"msg": "Stack trace",
"cause": "test errors err",
},
},
},
},
},
{
Test: "handles errors unwrap",
Opts: []ErrorOpt{OptMsg("test unwrap"), OptCause(&testUnwrapError{wrapped: []error{&testSingleUnwrapError{wrapped: errorsErr}}})},
Kind: errorsErr,
ErrMsg: map[string]any{
"msg": "test unwrap",
"cause": map[string]any{
"msg": "Stack trace",
"cause": map[string]any{
"msg": "Test unwrap error",
"cause": []any{
map[string]any{
"msg": "Test single unwrap error",
"cause": "test errors err",
},
},
},
},
},
},
} {
t.Run(tc.Test, func(t *testing.T) {
t.Parallel()
assert := require.New(t)
err := New(tc.Opts...)
assert.Error(err)
b, jerr := json.Marshal(JSONValue(err))
assert.NoError(jerr)
var errMsg map[string]any
assert.NoError(json.Unmarshal(b, &errMsg))
stackTrace := searchMapKey(errMsg, "stack")
assert.NotNil(stackTrace)
stack, ok := stackTrace["stack"].([]any)
assert.True(ok)
assert.NotNil(stack)
assert.Contains(stack[0].(map[string]any)["file"], "xorkevin.dev/kerrors/kerrors_test.go")
assert.Contains(stack[0].(map[string]any)["fn"], "xorkevin.dev/kerrors.TestError")
delete(stackTrace, "stack")
assert.Equal(tc.ErrMsg, errMsg)
assert.ErrorIs(err, tc.Kind)
kerr, ok := Find[*Error](err)
assert.True(ok)
assert.True(kerr.Cause() == kerr.wrapped[1])
assert.True(kerr.Kind() == kerr.wrapped[0])
assert.True(kerr.Error() == kerr.message)
_, ok = Find[StackStringer](err)
assert.True(ok)
_, ok = Find[*StackTrace](err)
assert.True(ok)
})
}
}
func TestStackTrace(t *testing.T) {
t.Parallel()
stackRegex := regexp.MustCompile(`^(?:\S+ \S+:\d+)(?:\n\S+ \S+:\d+)*$`)
stackMsgRegex := regexp.MustCompile(`^Stack trace \(\S+ \S+:\d+\)$`)
t.Run("Error", func(t *testing.T) {
t.Parallel()
assert := require.New(t)
st := NewStackTrace(nil, 0)
stackerrmsg := st.Error()
assert.Regexp(stackMsgRegex, stackerrmsg)
assert.Contains(stackerrmsg, "xorkevin.dev/kerrors/kerrors_test.go")
assert.Contains(stackerrmsg, "xorkevin.dev/kerrors.TestStackTrace")
})
t.Run("StackString", func(t *testing.T) {
t.Parallel()
assert := require.New(t)
st := NewStackTrace(nil, 0)
stackstr := st.StackString()
assert.Regexp(stackRegex, stackstr)
assert.Contains(stackstr, "xorkevin.dev/kerrors/kerrors_test.go")
assert.True(strings.HasPrefix(stackstr, "xorkevin.dev/kerrors.TestStackTrace"))
})
t.Run("empty stack", func(t *testing.T) {
t.Parallel()
assert := require.New(t)
st := NewStackTrace(nil, 8)
assert.Equal("Stack trace (empty)", st.Error())
assert.Equal("", st.StackString())
})
t.Run("As", func(t *testing.T) {
t.Parallel()
assert := require.New(t)
st := NewStackTrace(nil, 0)
stackstringer, ok := Find[StackStringer](st)
assert.True(ok)
assert.True(stackstringer == st)
})
}
type (
testBaseError struct{}
testUnwrapError struct {
wrapped []error
}
testSingleUnwrapError struct {
wrapped error
}
testAsError struct{}
)
func (e *testBaseError) Error() string {
return "Test base error"
}
func (e *testUnwrapError) Error() string {
return "Test unwrap error"
}
func (e *testUnwrapError) Unwrap() []error {
return e.wrapped
}
func (e *testSingleUnwrapError) Error() string {
return "Test single unwrap error"
}
func (e *testSingleUnwrapError) Unwrap() error {
return e.wrapped
}
func (e *testAsError) Error() string {
return "Test as error"
}
func (e *testAsError) As(t any) bool {
if k, ok := t.(**testBaseError); ok {
*k = &testBaseError{}
return true
}
return false
}
func TestFind(t *testing.T) {
t.Parallel()
t.Run("nil error", func(t *testing.T) {
t.Parallel()
assert := require.New(t)
err, ok := Find[*testBaseError](nil)
assert.False(ok)
assert.True(err == nil)
})
t.Run("single unwrap error", func(t *testing.T) {
t.Parallel()
assert := require.New(t)
errorsErr := &testBaseError{}
nestedErr := &testSingleUnwrapError{wrapped: errorsErr}
err, ok := Find[*testBaseError](nestedErr)
assert.True(ok)
assert.True(err == errorsErr)
})
t.Run("as error", func(t *testing.T) {
t.Parallel()
assert := require.New(t)
asErr := &testAsError{}
err, ok := Find[*testBaseError](asErr)
assert.True(ok)
assert.NotNil(err)
})
}