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

Commit 3dc37da

Browse files
committed
Reverse the order of Fprint output (#43)
Fprint should match the stack output, innermost error first. It probably doesn't matter as Fprint is going away.
1 parent c0c662e commit 3dc37da

File tree

3 files changed

+39
-33
lines changed

3 files changed

+39
-33
lines changed

errors.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,24 +182,21 @@ func Cause(err error) error {
182182

183183
// Fprint prints the error to the supplied writer.
184184
// If the error implements the Causer interface described in Cause
185-
// Print will recurse into the error's cause.
186-
// If the error implements one of the following interfaces:
187-
//
188-
// type Stacktrace interface {
189-
// Stacktrace() []Frame
190-
// }
191-
//
192-
// Print will also print the file and line of the error.
185+
// Fprint will recurse into the error's cause.
186+
// Fprint will also print the file and line of the error.
193187
// If err is nil, nothing is printed.
194188
//
195189
// Deprecated: Fprint will be removed in version 0.7.
196190
func Fprint(w io.Writer, err error) {
197-
for err != nil {
198-
fmt.Fprintf(w, "%+v\n", err)
199-
cause, ok := err.(causer)
200-
if !ok {
201-
break
191+
var fn func(err error)
192+
fn = func(err error) {
193+
if err == nil {
194+
return
202195
}
203-
err = cause.Cause()
196+
if cause, ok := err.(causer); ok {
197+
fn(cause.Cause())
198+
}
199+
fmt.Fprintf(w, "%+v\n", err)
204200
}
201+
fn(err)
205202
}

errors_test.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -119,20 +119,29 @@ func TestFprintError(t *testing.T) {
119119
want: "EOF\n",
120120
}, {
121121
// caused error returns cause
122-
err: &causeError{cause: io.EOF},
123-
want: "cause error\nEOF\n",
122+
err: &causeError{cause: io.EOF},
123+
want: "EOF\n" +
124+
"cause error\n",
124125
}, {
125126
err: x, // return from errors.New
126127
want: "github.com/pkg/errors/errors_test.go:106: error\n",
127128
}, {
128-
err: Wrap(x, "message"),
129-
want: "github.com/pkg/errors/errors_test.go:128: message\ngithub.com/pkg/errors/errors_test.go:106: error\n",
129+
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",
132+
}, {
133+
err: Wrap(io.EOF, "message"),
134+
want: "EOF\n" +
135+
"github.com/pkg/errors/errors_test.go:133: message\n",
130136
}, {
131-
err: Wrap(Wrap(x, "message"), "another message"),
132-
want: "github.com/pkg/errors/errors_test.go:131: another message\ngithub.com/pkg/errors/errors_test.go:131: message\ngithub.com/pkg/errors/errors_test.go:106: error\n",
137+
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",
133141
}, {
134-
err: Wrapf(x, "message"),
135-
want: "github.com/pkg/errors/errors_test.go:134: message\ngithub.com/pkg/errors/errors_test.go:106: error\n",
142+
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",
136145
}}
137146

138147
for i, tt := range tests {
@@ -198,30 +207,30 @@ func TestStack(t *testing.T) {
198207
want []fileline
199208
}{{
200209
New("ooh"), []fileline{
201-
{"github.com/pkg/errors/errors_test.go", 200},
210+
{"github.com/pkg/errors/errors_test.go", 209},
202211
},
203212
}, {
204213
Wrap(New("ooh"), "ahh"), []fileline{
205-
{"github.com/pkg/errors/errors_test.go", 204}, // this is the stack of Wrap, not New
214+
{"github.com/pkg/errors/errors_test.go", 213}, // this is the stack of Wrap, not New
206215
},
207216
}, {
208217
Cause(Wrap(New("ooh"), "ahh")), []fileline{
209-
{"github.com/pkg/errors/errors_test.go", 208}, // this is the stack of New
218+
{"github.com/pkg/errors/errors_test.go", 217}, // this is the stack of New
210219
},
211220
}, {
212221
func() error { return New("ooh") }(), []fileline{
213-
{"github.com/pkg/errors/errors_test.go", 212}, // this is the stack of New
214-
{"github.com/pkg/errors/errors_test.go", 212}, // this is the stack of New's caller
222+
{"github.com/pkg/errors/errors_test.go", 221}, // this is the stack of New
223+
{"github.com/pkg/errors/errors_test.go", 221}, // this is the stack of New's caller
215224
},
216225
}, {
217226
Cause(func() error {
218227
return func() error {
219228
return Errorf("hello %s", fmt.Sprintf("world"))
220229
}()
221230
}()), []fileline{
222-
{"github.com/pkg/errors/errors_test.go", 219}, // this is the stack of Errorf
223-
{"github.com/pkg/errors/errors_test.go", 220}, // this is the stack of Errorf's caller
224-
{"github.com/pkg/errors/errors_test.go", 221}, // this is the stack of Errorf's caller's caller
231+
{"github.com/pkg/errors/errors_test.go", 228}, // this is the stack of Errorf
232+
{"github.com/pkg/errors/errors_test.go", 229}, // this is the stack of Errorf's caller
233+
{"github.com/pkg/errors/errors_test.go", 230}, // this is the stack of Errorf's caller's caller
225234
},
226235
}}
227236
for _, tt := range tests {

example_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,10 @@ func ExampleFprint() {
4949
err := fn()
5050
errors.Fprint(os.Stdout, err)
5151

52-
// Output: github.com/pkg/errors/example_test.go:36: outer
53-
// github.com/pkg/errors/example_test.go:35: middle
52+
// Output: github.com/pkg/errors/example_test.go:33: error
5453
// github.com/pkg/errors/example_test.go:34: inner
55-
// github.com/pkg/errors/example_test.go:33: error
54+
// github.com/pkg/errors/example_test.go:35: middle
55+
// github.com/pkg/errors/example_test.go:36: outer
5656
}
5757

5858
func ExampleWrapf() {

0 commit comments

Comments
 (0)