Skip to content

Fix compatibility with errors.Is() and errors.As() #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion errwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,22 @@ func (w *wrappedError) Error() string {
return w.Outer.Error()
}

func (w *wrappedError) Is(err error) bool {
return errors.Is(w.Outer, err)
}

func (w *wrappedError) As(target interface{}) bool {
return errors.As(w.Outer, target)
}

func (w *wrappedError) WrappedErrors() []error {
return []error{w.Outer, w.Inner}
}

func (w *wrappedError) Unwrap() error {
return w.Inner
if i := errors.Unwrap(w.Inner); i != nil {
return Wrap(w.Inner, i)
} else {
return w.Inner
}
}
54 changes: 47 additions & 7 deletions errwrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ package errwrap
import (
"errors"
"fmt"
"strconv"
"testing"
)

func TestWrappedError_impl(t *testing.T) {
t.Parallel()
var _ error = new(wrappedError)
}

func TestGetAll(t *testing.T) {
t.Parallel()
cases := []struct {
Err error
Msg string
Expand Down Expand Up @@ -58,15 +61,18 @@ func TestGetAll(t *testing.T) {
}

for i, tc := range cases {
actual := GetAll(tc.Err, tc.Msg)
if len(actual) != tc.Len {
t.Fatalf("%d: bad: %#v", i, actual)
}
for _, v := range actual {
if v.Error() != tc.Msg {
t.Run(fmt.Sprintf("Test: %d", i), func(t *testing.T) {
t.Parallel()
actual := GetAll(tc.Err, tc.Msg)
if len(actual) != tc.Len {
t.Fatalf("%d: bad: %#v", i, actual)
}
}
for _, v := range actual {
if v.Error() != tc.Msg {
t.Fatalf("%d: bad: %#v", i, actual)
}
}
})
}
}

Expand Down Expand Up @@ -113,10 +119,44 @@ func TestGetAllType(t *testing.T) {
}

func TestWrappedError_IsCompatibleWithErrorsUnwrap(t *testing.T) {
t.Parallel()

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")
}
}

func TestWrappedError_IsCompatibleWithErrorsIs(t *testing.T) {
t.Parallel()

inner := errors.New("inner")
outer := errors.New("outer")
wrapped := Wrap(outer, inner)
if !errors.Is(wrapped, outer) {
t.Fatal("wrappedError did not errors.Is() to outer")
} else if !errors.Is(wrapped, inner) {
t.Fatal("wrappedError did not errors.Is() to inner")
} else if errors.Is(wrapped, errors.New("unexpected")) {
t.Fatal("wrappedError should not have errors.Is() to unexpected")
}
}

type customError int

func (c customError) Error() string { return strconv.Itoa(int(c)) }

func TestWrappedError_IsCompatibleWithErrorsAs(t *testing.T) {
t.Parallel()

inner := customError(123)
outer := errors.New("1234")
wrapped := Wrap(outer, inner)

var c customError
if !errors.As(wrapped, &c) {
t.Fatal("wrappedError should have errors.As() to customError")
}
}
Loading