Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 874c0ec

Browse files
authored
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.
1 parent 5776abf commit 874c0ec

File tree

3 files changed

+41
-39
lines changed

3 files changed

+41
-39
lines changed

errors.go

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ func (w wrapper) Format(s fmt.State, verb rune) {
115115
switch verb {
116116
case 'v':
117117
if s.Flag('+') {
118-
fmt.Fprintf(s, "%+v: %s", w.Stacktrace()[0], w.cause.msg)
118+
fmt.Fprintf(s, "%+v\n", w.Cause())
119+
fmt.Fprintf(s, "%+v: %s", w.Stacktrace()[0], w.msg)
119120
return
120121
}
121122
fallthrough
@@ -154,10 +155,6 @@ func Wrapf(err error, format string, args ...interface{}) error {
154155
}
155156
}
156157

157-
type causer interface {
158-
Cause() error
159-
}
160-
161158
// Cause returns the underlying cause of the error, if possible.
162159
// An error value has a cause if it implements the following
163160
// interface:
@@ -170,6 +167,10 @@ type causer interface {
170167
// be returned. If the error is nil, nil will be returned without further
171168
// investigation.
172169
func Cause(err error) error {
170+
type causer interface {
171+
Cause() error
172+
}
173+
173174
for err != nil {
174175
cause, ok := err.(causer)
175176
if !ok {
@@ -188,15 +189,8 @@ func Cause(err error) error {
188189
//
189190
// Deprecated: Fprint will be removed in version 0.7.
190191
func Fprint(w io.Writer, err error) {
191-
var fn func(err error)
192-
fn = func(err error) {
193-
if err == nil {
194-
return
195-
}
196-
if cause, ok := err.(causer); ok {
197-
fn(cause.Cause())
198-
}
199-
fmt.Fprintf(w, "%+v\n", err)
192+
if err == nil {
193+
return
200194
}
201-
fn(err)
195+
fmt.Fprintf(w, "%+v\n", err)
202196
}

errors_test.go

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,6 @@ type nilError struct{}
5757

5858
func (nilError) Error() string { return "nil error" }
5959

60-
type causeError struct {
61-
cause error
62-
}
63-
64-
func (e *causeError) Error() string { return "cause error" }
65-
func (e *causeError) Cause() error { return e.cause }
66-
6760
func TestCause(t *testing.T) {
6861
x := New("error")
6962
tests := []struct {
@@ -87,7 +80,7 @@ func TestCause(t *testing.T) {
8780
want: io.EOF,
8881
}, {
8982
// caused error returns cause
90-
err: &causeError{cause: io.EOF},
83+
err: Wrap(io.EOF, "ignored"),
9184
want: io.EOF,
9285
}, {
9386
err: x, // return from errors.New
@@ -118,30 +111,29 @@ func TestFprintError(t *testing.T) {
118111
err: io.EOF,
119112
want: "EOF\n",
120113
}, {
121-
// caused error returns cause
122-
err: &causeError{cause: io.EOF},
114+
err: Wrap(io.EOF, "cause error"),
123115
want: "EOF\n" +
124-
"cause error\n",
116+
"github.com/pkg/errors/errors_test.go:114: cause error\n",
125117
}, {
126118
err: x, // return from errors.New
127-
want: "github.com/pkg/errors/errors_test.go:106: error\n",
119+
want: "github.com/pkg/errors/errors_test.go:99: error\n",
128120
}, {
129121
err: Wrap(x, "message"),
130-
want: "github.com/pkg/errors/errors_test.go:106: error\n" +
131-
"github.com/pkg/errors/errors_test.go:129: message\n",
122+
want: "github.com/pkg/errors/errors_test.go:99: error\n" +
123+
"github.com/pkg/errors/errors_test.go:121: message\n",
132124
}, {
133125
err: Wrap(io.EOF, "message"),
134126
want: "EOF\n" +
135-
"github.com/pkg/errors/errors_test.go:133: message\n",
127+
"github.com/pkg/errors/errors_test.go:125: message\n",
136128
}, {
137129
err: Wrap(Wrap(x, "message"), "another message"),
138-
want: "github.com/pkg/errors/errors_test.go:106: error\n" +
139-
"github.com/pkg/errors/errors_test.go:137: message\n" +
140-
"github.com/pkg/errors/errors_test.go:137: another message\n",
130+
want: "github.com/pkg/errors/errors_test.go:99: error\n" +
131+
"github.com/pkg/errors/errors_test.go:129: message\n" +
132+
"github.com/pkg/errors/errors_test.go:129: another message\n",
141133
}, {
142134
err: Wrapf(x, "message"),
143-
want: "github.com/pkg/errors/errors_test.go:106: error\n" +
144-
"github.com/pkg/errors/errors_test.go:142: message\n",
135+
want: "github.com/pkg/errors/errors_test.go:99: error\n" +
136+
"github.com/pkg/errors/errors_test.go:134: message\n",
145137
}}
146138

147139
for i, tt := range tests {

format_test.go

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package errors
22

33
import (
44
"fmt"
5+
"io"
56
"testing"
67
)
78

@@ -22,7 +23,7 @@ func TestFormat(t *testing.T) {
2223
}, {
2324
New("error"),
2425
"%+v",
25-
"github.com/pkg/errors/format_test.go:23: error",
26+
"github.com/pkg/errors/format_test.go:24: error",
2627
}, {
2728
Errorf("%s", "error"),
2829
"%s",
@@ -34,7 +35,7 @@ func TestFormat(t *testing.T) {
3435
}, {
3536
Errorf("%s", "error"),
3637
"%+v",
37-
"github.com/pkg/errors/format_test.go:35: error",
38+
"github.com/pkg/errors/format_test.go:36: error",
3839
}, {
3940
Wrap(New("error"), "error2"),
4041
"%s",
@@ -46,19 +47,34 @@ func TestFormat(t *testing.T) {
4647
}, {
4748
Wrap(New("error"), "error2"),
4849
"%+v",
49-
"github.com/pkg/errors/format_test.go:47: error2",
50+
"github.com/pkg/errors/format_test.go:48: error\n" +
51+
"github.com/pkg/errors/format_test.go:48: error2",
52+
}, {
53+
Wrap(io.EOF, "error"),
54+
"%s",
55+
"error: EOF",
5056
}, {
5157
Wrapf(New("error"), "error%d", 2),
5258
"%s",
5359
"error2: error",
60+
}, {
61+
Wrap(io.EOF, "error"),
62+
"%v",
63+
"error: EOF",
64+
}, {
65+
Wrap(io.EOF, "error"),
66+
"%+v",
67+
"EOF\n" +
68+
"github.com/pkg/errors/format_test.go:65: error",
5469
}, {
5570
Wrapf(New("error"), "error%d", 2),
5671
"%v",
5772
"error2: error",
5873
}, {
5974
Wrapf(New("error"), "error%d", 2),
6075
"%+v",
61-
"github.com/pkg/errors/format_test.go:59: error2",
76+
"github.com/pkg/errors/format_test.go:74: error\n" +
77+
"github.com/pkg/errors/format_test.go:74: error2",
6278
}}
6379

6480
for _, tt := range tests {

0 commit comments

Comments
 (0)