Skip to content

Commit

Permalink
Reimplement Fprint in terms of fmt.Fprintf (pkg#44)
Browse files Browse the repository at this point in the history
This PR follows on from pkg#40 completely implementing Fprint in terms of
fmt.Fprintf. This will be the final PR before Fprint is removed.
  • Loading branch information
davecheney authored Jun 9, 2016
1 parent 5776abf commit 874c0ec
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 39 deletions.
24 changes: 9 additions & 15 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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 {
Expand All @@ -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)
}
32 changes: 12 additions & 20 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 20 additions & 4 deletions format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package errors

import (
"fmt"
"io"
"testing"
)

Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -46,19 +47,34 @@ 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",
"error2: error",
}, {
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 {
Expand Down

0 comments on commit 874c0ec

Please sign in to comment.