xerrors: add Format method to errorString
Fixes #30036.
Change-Id: I8155e94f3a4bbda77170ebbecb24c27d9e26860b
Reviewed-on: https://go-review.googlesource.com/c/160658
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
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 @@
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 @@
import (
"fmt"
+ "regexp"
"testing"
"golang.org/x/xerrors"
@@ -34,6 +35,18 @@
}
}
+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 {