| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package fmt_test |
| 6 |
|
| 7 |
import ( |
| 8 |
gofmt "fmt" |
| 9 |
"io" |
| 10 |
"os" |
| 11 |
"path" |
| 12 |
"reflect" |
| 13 |
"regexp" |
| 14 |
"strconv" |
| 15 |
"strings" |
| 16 |
"testing" |
| 17 |
|
| 18 |
"golang.org/x/exp/errors" |
| 19 |
"golang.org/x/exp/errors/fmt" |
| 20 |
) |
| 21 |
|
| 22 |
func TestErrorf(t *testing.T) { |
| 23 |
chained := &wrapped{"chained", nil} |
| 24 |
chain := func(s ...string) (a []string) { |
| 25 |
for _, s := range s { |
| 26 |
a = append(a, cleanPath(s)) |
| 27 |
} |
| 28 |
return a |
| 29 |
} |
| 30 |
testCases := []struct { |
| 31 |
got error |
| 32 |
want []string |
| 33 |
}{{ |
| 34 |
fmt.Errorf("no args"), |
| 35 |
chain("no args/path.TestErrorf/path.go:xxx"), |
| 36 |
}, { |
| 37 |
fmt.Errorf("no args: %s"), |
| 38 |
chain("no args: %!s(MISSING)/path.TestErrorf/path.go:xxx"), |
| 39 |
}, { |
| 40 |
fmt.Errorf("nounwrap: %s", "simple"), |
| 41 |
chain(`nounwrap: simple/path.TestErrorf/path.go:xxx`), |
| 42 |
}, { |
| 43 |
fmt.Errorf("nounwrap: %v", "simple"), |
| 44 |
chain(`nounwrap: simple/path.TestErrorf/path.go:xxx`), |
| 45 |
}, { |
| 46 |
fmt.Errorf("%s failed: %v", "foo", chained), |
| 47 |
chain("foo failed/path.TestErrorf/path.go:xxx", |
| 48 |
"chained/somefile.go:xxx"), |
| 49 |
}, { |
| 50 |
fmt.Errorf("no wrap: %s", chained), |
| 51 |
chain("no wrap/path.TestErrorf/path.go:xxx", |
| 52 |
"chained/somefile.go:xxx"), |
| 53 |
}, { |
| 54 |
fmt.Errorf("%s failed: %w", "foo", chained), |
| 55 |
chain("wraps:foo failed/path.TestErrorf/path.go:xxx", |
| 56 |
"chained/somefile.go:xxx"), |
| 57 |
}, { |
| 58 |
fmt.Errorf("nowrapv: %v", chained), |
| 59 |
chain("nowrapv/path.TestErrorf/path.go:xxx", |
| 60 |
"chained/somefile.go:xxx"), |
| 61 |
}, { |
| 62 |
fmt.Errorf("wrapw: %w", chained), |
| 63 |
chain("wraps:wrapw/path.TestErrorf/path.go:xxx", |
| 64 |
"chained/somefile.go:xxx"), |
| 65 |
}, { |
| 66 |
fmt.Errorf("not wrapped: %+v", chained), |
| 67 |
chain("not wrapped: chained: somefile.go:123/path.TestErrorf/path.go:xxx"), |
| 68 |
}} |
| 69 |
for i, tc := range testCases { |
| 70 |
t.Run(strconv.Itoa(i)+"/"+path.Join(tc.want...), func(t *testing.T) { |
| 71 |
got := errToParts(tc.got) |
| 72 |
if !reflect.DeepEqual(got, tc.want) { |
| 73 |
t.Errorf("Format:\n got: %#v\nwant: %#v", got, tc.want) |
| 74 |
} |
| 75 |
|
| 76 |
gotStr := tc.got.Error() |
| 77 |
wantStr := fmt.Sprint(tc.got) |
| 78 |
if gotStr != wantStr { |
| 79 |
t.Errorf("Error:\n got: %#v\nwant: %#v", got, tc.want) |
| 80 |
} |
| 81 |
}) |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
func TestErrorFormatter(t *testing.T) { |
| 86 |
var ( |
| 87 |
simple = &wrapped{"simple", nil} |
| 88 |
elephant = &wrapped{ |
| 89 |
"can't adumbrate elephant", |
| 90 |
detailed{}, |
| 91 |
} |
| 92 |
nonascii = &wrapped{"café", nil} |
| 93 |
newline = &wrapped{"msg with\nnewline", |
| 94 |
&wrapped{"and another\none", nil}} |
| 95 |
fallback = &wrapped{"fallback", os.ErrNotExist} |
| 96 |
oldAndNew = &wrapped{"new style", formatError("old style")} |
| 97 |
framed = &withFrameAndMore{ |
| 98 |
frame: errors.Caller(0), |
| 99 |
} |
| 100 |
opaque = &wrapped{"outer", |
| 101 |
errors.Opaque(&wrapped{"mid", |
| 102 |
&wrapped{"inner", nil}})} |
| 103 |
) |
| 104 |
testCases := []struct { |
| 105 |
err error |
| 106 |
fmt string |
| 107 |
want string |
| 108 |
regexp bool |
| 109 |
}{{ |
| 110 |
err: simple, |
| 111 |
fmt: "%s", |
| 112 |
want: "simple", |
| 113 |
}, { |
| 114 |
err: elephant, |
| 115 |
fmt: "%s", |
| 116 |
want: "can't adumbrate elephant: out of peanuts", |
| 117 |
}, { |
| 118 |
err: &wrapped{"a", &wrapped{"b", &wrapped{"c", nil}}}, |
| 119 |
fmt: "%s", |
| 120 |
want: "a: b: c", |
| 121 |
}, { |
| 122 |
err: simple, |
| 123 |
fmt: "%+v", |
| 124 |
want: "simple:" + |
| 125 |
"\n somefile.go:123", |
| 126 |
}, { |
| 127 |
err: elephant, |
| 128 |
fmt: "%+v", |
| 129 |
want: "can't adumbrate elephant:" + |
| 130 |
"\n somefile.go:123" + |
| 131 |
"\n - out of peanuts:" + |
| 132 |
"\n the elephant is on strike" + |
| 133 |
"\n and the 12 monkeys" + |
| 134 |
"\n are laughing", |
| 135 |
}, { |
| 136 |
err: simple, |
| 137 |
fmt: "%#v", |
| 138 |
want: "&fmt_test.wrapped{msg:\"simple\", err:error(nil)}", |
| 139 |
}, { |
| 140 |
err: framed, |
| 141 |
fmt: "%+v", |
| 142 |
want: "something:" + |
| 143 |
"\n golang.org/x/exp/errors/fmt_test.TestErrorFormatter" + |
| 144 |
"\n .+/fmt/errors_test.go:98" + |
| 145 |
"\n something more", |
| 146 |
regexp: true, |
| 147 |
}, { |
| 148 |
err: fmtTwice("Hello World!"), |
| 149 |
fmt: "%#v", |
| 150 |
want: "2 times Hello World!", |
| 151 |
}, { |
| 152 |
err: fallback, |
| 153 |
fmt: "%s", |
| 154 |
want: "fallback: file does not exist", |
| 155 |
}, { |
| 156 |
err: fallback, |
| 157 |
fmt: "%+v", |
| 158 |
|
| 159 |
want: "fallback:" + |
| 160 |
"\n somefile.go:123" + |
| 161 |
"\n - file does not exist", |
| 162 |
}, { |
| 163 |
err: opaque, |
| 164 |
fmt: "%s", |
| 165 |
want: "outer: mid: inner", |
| 166 |
}, { |
| 167 |
err: opaque, |
| 168 |
fmt: "%+v", |
| 169 |
want: "outer:" + |
| 170 |
"\n somefile.go:123" + |
| 171 |
"\n - mid:" + |
| 172 |
"\n somefile.go:123" + |
| 173 |
"\n - inner:" + |
| 174 |
"\n somefile.go:123", |
| 175 |
}, { |
| 176 |
err: oldAndNew, |
| 177 |
fmt: "%v", |
| 178 |
want: "new style: old style", |
| 179 |
}, { |
| 180 |
err: oldAndNew, |
| 181 |
fmt: "%q", |
| 182 |
want: `"new style: old style"`, |
| 183 |
}, { |
| 184 |
err: oldAndNew, |
| 185 |
fmt: "%+v", |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
want: "new style:" + |
| 190 |
"\n somefile.go:123" + |
| 191 |
"\n - old style:" + |
| 192 |
"\n otherfile.go:456", |
| 193 |
}, { |
| 194 |
err: simple, |
| 195 |
fmt: "%-12s", |
| 196 |
want: "simple ", |
| 197 |
}, { |
| 198 |
|
| 199 |
err: simple, |
| 200 |
fmt: "%+12v", |
| 201 |
want: "simple:" + |
| 202 |
"\n somefile.go:123", |
| 203 |
}, { |
| 204 |
err: elephant, |
| 205 |
fmt: "%+50s", |
| 206 |
want: " can't adumbrate elephant: out of peanuts", |
| 207 |
}, { |
| 208 |
err: nonascii, |
| 209 |
fmt: "%q", |
| 210 |
want: `"café"`, |
| 211 |
}, { |
| 212 |
err: nonascii, |
| 213 |
fmt: "%+q", |
| 214 |
want: `"caf\u00e9"`, |
| 215 |
}, { |
| 216 |
err: simple, |
| 217 |
fmt: "% x", |
| 218 |
want: "73 69 6d 70 6c 65", |
| 219 |
}, { |
| 220 |
err: newline, |
| 221 |
fmt: "%s", |
| 222 |
want: "msg with" + |
| 223 |
"\nnewline: and another" + |
| 224 |
"\none", |
| 225 |
}, { |
| 226 |
err: newline, |
| 227 |
fmt: "%+v", |
| 228 |
want: "msg with" + |
| 229 |
"\n newline:" + |
| 230 |
"\n somefile.go:123" + |
| 231 |
"\n - and another" + |
| 232 |
"\n one:" + |
| 233 |
"\n somefile.go:123", |
| 234 |
}, { |
| 235 |
err: wrapped{"", wrapped{"inner message", nil}}, |
| 236 |
fmt: "%+v", |
| 237 |
want: "somefile.go:123" + |
| 238 |
"\n - inner message:" + |
| 239 |
"\n somefile.go:123", |
| 240 |
}, { |
| 241 |
err: spurious(""), |
| 242 |
fmt: "%s", |
| 243 |
want: "spurious", |
| 244 |
}, { |
| 245 |
err: spurious(""), |
| 246 |
fmt: "%+v", |
| 247 |
want: "spurious", |
| 248 |
}, { |
| 249 |
err: spurious("extra"), |
| 250 |
fmt: "%s", |
| 251 |
want: "spurious", |
| 252 |
}, { |
| 253 |
err: spurious("extra"), |
| 254 |
fmt: "%+v", |
| 255 |
want: "spurious:\n" + |
| 256 |
" extra", |
| 257 |
}, { |
| 258 |
err: nil, |
| 259 |
fmt: "%+v", |
| 260 |
want: "<nil>", |
| 261 |
}, { |
| 262 |
err: (*wrapped)(nil), |
| 263 |
fmt: "%+v", |
| 264 |
want: "<nil>", |
| 265 |
}, { |
| 266 |
err: simple, |
| 267 |
fmt: "%T", |
| 268 |
want: "*fmt_test.wrapped", |
| 269 |
}, { |
| 270 |
err: simple, |
| 271 |
fmt: "%🤪", |
| 272 |
want: "%!🤪(*fmt_test.wrapped=&{simple <nil>})", |
| 273 |
}, { |
| 274 |
err: formatError("use fmt.Formatter"), |
| 275 |
fmt: "%#v", |
| 276 |
want: "use fmt.Formatter", |
| 277 |
}, { |
| 278 |
err: wrapped{"using errors.Formatter", |
| 279 |
formatError("use fmt.Formatter")}, |
| 280 |
fmt: "%#v", |
| 281 |
want: "fmt_test.wrapped{msg:\"using errors.Formatter\", err:\"use fmt.Formatter\"}", |
| 282 |
}, { |
| 283 |
err: fmtTwice("%s %s", "ok", panicValue{}), |
| 284 |
fmt: "%s", |
| 285 |
want: "ok %!s(PANIC=panic)/ok %!s(PANIC=panic)", |
| 286 |
}, { |
| 287 |
err: fmtTwice("%o %s", panicValue{}, "ok"), |
| 288 |
fmt: "%s", |
| 289 |
want: "{} ok/{} ok", |
| 290 |
}, { |
| 291 |
err: adapted{"adapted", nil}, |
| 292 |
fmt: "%+v", |
| 293 |
want: "adapted:" + |
| 294 |
"\n detail", |
| 295 |
}, { |
| 296 |
err: adapted{"outer", adapted{"mid", adapted{"inner", nil}}}, |
| 297 |
fmt: "%+v", |
| 298 |
want: "outer:" + |
| 299 |
"\n detail" + |
| 300 |
"\n - mid:" + |
| 301 |
"\n detail" + |
| 302 |
"\n - inner:" + |
| 303 |
"\n detail", |
| 304 |
}} |
| 305 |
for i, tc := range testCases { |
| 306 |
t.Run(fmt.Sprintf("%d/%s", i, tc.fmt), func(t *testing.T) { |
| 307 |
got := fmt.Sprintf(tc.fmt, tc.err) |
| 308 |
var ok bool |
| 309 |
if tc.regexp { |
| 310 |
var err error |
| 311 |
ok, err = regexp.MatchString(tc.want, got) |
| 312 |
if err != nil { |
| 313 |
t.Fatal(err) |
| 314 |
} |
| 315 |
} else { |
| 316 |
ok = got == tc.want |
| 317 |
} |
| 318 |
if !ok { |
| 319 |
t.Errorf("\n got: %q\nwant: %q", got, tc.want) |
| 320 |
} |
| 321 |
}) |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
func TestAdaptor(t *testing.T) { |
| 326 |
testCases := []struct { |
| 327 |
err error |
| 328 |
fmt string |
| 329 |
want string |
| 330 |
regexp bool |
| 331 |
}{{ |
| 332 |
err: adapted{"adapted", nil}, |
| 333 |
fmt: "%+v", |
| 334 |
want: "adapted:" + |
| 335 |
"\n detail", |
| 336 |
}, { |
| 337 |
err: adapted{"outer", adapted{"mid", adapted{"inner", nil}}}, |
| 338 |
fmt: "%+v", |
| 339 |
want: "outer:" + |
| 340 |
"\n detail" + |
| 341 |
"\n - mid:" + |
| 342 |
"\n detail" + |
| 343 |
"\n - inner:" + |
| 344 |
"\n detail", |
| 345 |
}} |
| 346 |
for i, tc := range testCases { |
| 347 |
t.Run(fmt.Sprintf("%d/%s", i, tc.fmt), func(t *testing.T) { |
| 348 |
got := gofmt.Sprintf(tc.fmt, tc.err) |
| 349 |
if got != tc.want { |
| 350 |
t.Errorf("\n got: %q\nwant: %q", got, tc.want) |
| 351 |
} |
| 352 |
}) |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
var _ errors.Formatter = wrapped{} |
| 357 |
|
| 358 |
type wrapped struct { |
| 359 |
msg string |
| 360 |
err error |
| 361 |
} |
| 362 |
|
| 363 |
func (e wrapped) Error() string { return "should call Format" } |
| 364 |
|
| 365 |
func (e wrapped) FormatError(p errors.Printer) (next error) { |
| 366 |
p.Print(e.msg) |
| 367 |
p.Detail() |
| 368 |
p.Print("somefile.go:123") |
| 369 |
return e.err |
| 370 |
} |
| 371 |
|
| 372 |
var _ errors.Formatter = detailed{} |
| 373 |
|
| 374 |
type detailed struct{} |
| 375 |
|
| 376 |
func (e detailed) Error() string { return fmt.Sprint(e) } |
| 377 |
|
| 378 |
func (detailed) FormatError(p errors.Printer) (next error) { |
| 379 |
p.Printf("out of %s", "peanuts") |
| 380 |
p.Detail() |
| 381 |
p.Print("the elephant is on strike\n") |
| 382 |
p.Printf("and the %d monkeys\nare laughing", 12) |
| 383 |
return nil |
| 384 |
} |
| 385 |
|
| 386 |
type withFrameAndMore struct { |
| 387 |
frame errors.Frame |
| 388 |
} |
| 389 |
|
| 390 |
func (e *withFrameAndMore) Error() string { return fmt.Sprint(e) } |
| 391 |
|
| 392 |
func (e *withFrameAndMore) FormatError(p errors.Printer) (next error) { |
| 393 |
p.Print("something") |
| 394 |
if p.Detail() { |
| 395 |
e.frame.Format(p) |
| 396 |
p.Print("something more") |
| 397 |
} |
| 398 |
return nil |
| 399 |
} |
| 400 |
|
| 401 |
type spurious string |
| 402 |
|
| 403 |
func (e spurious) Error() string { return fmt.Sprint(e) } |
| 404 |
|
| 405 |
func (e spurious) Format(fmt.State, rune) { |
| 406 |
panic("should never be called by one of the tests") |
| 407 |
} |
| 408 |
|
| 409 |
func (e spurious) FormatError(p errors.Printer) (next error) { |
| 410 |
p.Print("spurious") |
| 411 |
p.Detail() |
| 412 |
if e == "" { |
| 413 |
p.Print() |
| 414 |
} else { |
| 415 |
p.Print("\n", string(e)) |
| 416 |
} |
| 417 |
return nil |
| 418 |
} |
| 419 |
|
| 420 |
type adapted struct { |
| 421 |
msg string |
| 422 |
err error |
| 423 |
} |
| 424 |
|
| 425 |
func (e adapted) Error() string { return string(e.msg) } |
| 426 |
|
| 427 |
func (e adapted) Format(s fmt.State, verb rune) { |
| 428 |
fmt.FormatError(s, verb, e) |
| 429 |
} |
| 430 |
|
| 431 |
func (e adapted) FormatError(p errors.Printer) error { |
| 432 |
p.Print(e.msg) |
| 433 |
p.Detail() |
| 434 |
p.Print("detail") |
| 435 |
return e.err |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
type formatError string |
| 441 |
|
| 442 |
func (e formatError) Error() string { return string(e) } |
| 443 |
|
| 444 |
func (e formatError) Format(s fmt.State, verb rune) { |
| 445 |
|
| 446 |
switch verb { |
| 447 |
case 'v': |
| 448 |
if s.Flag('+') { |
| 449 |
io.WriteString(s, string(e)) |
| 450 |
fmt.Fprintf(s, ":\n%s", "otherfile.go:456") |
| 451 |
return |
| 452 |
} |
| 453 |
fallthrough |
| 454 |
case 's': |
| 455 |
io.WriteString(s, string(e)) |
| 456 |
case 'q': |
| 457 |
fmt.Fprintf(s, "%q", string(e)) |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
func (e formatError) GoString() string { |
| 462 |
panic("should never be called") |
| 463 |
} |
| 464 |
|
| 465 |
type fmtTwiceErr struct { |
| 466 |
format string |
| 467 |
args []interface{} |
| 468 |
} |
| 469 |
|
| 470 |
func fmtTwice(format string, a ...interface{}) error { |
| 471 |
return fmtTwiceErr{format, a} |
| 472 |
} |
| 473 |
|
| 474 |
func (e fmtTwiceErr) Error() string { return fmt.Sprint(e) } |
| 475 |
|
| 476 |
func (e fmtTwiceErr) FormatError(p errors.Printer) (next error) { |
| 477 |
p.Printf(e.format, e.args...) |
| 478 |
p.Print("/") |
| 479 |
p.Printf(e.format, e.args...) |
| 480 |
return nil |
| 481 |
} |
| 482 |
|
| 483 |
func (e fmtTwiceErr) GoString() string { |
| 484 |
return "2 times " + fmt.Sprintf(e.format, e.args...) |
| 485 |
} |
| 486 |
|
| 487 |
type panicValue struct{} |
| 488 |
|
| 489 |
func (panicValue) String() string { panic("panic") } |
| 490 |
|
| 491 |
var rePath = regexp.MustCompile(`( [^ ]*)fmt.*test\.`) |
| 492 |
var reLine = regexp.MustCompile(":[0-9]*\n?$") |
| 493 |
|
| 494 |
func cleanPath(s string) string { |
| 495 |
s = rePath.ReplaceAllString(s, "/path.") |
| 496 |
s = reLine.ReplaceAllString(s, ":xxx") |
| 497 |
s = strings.Replace(s, "\n ", "", -1) |
| 498 |
s = strings.Replace(s, " /", "/", -1) |
| 499 |
return s |
| 500 |
} |
| 501 |
|
| 502 |
func errToParts(err error) (a []string) { |
| 503 |
for err != nil { |
| 504 |
var p testPrinter |
| 505 |
if errors.Unwrap(err) != nil { |
| 506 |
p.str += "wraps:" |
| 507 |
} |
| 508 |
f, ok := err.(errors.Formatter) |
| 509 |
if !ok { |
| 510 |
a = append(a, err.Error()) |
| 511 |
break |
| 512 |
} |
| 513 |
err = f.FormatError(&p) |
| 514 |
a = append(a, cleanPath(p.str)) |
| 515 |
} |
| 516 |
return a |
| 517 |
|
| 518 |
} |
| 519 |
|
| 520 |
type testPrinter struct { |
| 521 |
str string |
| 522 |
} |
| 523 |
|
| 524 |
func (p *testPrinter) Print(a ...interface{}) { |
| 525 |
p.str += fmt.Sprint(a...) |
| 526 |
} |
| 527 |
|
| 528 |
func (p *testPrinter) Printf(format string, a ...interface{}) { |
| 529 |
p.str += fmt.Sprintf(format, a...) |
| 530 |
} |
| 531 |
|
| 532 |
func (p *testPrinter) Detail() bool { |
| 533 |
p.str += " /" |
| 534 |
return true |
| 535 |
} |