Debian Codegraph @

fmt/errors/errors_test.go

raw symbols SCIP
1 // Copyright 2014 Roger Peppe.
2 // See LICENCE file for details.
3
4 package errors_test
5
6 import (
7 "fmt"
8 "io/ioutil"
9 "runtime"
10 "strings"
11 "testing"
12
13 gc "gopkg.in/check.v1"
14
15 "gopkg.in/errgo.v2/fmt/errors"
16 )
17
18 func Test(t *testing.T) {
19 gc.TestingT(t)
20 }
21
22 type errorsSuite struct{}
23
24 var _ = gc.Suite(&errorsSuite{})
25
26 func (*errorsSuite) TestNew(c *gc.C) {
27 err := errors.New("foo") //err TestNew
28 checkErr(c, err, nil, "foo", `[
29 {$TestNew$: foo}
30 ]`, err)
31 }
32
33 func (*errorsSuite) TestNewf(c *gc.C) {
34 err := errors.Newf("foo %d", 99) //err TestNewf
35 checkErr(c, err, nil, "foo 99", `[
36 {$TestNewf$: foo 99}
37 ]`, err)
38 }
39
40 var someErr = errors.New("some error") //err varSomeErr
41
42 func annotate1() error {
43 err := errors.Note(someErr, nil, "annotate1") //err annotate1
44 return err
45 }
46
47 func annotate2() error {
48 err := annotate1()
49 err = errors.Note(err, nil, "annotate2") //err annotate2
50 return err
51 }
52
53 func (*errorsSuite) TestNoteUsage(c *gc.C) {
54 err0 := annotate2()
55 err, ok := err0.(errors.Wrapper)
56 c.Assert(ok, gc.Equals, true)
57 underlying := err.Underlying()
58 checkErr(
59 c, err0, underlying,
60 "annotate2: annotate1: some error",
61 `[
62 {$annotate2$: annotate2}
63 {$annotate1$: annotate1}
64 {$varSomeErr$: some error}
65 ]`,
66 err0)
67 }
68
69 func (*errorsSuite) TestWrap(c *gc.C) {
70 err0 := errors.Because(nil, someErr, "foo") //err TestWrap#0
71 err := errors.Wrap(err0) //err TestWrap#1
72 checkErr(c, err, err0, "foo", `[
73 {$TestWrap#1$: }
74 {$TestWrap#0$: foo}
75 ]`, err)
76
77 err = errors.Wrap(nil)
78 c.Assert(err, gc.Equals, nil)
79 }
80
81 func (*errorsSuite) TestNoteWithNilError(c *gc.C) {
82 c.Assert(errors.Note(nil, nil, "annotation"), gc.Equals, nil)
83 }
84
85 func (*errorsSuite) TestNote(c *gc.C) {
86 err0 := errors.Because(nil, someErr, "foo") //err TestNote#0
87 err := errors.Note(err0, nil, "bar") //err TestNote#1
88 checkErr(c, err, err0, "bar: foo", `[
89 {$TestNote#1$: bar}
90 {$TestNote#0$: foo}
91 ]`, err)
92
93 err = errors.Note(err0, errors.Is(someErr), "bar") //err TestNote#2
94 checkErr(c, err, err0, "bar: foo", `[
95 {$TestNote#2$: bar}
96 {$TestNote#0$: foo}
97 ]`, someErr)
98
99 err = errors.Note(err0, func(error) bool { return false }, "") //err TestNote#3
100 checkErr(c, err, err0, "foo", `[
101 {$TestNote#3$: }
102 {$TestNote#0$: foo}
103 ]`, err)
104 }
105
106 func (*errorsSuite) TestNotef(c *gc.C) {
107 err0 := errors.Because(nil, someErr, "foo") //err TestNotef#0
108 err := errors.Notef(err0, nil, "bar %d", 99) //err TestNotef#1
109 checkErr(c, err, err0, "bar 99: foo", `[
110 {$TestNotef#1$: bar 99}
111 {$TestNotef#0$: foo}
112 ]`, err)
113 }
114
115 func (*errorsSuite) TestCause(c *gc.C) {
116 c.Assert(errors.Cause(someErr), gc.Equals, someErr)
117
118 causeErr := errors.New("cause error")
119 underlyingErr := errors.New("underlying error") //err TestCause#1
120 err := errors.Because(underlyingErr, causeErr, "foo 99") //err TestCause#2
121 c.Assert(errors.Cause(err), gc.Equals, causeErr)
122
123 checkErr(c, err, underlyingErr, "foo 99: underlying error", `[
124 {$TestCause#2$: foo 99}
125 {$TestCause#1$: underlying error}
126 ]`, causeErr)
127
128 err = customError{err}
129 c.Assert(errors.Cause(err), gc.Equals, causeErr)
130 }
131
132 func (*errorsSuite) TestBecausef(c *gc.C) {
133 c.Assert(errors.Cause(someErr), gc.Equals, someErr)
134
135 causeErr := errors.New("cause error")
136 underlyingErr := errors.New("underlying error") //err TestBecausef#1
137 err := errors.Becausef(underlyingErr, causeErr, "foo %d", 99) //err TestBecausef#2
138 c.Assert(errors.Cause(err), gc.Equals, causeErr)
139
140 checkErr(c, err, underlyingErr, "foo 99: underlying error", `[
141 {$TestBecausef#2$: foo 99}
142 {$TestBecausef#1$: underlying error}
143 ]`, causeErr)
144 }
145
146 func (*errorsSuite) TestBecauseWithNoMessage(c *gc.C) {
147 cause := errors.New("cause")
148 err := errors.Because(nil, cause, "")
149 c.Assert(err, gc.ErrorMatches, "cause")
150 c.Assert(errors.Cause(err), gc.Equals, cause)
151 }
152
153 func (*errorsSuite) TestBecauseWithUnderlyingButNoMessage(c *gc.C) {
154 err := errors.New("something")
155 cause := errors.New("cause")
156 err = errors.Because(err, cause, "")
157 c.Assert(err, gc.ErrorMatches, "something")
158 c.Assert(errors.Cause(err), gc.Equals, cause)
159 }
160
161 func (*errorsSuite) TestBecauseWithAllZeroArgs(c *gc.C) {
162 err := errors.Because(nil, nil, "")
163 c.Assert(err, gc.Equals, nil)
164 }
165
166 func (*errorsSuite) TestDetails(c *gc.C) {
167 c.Assert(errors.Details(nil), gc.Equals, "[]")
168
169 otherErr := fmt.Errorf("other")
170 checkErr(c, otherErr, nil, "other", `[
171 {other}
172 ]`, otherErr)
173
174 err0 := customError{errors.New("foo")} //err TestDetails#0
175 checkErr(c, err0, nil, "foo", `[
176 {$TestDetails#0$: foo}
177 ]`, err0)
178
179 err1 := customError{errors.Note(err0, nil, "bar")} //err TestDetails#1
180 checkErr(c, err1, err0, "bar: foo", `[
181 {$TestDetails#1$: bar}
182 {$TestDetails#0$: foo}
183 ]`, err1)
184
185 err2 := errors.Wrap(err1) //err TestDetails#2
186 checkErr(c, err2, err1, "bar: foo", `[
187 {$TestDetails#2$: }
188 {$TestDetails#1$: bar}
189 {$TestDetails#0$: foo}
190 ]`, err2)
191 }
192
193 func (*errorsSuite) TestSetLocation(c *gc.C) {
194 err := customNewError() //err TestSetLocation#0
195 checkErr(c, err, nil, "custom", `[
196 {$TestSetLocation#0$: custom}
197 ]`, err)
198 }
199
200 func customNewError() error {
201 err := errors.New("custom")
202 errors.SetLocation(err, 1)
203 return err
204 }
205
206 func checkErr(c *gc.C, err, underlying error, msg string, details string, cause error) {
207 c.Assert(err, gc.NotNil)
208 c.Assert(err.Error(), gc.Equals, msg)
209 if err, ok := err.(errors.Wrapper); ok {
210 c.Assert(err.Underlying(), gc.Equals, underlying)
211 } else {
212 c.Assert(underlying, gc.Equals, nil)
213 }
214 c.Assert(errors.Cause(err), gc.Equals, cause)
215 wantDetails := replaceLocations(details)
216 c.Assert(errors.Details(err), gc.Equals, wantDetails)
217 }
218
219 func replaceLocations(s string) string {
220 t := ""
221 for {
222 i := strings.Index(s, "$")
223 if i == -1 {
224 break
225 }
226 t += s[0:i]
227 s = s[i+1:]
228 i = strings.Index(s, "$")
229 if i == -1 {
230 panic("no second $")
231 }
232 file, line := location(s[0:i])
233 t += fmt.Sprintf("%s:%d", file, line)
234 s = s[i+1:]
235 }
236 t += s
237 return t
238 }
239
240 func location(tag string) (string, int) {
241 line, ok := tagToLine[tag]
242 if !ok {
243 panic(fmt.Errorf("tag %q not found", tag))
244 }
245 return filename, line
246 }
247
248 var tagToLine = make(map[string]int)
249 var filename string
250
251 func init() {
252 data, err := ioutil.ReadFile("errors_test.go")
253 if err != nil {
254 panic(err)
255 }
256 lines := strings.Split(string(data), "\n")
257 for i, line := range lines {
258 if j := strings.Index(line, "//err "); j >= 0 {
259 tagToLine[line[j+len("//err "):]] = i + 1
260 }
261 }
262 _, filename, _, _ = runtime.Caller(0)
263 }
264
265 type customError struct {
266 error
267 }
268
269 func (e customError) Location() (string, int) {
270 if err, ok := e.error.(errors.Locator); ok {
271 return err.Location()
272 }
273 return "", 0
274 }
275
276 func (e customError) Underlying() error {
277 if err, ok := e.error.(errors.Wrapper); ok {
278 return err.Underlying()
279 }
280 return nil
281 }
282
283 func (e customError) Message() string {
284 if err, ok := e.error.(errors.Wrapper); ok {
285 return err.Message()
286 }
287 return ""
288 }
289
290 func (e customError) Cause() error {
291 if err, ok := e.error.(errors.Causer); ok {
292 return err.Cause()
293 }
294 return nil
295 }