|
1 | | -package errors |
| 1 | +package errors_test |
2 | 2 |
|
3 | 3 | import ( |
4 | 4 | "fmt" |
5 | 5 | "io" |
6 | 6 | "log/slog" |
| 7 | + "os" |
7 | 8 | "runtime" |
8 | 9 | "slices" |
9 | 10 | "strings" |
10 | 11 | "testing" |
11 | 12 |
|
12 | 13 | "github.com/gravitton/assert" |
| 14 | + . "github.com/gravitton/errors" |
13 | 15 | ) |
14 | 16 |
|
15 | 17 | func TestError(t *testing.T) { |
@@ -473,6 +475,98 @@ func TestError_LogValue(t *testing.T) { |
473 | 475 | }) |
474 | 476 | } |
475 | 477 |
|
| 478 | +func ExampleNew() { |
| 479 | + err := New("boom") |
| 480 | + |
| 481 | + fmt.Printf("%v\n", err) |
| 482 | + fmt.Printf("%q\n", err) |
| 483 | + // Output: |
| 484 | + // boom |
| 485 | + // "boom" |
| 486 | +} |
| 487 | + |
| 488 | +func ExampleNewf() { |
| 489 | + err := Newf("load user %d: %w", 42, ErrNotFound) |
| 490 | + |
| 491 | + fmt.Println(err) |
| 492 | + // Output: |
| 493 | + // load user 42: not found |
| 494 | +} |
| 495 | + |
| 496 | +func ExampleWrap() { |
| 497 | + inner := New("connection refused") |
| 498 | + err := Wrap(fmt.Errorf("load config: %w", inner)) |
| 499 | + |
| 500 | + fmt.Println(err) |
| 501 | + fmt.Println(slices.Equal(err.StackTrace(), inner.StackTrace())) |
| 502 | + // Output: |
| 503 | + // load config: connection refused |
| 504 | + // true |
| 505 | +} |
| 506 | + |
| 507 | +func ExampleError_Fields() { |
| 508 | + err := Wrap(ErrNotFound).WithField("id", 42).WithField("table", "users") |
| 509 | + |
| 510 | + fmt.Println(err) |
| 511 | + for key, value := range err.Fields() { |
| 512 | + fmt.Println(key, value) |
| 513 | + } |
| 514 | + // Output: |
| 515 | + // not found |
| 516 | + // id 42 |
| 517 | + // table users |
| 518 | +} |
| 519 | + |
| 520 | +func ExampleError_WithCause() { |
| 521 | + cause := New("connection refused").WithField("port", 5432) |
| 522 | + err := New("could not load config").WithCause(cause).WithCause(io.ErrClosedPipe) |
| 523 | + |
| 524 | + fmt.Println(err) |
| 525 | + fmt.Println(Is(err, io.ErrClosedPipe)) |
| 526 | + // Output: |
| 527 | + // could not load config |
| 528 | + // true |
| 529 | +} |
| 530 | + |
| 531 | +func ExampleError_Format() { |
| 532 | + err := New("could not load config").WithField("path", "config.yml").WithCause(io.EOF) |
| 533 | + |
| 534 | + fmt.Printf("%+v\n", err) |
| 535 | + // The stack trace holds file paths that depend on where the module is built, so this |
| 536 | + // example has no checked output. It prints something like: |
| 537 | + // |
| 538 | + // could not load config |
| 539 | + // path=config.yml |
| 540 | + // github.com/gravitton/errors_test.ExampleError_Format |
| 541 | + // /app/error_test.go:532 |
| 542 | + // ... |
| 543 | + // caused by: EOF |
| 544 | +} |
| 545 | + |
| 546 | +func ExampleError_GoString() { |
| 547 | + var zero Error |
| 548 | + err := New("boom") |
| 549 | + |
| 550 | + fmt.Printf("%#v\n", &zero) |
| 551 | + fmt.Printf("%#v\n", err) |
| 552 | + fmt.Printf("%#v\n", err.WithField("id", 42)) |
| 553 | + fmt.Printf("%#v\n", err.WithCause(io.EOF)) |
| 554 | + // Output: |
| 555 | + // &errors.Error{} |
| 556 | + // &errors.Error{err:&errors.errorString{s:"boom"}} |
| 557 | + // &errors.Error{err:&errors.errorString{s:"boom"}, fields:errors.fieldList{errors.field{key:"id", value:42}}} |
| 558 | + // &errors.Error{err:&errors.errorString{s:"boom"}, cause:&errors.errorString{s:"EOF"}} |
| 559 | +} |
| 560 | + |
| 561 | +func ExampleError_LogValue() { |
| 562 | + cause := New("connection refused").WithField("port", 5432) |
| 563 | + err := Wrap(ErrNotFound).WithField("id", 42).WithCause(cause) |
| 564 | + |
| 565 | + newLogger().Error("load user", "err", err) |
| 566 | + // Output: |
| 567 | + // level=ERROR msg="load user" err.msg="not found" err.fields.id=42 err.cause.msg="connection refused" err.cause.fields.port=5432 |
| 568 | +} |
| 569 | + |
476 | 570 | func here() string { |
477 | 571 | pc, _, _, _ := runtime.Caller(1) |
478 | 572 |
|
@@ -511,6 +605,18 @@ func logged(err error) string { |
511 | 605 | return strings.TrimSpace(b.String()) |
512 | 606 | } |
513 | 607 |
|
| 608 | +func newLogger() *slog.Logger { |
| 609 | + return slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ |
| 610 | + ReplaceAttr: func(groups []string, attr slog.Attr) slog.Attr { |
| 611 | + if len(groups) == 0 && attr.Key == slog.TimeKey { |
| 612 | + return slog.Attr{} |
| 613 | + } |
| 614 | + |
| 615 | + return attr |
| 616 | + }, |
| 617 | + })) |
| 618 | +} |
| 619 | + |
514 | 620 | func stackAt(err *Error, level int) string { |
515 | 621 | b := &strings.Builder{} |
516 | 622 | indent := strings.Repeat("\t", level) |
@@ -547,6 +653,7 @@ var ( |
547 | 653 | valueSink any |
548 | 654 |
|
549 | 655 | errSentinel = Sentinel("sentinel") |
| 656 | + ErrNotFound = Sentinel("not found") |
550 | 657 | errInit = New("init") |
551 | 658 |
|
552 | 659 | initFunction = here() |
|
0 commit comments