forked from juju/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions_test.go
375 lines (327 loc) · 10.6 KB
/
functions_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
// Copyright 2014 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package errors_test
import (
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
gc "gopkg.in/check.v1"
"github.com/juju/errors"
)
type functionSuite struct {
}
var _ = gc.Suite(&functionSuite{})
func (*functionSuite) TestNew(c *gc.C) {
err := errors.New("testing")
loc := errorLocationValue(c)
c.Assert(err.Error(), gc.Equals, "testing")
c.Assert(errors.Cause(err), gc.Equals, err)
c.Assert(errors.Details(err), Contains, loc)
}
func (*functionSuite) TestErrorf(c *gc.C) {
err := errors.Errorf("testing %d", 42)
loc := errorLocationValue(c)
c.Assert(err.Error(), gc.Equals, "testing 42")
c.Assert(errors.Cause(err), gc.Equals, err)
c.Assert(errors.Details(err), Contains, loc)
}
func (*functionSuite) TestTrace(c *gc.C) {
first := errors.New("first")
err := errors.Trace(first)
loc := errorLocationValue(c)
c.Assert(err.Error(), gc.Equals, "first")
c.Assert(errors.Cause(err), gc.Equals, first)
c.Assert(errors.Details(err), Contains, loc)
c.Assert(errors.Trace(nil), gc.IsNil)
}
func (*functionSuite) TestAnnotate(c *gc.C) {
first := errors.New("first")
err := errors.Annotate(first, "annotation")
loc := errorLocationValue(c)
c.Assert(err.Error(), gc.Equals, "annotation: first")
c.Assert(errors.Cause(err), gc.Equals, first)
c.Assert(errors.Details(err), Contains, loc)
c.Assert(errors.Annotate(nil, "annotate"), gc.IsNil)
}
func (*functionSuite) TestAnnotatef(c *gc.C) {
first := errors.New("first")
err := errors.Annotatef(first, "annotation %d", 2) //err annotatefTest
loc := errorLocationValue(c)
c.Assert(err.Error(), gc.Equals, "annotation 2: first")
c.Assert(errors.Cause(err), gc.Equals, first)
c.Assert(errors.Details(err), Contains, loc)
c.Assert(errors.Annotatef(nil, "annotate"), gc.IsNil)
}
func (*functionSuite) TestDeferredAnnotatef(c *gc.C) {
// NOTE: this test fails with gccgo
if runtime.Compiler == "gccgo" {
c.Skip("gccgo can't determine the location")
}
first := errors.New("first")
test := func() (err error) {
defer errors.DeferredAnnotatef(&err, "deferred %s", "annotate")
return first
}
err := test()
c.Assert(err.Error(), gc.Equals, "deferred annotate: first")
c.Assert(errors.Cause(err), gc.Equals, first)
err = nil
errors.DeferredAnnotatef(&err, "deferred %s", "annotate")
c.Assert(err, gc.IsNil)
}
func (*functionSuite) TestWrap(c *gc.C) {
first := errors.New("first")
firstLoc := errorLocationValue(c)
detailed := errors.New("detailed")
err := errors.Wrap(first, detailed)
secondLoc := errorLocationValue(c)
c.Assert(err.Error(), gc.Equals, "detailed")
c.Assert(errors.Cause(err), gc.Equals, detailed)
c.Assert(errors.Details(err), Contains, firstLoc)
c.Assert(errors.Details(err), Contains, secondLoc)
}
func (*functionSuite) TestWrapOfNil(c *gc.C) {
detailed := errors.New("detailed")
err := errors.Wrap(nil, detailed)
loc := errorLocationValue(c)
c.Assert(err.Error(), gc.Equals, "detailed")
c.Assert(errors.Cause(err), gc.Equals, detailed)
c.Assert(errors.Details(err), Contains, loc)
}
func (*functionSuite) TestWrapf(c *gc.C) {
first := errors.New("first")
firstLoc := errorLocationValue(c)
detailed := errors.New("detailed")
err := errors.Wrapf(first, detailed, "value %d", 42)
secondLoc := errorLocationValue(c)
c.Assert(err.Error(), gc.Equals, "value 42: detailed")
c.Assert(errors.Cause(err), gc.Equals, detailed)
c.Assert(errors.Details(err), Contains, firstLoc)
c.Assert(errors.Details(err), Contains, secondLoc)
}
func (*functionSuite) TestWrapfOfNil(c *gc.C) {
detailed := errors.New("detailed")
err := errors.Wrapf(nil, detailed, "value %d", 42)
loc := errorLocationValue(c)
c.Assert(err.Error(), gc.Equals, "value 42: detailed")
c.Assert(errors.Cause(err), gc.Equals, detailed)
c.Assert(errors.Details(err), Contains, loc)
}
func (*functionSuite) TestMask(c *gc.C) {
first := errors.New("first")
err := errors.Mask(first)
loc := errorLocationValue(c)
c.Assert(err.Error(), gc.Equals, "first")
c.Assert(errors.Cause(err), gc.Equals, err)
c.Assert(errors.Details(err), Contains, loc)
c.Assert(errors.Mask(nil), gc.IsNil)
}
func (*functionSuite) TestMaskf(c *gc.C) {
first := errors.New("first")
err := errors.Maskf(first, "masked %d", 42)
loc := errorLocationValue(c)
c.Assert(err.Error(), gc.Equals, "masked 42: first")
c.Assert(errors.Cause(err), gc.Equals, err)
c.Assert(errors.Details(err), Contains, loc)
c.Assert(errors.Maskf(nil, "mask"), gc.IsNil)
}
func (*functionSuite) TestCause(c *gc.C) {
c.Assert(errors.Cause(nil), gc.IsNil)
c.Assert(errors.Cause(someErr), gc.Equals, someErr)
fmtErr := fmt.Errorf("simple")
c.Assert(errors.Cause(fmtErr), gc.Equals, fmtErr)
err := errors.Wrap(someErr, fmtErr)
c.Assert(errors.Cause(err), gc.Equals, fmtErr)
err = errors.Annotate(err, "annotated")
c.Assert(errors.Cause(err), gc.Equals, fmtErr)
err = errors.Maskf(err, "masked")
c.Assert(errors.Cause(err), gc.Equals, err)
// Look for a file that we know isn't there.
dir := c.MkDir()
_, err = os.Stat(filepath.Join(dir, "not-there"))
c.Assert(os.IsNotExist(err), gc.Equals, true)
err = errors.Annotatef(err, "wrap it")
// Now the error itself isn't a 'IsNotExist'.
c.Assert(os.IsNotExist(err), gc.Equals, false)
// However if we use the Check method, it is.
c.Assert(os.IsNotExist(errors.Cause(err)), gc.Equals, true)
}
type tracer interface {
StackTrace() []string
}
func (*functionSuite) TestErrorStack(c *gc.C) {
for i, test := range []struct {
message string
generator func(*gc.C, io.Writer) error
tracer bool
}{{
message: "nil",
generator: func(_ *gc.C, _ io.Writer) error {
return nil
},
}, {
message: "raw error",
generator: func(c *gc.C, expected io.Writer) error {
fmt.Fprint(expected, "raw")
return fmt.Errorf("raw")
},
}, {
message: "single error stack",
generator: func(c *gc.C, expected io.Writer) error {
err := errors.New("first error")
fmt.Fprintf(expected, "%s: first error", errorLocationValue(c))
return err
},
tracer: true,
}, {
message: "annotated error",
generator: func(c *gc.C, expected io.Writer) error {
err := errors.New("first error")
fmt.Fprintf(expected, "%s: first error\n", errorLocationValue(c))
err = errors.Annotate(err, "annotation")
fmt.Fprintf(expected, "%s: annotation", errorLocationValue(c))
return err
},
tracer: true,
}, {
message: "wrapped error",
generator: func(c *gc.C, expected io.Writer) error {
err := errors.New("first error")
fmt.Fprintf(expected, "%s: first error\n", errorLocationValue(c))
err = errors.Wrap(err, newError("detailed error"))
fmt.Fprintf(expected, "%s: detailed error", errorLocationValue(c))
return err
},
tracer: true,
}, {
message: "annotated wrapped error",
generator: func(c *gc.C, expected io.Writer) error {
err := errors.Errorf("first error")
fmt.Fprintf(expected, "%s: first error\n", errorLocationValue(c))
err = errors.Wrap(err, fmt.Errorf("detailed error"))
fmt.Fprintf(expected, "%s: detailed error\n", errorLocationValue(c))
err = errors.Annotatef(err, "annotated")
fmt.Fprintf(expected, "%s: annotated", errorLocationValue(c))
return err
},
tracer: true,
}, {
message: "traced, and annotated",
generator: func(c *gc.C, expected io.Writer) error {
err := errors.New("first error")
fmt.Fprintf(expected, "%s: first error\n", errorLocationValue(c))
err = errors.Trace(err)
fmt.Fprintf(expected, "%s: \n", errorLocationValue(c))
err = errors.Annotate(err, "some context")
fmt.Fprintf(expected, "%s: some context\n", errorLocationValue(c))
err = errors.Trace(err)
fmt.Fprintf(expected, "%s: \n", errorLocationValue(c))
err = errors.Annotate(err, "more context")
fmt.Fprintf(expected, "%s: more context\n", errorLocationValue(c))
err = errors.Trace(err)
fmt.Fprintf(expected, "%s: ", errorLocationValue(c))
return err
},
tracer: true,
}, {
message: "uncomparable, wrapped with a value error",
generator: func(c *gc.C, expected io.Writer) error {
err := newNonComparableError("first error")
fmt.Fprintln(expected, "first error")
err = errors.Trace(err)
fmt.Fprintf(expected, "%s: \n", errorLocationValue(c))
err = errors.Wrap(err, newError("value error"))
fmt.Fprintf(expected, "%s: value error\n", errorLocationValue(c))
err = errors.Maskf(err, "masked")
fmt.Fprintf(expected, "%s: masked\n", errorLocationValue(c))
err = errors.Annotate(err, "more context")
fmt.Fprintf(expected, "%s: more context\n", errorLocationValue(c))
err = errors.Trace(err)
fmt.Fprintf(expected, "%s: ", errorLocationValue(c))
return err
},
tracer: true,
}} {
c.Logf("%v: %s", i, test.message)
expected := strings.Builder{}
err := test.generator(c, &expected)
stack := errors.ErrorStack(err)
ok := c.Check(stack, gc.Equals, expected.String())
if !ok {
c.Logf("%#v", err)
}
tracer, ok := err.(tracer)
c.Check(ok, gc.Equals, test.tracer)
if ok {
stackTrace := tracer.StackTrace()
c.Check(stackTrace, gc.DeepEquals, strings.Split(stack, "\n"))
}
}
}
func (*functionSuite) TestFormat(c *gc.C) {
formatErrorExpected := &strings.Builder{}
err := errors.New("TestFormat")
fmt.Fprintf(formatErrorExpected, "%s: TestFormat\n", errorLocationValue(c))
err = errors.Mask(err)
fmt.Fprintf(formatErrorExpected, "%s: ", errorLocationValue(c))
for i, test := range []struct {
format string
expect string
}{{
format: "%s",
expect: "TestFormat",
}, {
format: "%v",
expect: "TestFormat",
}, {
format: "%q",
expect: `"TestFormat"`,
}, {
format: "%A",
expect: `%!A(*errors.Err=TestFormat)`,
}, {
format: "%+v",
expect: formatErrorExpected.String(),
}} {
c.Logf("test %d: %q", i, test.format)
s := fmt.Sprintf(test.format, err)
c.Check(s, gc.Equals, test.expect)
}
}
type basicError struct {
Reason string
}
func (b *basicError) Error() string {
return b.Reason
}
func (*functionSuite) TestAs(c *gc.C) {
baseError := &basicError{"I'm an error"}
testErrors := []error{
errors.Trace(baseError),
errors.Annotate(baseError, "annotation"),
errors.Wrap(baseError, errors.New("wrapper")),
errors.Mask(baseError),
}
for _, err := range testErrors {
bError := &basicError{}
val := errors.As(err, &bError)
c.Check(val, gc.Equals, true)
c.Check(bError.Reason, gc.Equals, "I'm an error")
}
}
func (*functionSuite) TestIs(c *gc.C) {
baseError := &basicError{"I'm an error"}
testErrors := []error{
errors.Trace(baseError),
errors.Annotate(baseError, "annotation"),
errors.Wrap(baseError, errors.New("wrapper")),
errors.Mask(baseError),
}
for _, err := range testErrors {
val := errors.Is(err, baseError)
c.Check(val, gc.Equals, true)
}
}