diff --git a/errors.go b/errors.go index 0a812bd..e88d377 100644 --- a/errors.go +++ b/errors.go @@ -4,6 +4,8 @@ package xerrors +import "fmt" + // errorString is a trivial implementation of error. type errorString struct { s string @@ -22,6 +24,8 @@ func (e *errorString) Error() string { return e.s } +func (e *errorString) Format(s fmt.State, v rune) { FormatError(e, s, v) } + func (e *errorString) FormatError(p Printer) (next error) { p.Print(e.s) e.frame.Format(p) diff --git a/errors_test.go b/errors_test.go index 3417ee6..7e0e77f 100644 --- a/errors_test.go +++ b/errors_test.go @@ -6,6 +6,7 @@ package xerrors_test import ( "fmt" + "regexp" "testing" "golang.org/x/xerrors" @@ -34,6 +35,18 @@ func TestErrorMethod(t *testing.T) { } } +func TestNewDetail(t *testing.T) { + got := fmt.Sprintf("%+v", xerrors.New("error")) + want := `(?s)error:.+errors_test.go:\d+` + ok, err := regexp.MatchString(want, got) + if err != nil { + t.Fatal(err) + } + if !ok { + t.Errorf(`fmt.Sprintf("%%+v", New("error")) = %q, want %q"`, got, want) + } +} + func ExampleNew() { err := xerrors.New("emit macho dwarf: elf header corrupted") if err != nil {