Skip to content

Commit

Permalink
xerrors: add Format method to errorString
Browse files Browse the repository at this point in the history
Fixes #30036.

Change-Id: I8155e94f3a4bbda77170ebbecb24c27d9e26860b
Reviewed-on: https://go-review.googlesource.com/c/160658
Reviewed-by: Brad Fitzpatrick <[email protected]>
  • Loading branch information
neild committed Jan 31, 2019
1 parent 31580a5 commit 10f3629
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

package xerrors

import "fmt"

// errorString is a trivial implementation of error.
type errorString struct {
s string
Expand All @@ -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)
Expand Down
13 changes: 13 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package xerrors_test

import (
"fmt"
"regexp"
"testing"

"golang.org/x/xerrors"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 10f3629

Please sign in to comment.