forked from jjeffery/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_test.go
380 lines (354 loc) · 7.88 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
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
378
379
380
package errors
import (
"encoding"
"fmt"
"io"
"reflect"
"testing"
"time"
)
func TestNew(t *testing.T) {
tests := []struct {
msg string
opts []interface{}
expect string
}{
{
msg: "",
opts: nil,
expect: "",
},
{
msg: "xx",
opts: nil,
expect: "xx",
},
{
msg: "xx",
opts: []interface{}{
"key1", "val1",
"key2", 2,
},
expect: "xx key1=val1 key2=2",
},
{
msg: "msg",
opts: []interface{}{
"key", time.Time{},
},
expect: `msg key="0001-01-01T00:00:00Z"`,
},
{
msg: "msg",
opts: []interface{}{
"nil1", nil,
"nil2", []byte(nil),
},
expect: "msg nil1=null nil2=null",
},
{
msg: "msg",
opts: []interface{}{
"b1", []byte("noquotes"),
"b2", []byte("needs quotes"),
"b3", []byte("needs \\ \"escaped quotes\""),
},
expect: `msg b1=noquotes b2="needs quotes" b3="needs \\ \"escaped quotes\""`,
},
{
msg: "msg",
opts: []interface{}{
"s1", "noquotes",
"s2", "needs quotes",
"s3", "needs \\ \"escaped quotes\"",
},
expect: `msg s1=noquotes s2="needs quotes" s3="needs \\ \"escaped quotes\""`,
},
{
msg: "msg",
opts: []interface{}{
"f1", failingTextMarshaler(0),
},
expect: `msg f1=ERROR`,
},
{
msg: "msg",
opts: []interface{}{
"e1", fmt.Errorf("this failed"),
},
expect: `msg e1="this failed"`,
},
{
msg: "msg",
opts: []interface{}{
"s1", testStringer("I'm a Stringer"),
},
expect: `msg s1="Stringer: I'm a Stringer"`,
},
{
msg: "msg",
opts: []interface{}{
"p1", func() *int { n := 0; return &n }(),
"p2", func() *int32 { return nil }(),
"p3", func() *int64 { var n int64 = 3; return &n }(),
},
expect: `msg p1=0 p2=null p3=3`,
},
{
msg: "msg",
opts: []interface{}{
"x1", struct{ v int }{},
},
expect: `msg x1="{0}"`,
},
{
msg: "msg",
opts: []interface{}{
"p1", panicingStringer("I can't do this"),
},
expect: `msg p1=PANIC`,
},
}
for i, tt := range tests {
got := New(tt.msg).With(tt.opts...)
if got.Error() != tt.expect {
t.Errorf("%d: New.Error(): got: %q, want %q", i, got, tt.expect)
}
}
}
func TestWrapNil(t *testing.T) {
got := Wrap(nil, "no error")
if got != nil {
t.Errorf("Wrap(nil, \"no error\"): got %#v, expected nil", got)
}
}
type nilError struct{}
func (nilError) Error() string { return "nil error" }
type nilCauseError struct{}
func (e *nilCauseError) Error() string { return "nil cause error" }
func (e *nilCauseError) Cause() error { return nil }
var nilCauseErrorVal = &nilCauseError{}
func TestCause(t *testing.T) {
x := New("error")
tests := []struct {
err error
want error
}{
{
// nil error is nil
err: nil,
want: nil,
}, {
// explicit nil error is nil
err: (error)(nil),
want: nil,
}, {
// typed nil is nil
err: (*nilError)(nil),
want: (*nilError)(nil),
}, {
// uncaused error is unaffected
err: io.EOF,
want: io.EOF,
}, {
// caused error returns cause
err: Wrap(io.EOF, "ignored"),
want: io.EOF,
}, {
err: x, // return from errors.New
want: x,
}, {
// when the error implements the causer interface,
// but Cause() returns nil, we want to return
// the error, not the cause
err: nilCauseErrorVal,
want: nilCauseErrorVal,
},
}
for i, tt := range tests {
got := Cause(tt.err)
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("test %d: got %#v, want %#v", i+1, got, tt.want)
}
}
}
func TestAttachNil(t *testing.T) {
got := Wrap(nil, "")
if got != nil {
t.Errorf("Attach(nil, \"no error\"): got %#v, expected nil", got)
}
}
func TestAttach(t *testing.T) {
tests := []struct {
cause error
opts []interface{}
expectedMsg string
expectedCause error
expectedErr error
}{
{
// this test case tests that when no options are passed, the
// original error is returned
cause: io.EOF,
opts: nil,
expectedMsg: "EOF",
expectedCause: io.EOF,
expectedErr: io.EOF,
},
{
cause: io.EOF,
opts: []interface{}{"k1", "v1", "k2", "v2"},
expectedMsg: "EOF k1=v1 k2=v2",
expectedCause: io.EOF,
},
{
cause: Wrap(io.EOF, "something failed").With("k3", "v3"),
opts: []interface{}{"k1", "v1", "k2", "v2"},
expectedMsg: "something failed k3=v3: EOF k1=v1 k2=v2",
expectedCause: io.EOF,
},
}
for i, tt := range tests {
err := Wrap(tt.cause, "").With(tt.opts...)
actualMsg := err.Error()
if actualMsg != tt.expectedMsg {
t.Errorf("%d: expected=%q, actual=%q", i, tt.expectedMsg, actualMsg)
}
actualCause := Cause(err)
if actualCause != tt.expectedCause {
t.Errorf("%d: cause: expected=%v, actual=%v", i, tt.expectedCause, actualCause)
}
// only test if non-nil in the test case
if tt.expectedErr != nil {
if tt.expectedErr.Error() != err.Error() {
t.Errorf("%d: error: expected=%v, actual=%v", i, tt.expectedErr, err)
}
}
}
}
func TestKeyvals(t *testing.T) {
tests := []struct {
err error
errKeyvals []interface{}
ctx Context
ctxKeyvals []interface{}
}{
{
err: New("message"),
errKeyvals: []interface{}{"msg", "message"},
},
{
err: New("message").With("k1", "v1", "k2", 2),
errKeyvals: []interface{}{"msg", "message", "k1", "v1", "k2", 2},
},
{
err: Wrap(io.EOF, "message"),
errKeyvals: []interface{}{"msg", "message", "cause", "EOF"},
},
{
err: Wrap(io.EOF, "message").With(),
errKeyvals: []interface{}{"msg", "message", "cause", "EOF"},
},
{
err: Wrap(io.EOF, "message").With("k1", "v1", "k2", 2),
errKeyvals: []interface{}{"msg", "message", "k1", "v1", "k2", 2, "cause", "EOF"},
},
{
err: Wrap(io.EOF, "").With("k1", "v1", "k2", 2),
errKeyvals: []interface{}{"msg", "EOF", "k1", "v1", "k2", 2},
},
{
ctx: With(),
ctxKeyvals: nil,
},
{
ctx: With("k1", "v1", "k2", 2),
ctxKeyvals: []interface{}{"k1", "v1", "k2", 2},
},
{
ctx: With("k1", "v1", "k2", 2).With("k3", 3),
ctxKeyvals: []interface{}{"k1", "v1", "k2", 2, "k3", 3},
},
}
type keyvalser interface {
Keyvals() []interface{}
}
for i, tt := range tests {
if tt.err != nil {
keyvals, ok := tt.err.(keyvalser)
if !ok {
t.Errorf("%d: expected Keyvals(), none available", i)
continue
}
kvs := keyvals.Keyvals()
if !reflect.DeepEqual(tt.errKeyvals, kvs) {
t.Errorf("%d: expected %v, actual %v", i, tt.errKeyvals, kvs)
}
}
if tt.ctx != nil {
keyvals, ok := tt.ctx.(keyvalser)
if !ok {
t.Errorf("%d: expected Keyvals(), none available", i)
continue
}
kvs := keyvals.Keyvals()
if !reflect.DeepEqual(tt.ctxKeyvals, kvs) {
t.Errorf("%d: expected %v, actual %v", i, tt.ctxKeyvals, kvs)
}
}
}
}
func TestMarshalText(t *testing.T) {
tests := []struct {
err error
text string
}{
{
err: New("error message"),
text: "error message",
},
{
err: Wrap(io.EOF, "error message"),
text: "error message: EOF",
},
{
err: Wrap(io.EOF, ""),
text: "EOF",
},
{
err: New("error message").With("a", 1),
text: "error message a=1",
},
{
err: Wrap(io.EOF, "error message").With("b2", "b2"),
text: "error message b2=b2: EOF",
},
{
err: Wrap(io.EOF, "").With("c3", 3),
text: "EOF c3=3",
},
}
for i, tt := range tests {
m := tt.err.(encoding.TextMarshaler)
b, err := m.MarshalText()
if err != nil {
t.Errorf("%d: want no error, got %v", i, err)
continue
}
if want, got := tt.text, string(b); want != got {
t.Errorf("%d: want %q, got %q", i, want, got)
}
}
}
type failingTextMarshaler int
func (m failingTextMarshaler) MarshalText() ([]byte, error) {
return nil, fmt.Errorf("cannot marshal text")
}
type testStringer string
func (s testStringer) String() string {
return "Stringer: " + string(s)
}
type panicingStringer string
func (s panicingStringer) String() string {
panic(s)
}