Skip to content

Commit 10f3629

Browse files
committedJan 31, 2019
xerrors: add Format method to errorString
Fixes #30036. Change-Id: I8155e94f3a4bbda77170ebbecb24c27d9e26860b Reviewed-on: https://go-review.googlesource.com/c/160658 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 31580a5 commit 10f3629

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed
 

‎errors.go

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
package xerrors
66

7+
import "fmt"
8+
79
// errorString is a trivial implementation of error.
810
type errorString struct {
911
s string
@@ -22,6 +24,8 @@ func (e *errorString) Error() string {
2224
return e.s
2325
}
2426

27+
func (e *errorString) Format(s fmt.State, v rune) { FormatError(e, s, v) }
28+
2529
func (e *errorString) FormatError(p Printer) (next error) {
2630
p.Print(e.s)
2731
e.frame.Format(p)

‎errors_test.go

+13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package xerrors_test
66

77
import (
88
"fmt"
9+
"regexp"
910
"testing"
1011

1112
"golang.org/x/xerrors"
@@ -34,6 +35,18 @@ func TestErrorMethod(t *testing.T) {
3435
}
3536
}
3637

38+
func TestNewDetail(t *testing.T) {
39+
got := fmt.Sprintf("%+v", xerrors.New("error"))
40+
want := `(?s)error:.+errors_test.go:\d+`
41+
ok, err := regexp.MatchString(want, got)
42+
if err != nil {
43+
t.Fatal(err)
44+
}
45+
if !ok {
46+
t.Errorf(`fmt.Sprintf("%%+v", New("error")) = %q, want %q"`, got, want)
47+
}
48+
}
49+
3750
func ExampleNew() {
3851
err := xerrors.New("emit macho dwarf: elf header corrupted")
3952
if err != nil {

0 commit comments

Comments
 (0)
Please sign in to comment.