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 96a78fc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion 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,6 +150,9 @@ 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)
}
Expand All @@ -170,4 +175,4 @@ func (w *wrappedError) WrappedErrors() []error {

func (w *wrappedError) Unwrap() error {
return w.Inner
}
}
17 changes: 16 additions & 1 deletion 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 All @@ -101,4 +116,4 @@ func TestWrappedError_IsCompatibleWithErrorsUnwrap(t *testing.T) {
if actual != inner {
t.Fatal("wrappedError did not unwrap to inner")
}
}
}

0 comments on commit 96a78fc

Please sign in to comment.