mirror of
https://github.com/pkg/errors.git
synced 2025-05-31 11:42:45 +00:00
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.
This commit is contained in:
parent
c0c662e216
commit
3dc37da2ca
25
errors.go
25
errors.go
@ -182,24 +182,21 @@ func Cause(err error) error {
|
|||||||
|
|
||||||
// Fprint prints the error to the supplied writer.
|
// Fprint prints the error to the supplied writer.
|
||||||
// If the error implements the Causer interface described in Cause
|
// If the error implements the Causer interface described in Cause
|
||||||
// Print will recurse into the error's cause.
|
// Fprint will recurse into the error's cause.
|
||||||
// If the error implements one of the following interfaces:
|
// Fprint will also print the file and line of the error.
|
||||||
//
|
|
||||||
// type Stacktrace interface {
|
|
||||||
// Stacktrace() []Frame
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// Print will also print the file and line of the error.
|
|
||||||
// If err is nil, nothing is printed.
|
// If err is nil, nothing is printed.
|
||||||
//
|
//
|
||||||
// Deprecated: Fprint will be removed in version 0.7.
|
// Deprecated: Fprint will be removed in version 0.7.
|
||||||
func Fprint(w io.Writer, err error) {
|
func Fprint(w io.Writer, err error) {
|
||||||
for err != nil {
|
var fn func(err error)
|
||||||
fmt.Fprintf(w, "%+v\n", err)
|
fn = func(err error) {
|
||||||
cause, ok := err.(causer)
|
if err == nil {
|
||||||
if !ok {
|
return
|
||||||
break
|
|
||||||
}
|
}
|
||||||
err = cause.Cause()
|
if cause, ok := err.(causer); ok {
|
||||||
|
fn(cause.Cause())
|
||||||
|
}
|
||||||
|
fmt.Fprintf(w, "%+v\n", err)
|
||||||
}
|
}
|
||||||
|
fn(err)
|
||||||
}
|
}
|
||||||
|
@ -119,20 +119,29 @@ func TestFprintError(t *testing.T) {
|
|||||||
want: "EOF\n",
|
want: "EOF\n",
|
||||||
}, {
|
}, {
|
||||||
// caused error returns cause
|
// caused error returns cause
|
||||||
err: &causeError{cause: io.EOF},
|
err: &causeError{cause: io.EOF},
|
||||||
want: "cause error\nEOF\n",
|
want: "EOF\n" +
|
||||||
|
"cause error\n",
|
||||||
}, {
|
}, {
|
||||||
err: x, // return from errors.New
|
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:106: error\n",
|
||||||
}, {
|
}, {
|
||||||
err: Wrap(x, "message"),
|
err: Wrap(x, "message"),
|
||||||
want: "github.com/pkg/errors/errors_test.go:128: message\ngithub.com/pkg/errors/errors_test.go:106: error\n",
|
want: "github.com/pkg/errors/errors_test.go:106: error\n" +
|
||||||
|
"github.com/pkg/errors/errors_test.go:129: message\n",
|
||||||
}, {
|
}, {
|
||||||
err: Wrap(Wrap(x, "message"), "another message"),
|
err: Wrap(io.EOF, "message"),
|
||||||
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",
|
want: "EOF\n" +
|
||||||
|
"github.com/pkg/errors/errors_test.go:133: message\n",
|
||||||
}, {
|
}, {
|
||||||
err: Wrapf(x, "message"),
|
err: Wrap(Wrap(x, "message"), "another message"),
|
||||||
want: "github.com/pkg/errors/errors_test.go:134: message\ngithub.com/pkg/errors/errors_test.go:106: error\n",
|
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",
|
||||||
|
}, {
|
||||||
|
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",
|
||||||
}}
|
}}
|
||||||
|
|
||||||
for i, tt := range tests {
|
for i, tt := range tests {
|
||||||
@ -198,20 +207,20 @@ func TestStack(t *testing.T) {
|
|||||||
want []fileline
|
want []fileline
|
||||||
}{{
|
}{{
|
||||||
New("ooh"), []fileline{
|
New("ooh"), []fileline{
|
||||||
{"github.com/pkg/errors/errors_test.go", 200},
|
{"github.com/pkg/errors/errors_test.go", 209},
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
Wrap(New("ooh"), "ahh"), []fileline{
|
Wrap(New("ooh"), "ahh"), []fileline{
|
||||||
{"github.com/pkg/errors/errors_test.go", 204}, // this is the stack of Wrap, not New
|
{"github.com/pkg/errors/errors_test.go", 213}, // this is the stack of Wrap, not New
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
Cause(Wrap(New("ooh"), "ahh")), []fileline{
|
Cause(Wrap(New("ooh"), "ahh")), []fileline{
|
||||||
{"github.com/pkg/errors/errors_test.go", 208}, // this is the stack of New
|
{"github.com/pkg/errors/errors_test.go", 217}, // this is the stack of New
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
func() error { return New("ooh") }(), []fileline{
|
func() error { return New("ooh") }(), []fileline{
|
||||||
{"github.com/pkg/errors/errors_test.go", 212}, // this is the stack of New
|
{"github.com/pkg/errors/errors_test.go", 221}, // this is the stack of New
|
||||||
{"github.com/pkg/errors/errors_test.go", 212}, // this is the stack of New's caller
|
{"github.com/pkg/errors/errors_test.go", 221}, // this is the stack of New's caller
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
Cause(func() error {
|
Cause(func() error {
|
||||||
@ -219,9 +228,9 @@ func TestStack(t *testing.T) {
|
|||||||
return Errorf("hello %s", fmt.Sprintf("world"))
|
return Errorf("hello %s", fmt.Sprintf("world"))
|
||||||
}()
|
}()
|
||||||
}()), []fileline{
|
}()), []fileline{
|
||||||
{"github.com/pkg/errors/errors_test.go", 219}, // this is the stack of Errorf
|
{"github.com/pkg/errors/errors_test.go", 228}, // this is the stack of Errorf
|
||||||
{"github.com/pkg/errors/errors_test.go", 220}, // this is the stack of Errorf's caller
|
{"github.com/pkg/errors/errors_test.go", 229}, // this is the stack of Errorf's caller
|
||||||
{"github.com/pkg/errors/errors_test.go", 221}, // this is the stack of Errorf's caller's caller
|
{"github.com/pkg/errors/errors_test.go", 230}, // this is the stack of Errorf's caller's caller
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
@ -49,10 +49,10 @@ func ExampleFprint() {
|
|||||||
err := fn()
|
err := fn()
|
||||||
errors.Fprint(os.Stdout, err)
|
errors.Fprint(os.Stdout, err)
|
||||||
|
|
||||||
// Output: github.com/pkg/errors/example_test.go:36: outer
|
// Output: github.com/pkg/errors/example_test.go:33: error
|
||||||
// github.com/pkg/errors/example_test.go:35: middle
|
|
||||||
// github.com/pkg/errors/example_test.go:34: inner
|
// github.com/pkg/errors/example_test.go:34: inner
|
||||||
// github.com/pkg/errors/example_test.go:33: error
|
// github.com/pkg/errors/example_test.go:35: middle
|
||||||
|
// github.com/pkg/errors/example_test.go:36: outer
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleWrapf() {
|
func ExampleWrapf() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user