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