From 09e55f21458f9929edce6d41d976dc394f730550 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Sun, 12 Jul 2020 22:27:32 -0400 Subject: [PATCH 1/2] Add Unwrap for compat with go1.13 errors --- errwrap.go | 4 ++++ errwrap_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/errwrap.go b/errwrap.go index a733bef..325f278 100644 --- a/errwrap.go +++ b/errwrap.go @@ -167,3 +167,7 @@ func (w *wrappedError) Error() string { func (w *wrappedError) WrappedErrors() []error { return []error{w.Outer, w.Inner} } + +func (w *wrappedError) Unwrap() error { + return w.Inner +} \ No newline at end of file diff --git a/errwrap_test.go b/errwrap_test.go index 5ae5f8e..60d37d3 100644 --- a/errwrap_test.go +++ b/errwrap_test.go @@ -1,6 +1,7 @@ package errwrap import ( + "errors" "fmt" "testing" ) @@ -92,3 +93,12 @@ func TestGetAllType(t *testing.T) { } } } + +func TestWrappedError_IsCompatibleWithErrorsUnwrap(t *testing.T) { + inner := errors.New("inner error") + err := Wrap(errors.New("outer"), inner) + actual := errors.Unwrap(err) + if actual != inner { + t.Fatal("wrappedError did not unwrap to inner") + } +} \ No newline at end of file From 96a78fc01475f1ab9853c716f182a5107af3fc91 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Sun, 12 Jul 2020 23:23:39 -0400 Subject: [PATCH 2/2] Unwrap in Walk for compat with go1.13 errors --- errwrap.go | 7 ++++++- errwrap_test.go | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/errwrap.go b/errwrap.go index 325f278..44e368e 100644 --- a/errwrap.go +++ b/errwrap.go @@ -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 := "" if err != nil { @@ -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) } @@ -170,4 +175,4 @@ func (w *wrappedError) WrappedErrors() []error { func (w *wrappedError) Unwrap() error { return w.Inner -} \ No newline at end of file +} diff --git a/errwrap_test.go b/errwrap_test.go index 60d37d3..8c16a56 100644 --- a/errwrap_test.go +++ b/errwrap_test.go @@ -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 { @@ -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 { @@ -101,4 +116,4 @@ func TestWrappedError_IsCompatibleWithErrorsUnwrap(t *testing.T) { if actual != inner { t.Fatal("wrappedError did not unwrap to inner") } -} \ No newline at end of file +}