From 874c0ec5b01e7389d4c89877dc1eb1e22685188a Mon Sep 17 00:00:00 2001 From: Dave Cheney Date: Fri, 10 Jun 2016 08:39:59 +1000 Subject: [PATCH] Reimplement Fprint in terms of fmt.Fprintf (#44) This PR follows on from #40 completely implementing Fprint in terms of fmt.Fprintf. This will be the final PR before Fprint is removed. --- errors.go | 24 +++++++++--------------- errors_test.go | 32 ++++++++++++-------------------- format_test.go | 24 ++++++++++++++++++++---- 3 files changed, 41 insertions(+), 39 deletions(-) diff --git a/errors.go b/errors.go index e77e394..0261282 100644 --- a/errors.go +++ b/errors.go @@ -115,7 +115,8 @@ func (w wrapper) Format(s fmt.State, verb rune) { switch verb { case 'v': if s.Flag('+') { - fmt.Fprintf(s, "%+v: %s", w.Stacktrace()[0], w.cause.msg) + fmt.Fprintf(s, "%+v\n", w.Cause()) + fmt.Fprintf(s, "%+v: %s", w.Stacktrace()[0], w.msg) return } fallthrough @@ -154,10 +155,6 @@ func Wrapf(err error, format string, args ...interface{}) error { } } -type causer interface { - Cause() error -} - // Cause returns the underlying cause of the error, if possible. // An error value has a cause if it implements the following // interface: @@ -170,6 +167,10 @@ type causer interface { // be returned. If the error is nil, nil will be returned without further // investigation. func Cause(err error) error { + type causer interface { + Cause() error + } + for err != nil { cause, ok := err.(causer) if !ok { @@ -188,15 +189,8 @@ func Cause(err error) error { // // Deprecated: Fprint will be removed in version 0.7. func Fprint(w io.Writer, err error) { - var fn func(err error) - fn = func(err error) { - if err == nil { - return - } - if cause, ok := err.(causer); ok { - fn(cause.Cause()) - } - fmt.Fprintf(w, "%+v\n", err) + if err == nil { + return } - fn(err) + fmt.Fprintf(w, "%+v\n", err) } diff --git a/errors_test.go b/errors_test.go index 30796d8..ac58ec3 100644 --- a/errors_test.go +++ b/errors_test.go @@ -57,13 +57,6 @@ type nilError struct{} func (nilError) Error() string { return "nil error" } -type causeError struct { - cause error -} - -func (e *causeError) Error() string { return "cause error" } -func (e *causeError) Cause() error { return e.cause } - func TestCause(t *testing.T) { x := New("error") tests := []struct { @@ -87,7 +80,7 @@ func TestCause(t *testing.T) { want: io.EOF, }, { // caused error returns cause - err: &causeError{cause: io.EOF}, + err: Wrap(io.EOF, "ignored"), want: io.EOF, }, { err: x, // return from errors.New @@ -118,30 +111,29 @@ func TestFprintError(t *testing.T) { err: io.EOF, want: "EOF\n", }, { - // caused error returns cause - err: &causeError{cause: io.EOF}, + err: Wrap(io.EOF, "cause error"), want: "EOF\n" + - "cause error\n", + "github.com/pkg/errors/errors_test.go:114: cause error\n", }, { err: x, // return from errors.New - want: "github.com/pkg/errors/errors_test.go:106: error\n", + want: "github.com/pkg/errors/errors_test.go:99: error\n", }, { err: Wrap(x, "message"), - want: "github.com/pkg/errors/errors_test.go:106: error\n" + - "github.com/pkg/errors/errors_test.go:129: message\n", + want: "github.com/pkg/errors/errors_test.go:99: error\n" + + "github.com/pkg/errors/errors_test.go:121: message\n", }, { err: Wrap(io.EOF, "message"), want: "EOF\n" + - "github.com/pkg/errors/errors_test.go:133: message\n", + "github.com/pkg/errors/errors_test.go:125: message\n", }, { err: Wrap(Wrap(x, "message"), "another message"), - want: "github.com/pkg/errors/errors_test.go:106: error\n" + - "github.com/pkg/errors/errors_test.go:137: message\n" + - "github.com/pkg/errors/errors_test.go:137: another message\n", + want: "github.com/pkg/errors/errors_test.go:99: error\n" + + "github.com/pkg/errors/errors_test.go:129: message\n" + + "github.com/pkg/errors/errors_test.go:129: another message\n", }, { err: Wrapf(x, "message"), - want: "github.com/pkg/errors/errors_test.go:106: error\n" + - "github.com/pkg/errors/errors_test.go:142: message\n", + want: "github.com/pkg/errors/errors_test.go:99: error\n" + + "github.com/pkg/errors/errors_test.go:134: message\n", }} for i, tt := range tests { diff --git a/format_test.go b/format_test.go index 70a37cc..c7d49f4 100644 --- a/format_test.go +++ b/format_test.go @@ -2,6 +2,7 @@ package errors import ( "fmt" + "io" "testing" ) @@ -22,7 +23,7 @@ func TestFormat(t *testing.T) { }, { New("error"), "%+v", - "github.com/pkg/errors/format_test.go:23: error", + "github.com/pkg/errors/format_test.go:24: error", }, { Errorf("%s", "error"), "%s", @@ -34,7 +35,7 @@ func TestFormat(t *testing.T) { }, { Errorf("%s", "error"), "%+v", - "github.com/pkg/errors/format_test.go:35: error", + "github.com/pkg/errors/format_test.go:36: error", }, { Wrap(New("error"), "error2"), "%s", @@ -46,11 +47,25 @@ func TestFormat(t *testing.T) { }, { Wrap(New("error"), "error2"), "%+v", - "github.com/pkg/errors/format_test.go:47: error2", + "github.com/pkg/errors/format_test.go:48: error\n" + + "github.com/pkg/errors/format_test.go:48: error2", + }, { + Wrap(io.EOF, "error"), + "%s", + "error: EOF", }, { Wrapf(New("error"), "error%d", 2), "%s", "error2: error", + }, { + Wrap(io.EOF, "error"), + "%v", + "error: EOF", + }, { + Wrap(io.EOF, "error"), + "%+v", + "EOF\n" + + "github.com/pkg/errors/format_test.go:65: error", }, { Wrapf(New("error"), "error%d", 2), "%v", @@ -58,7 +73,8 @@ func TestFormat(t *testing.T) { }, { Wrapf(New("error"), "error%d", 2), "%+v", - "github.com/pkg/errors/format_test.go:59: error2", + "github.com/pkg/errors/format_test.go:74: error\n" + + "github.com/pkg/errors/format_test.go:74: error2", }} for _, tt := range tests {