Skip to content

Commit

Permalink
Unwrap in Walk for compat with go1.13 errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed Jul 13, 2020
1 parent 09e55f2 commit 07d9576
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
6 changes: 6 additions & 0 deletions errwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func Wrap(outer, inner error) error {
//
// format is the format of the error message. The string '{{err}}' will
// be replaced with the original error message.
//
// Deprecated: Use fmt.Errorf()
func Wrapf(format string, err error) error {
outerMsg := "<nil>"
if err != nil {
Expand Down Expand Up @@ -148,11 +150,15 @@ func Walk(err error, cb WalkFunc) {
for _, err := range e.WrappedErrors() {
Walk(err, cb)
}
case interface { Unwrap() error }:
cb(err)
Walk(e.Unwrap(), cb)
default:
cb(err)
}
}


// wrappedError is an implementation of error that has both the
// outer and inner errors.
type wrappedError struct {
Expand Down
15 changes: 15 additions & 0 deletions errwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ func TestGetAll(t *testing.T) {
"foo",
1,
},
{
fmt.Errorf("foo: %w", fmt.Errorf("bar")),
"foo: bar",
1,
},
{
fmt.Errorf("foo: %w", fmt.Errorf("bar")),
"bar",
1,
},
}

for i, tc := range cases {
Expand Down Expand Up @@ -84,6 +94,11 @@ func TestGetAllType(t *testing.T) {
Wrapf("", nil),
0,
},
{
fmt.Errorf("one: %w", fmt.Errorf("two: %w", fmt.Errorf("three"))),
fmt.Errorf("%w", errors.New("")),
2,
},
}

for i, tc := range cases {
Expand Down

0 comments on commit 07d9576

Please sign in to comment.