diff --git a/errors/errors.go b/errors/errors.go index b2b32aac..0a68c679 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -169,7 +169,8 @@ type withStack struct { *stack.CallStack } -func (w *withStack) Cause() error { return w.error } +func (w *withStack) Cause() error { return w.error } +func (w *withStack) Unwrap() error { return w.error } func (w *withStack) Context() map[string]interface{} { if child, ok := w.error.(HasContext); ok { return child.Context() @@ -263,6 +264,7 @@ type withMessage struct { func (w *withMessage) Error() string { return w.msg + ": " + w.cause.Error() } func (w *withMessage) Cause() error { return w.cause } +func (w *withMessage) Unwrap() error { return w.cause } func (w *withMessage) Format(s fmt.State, verb rune) { switch verb { diff --git a/errors/go113.go b/errors/go113.go index be0d10d0..2c83c724 100644 --- a/errors/go113.go +++ b/errors/go113.go @@ -1,3 +1,4 @@ +//go:build go1.13 // +build go1.13 package errors diff --git a/errors/go113_test.go b/errors/go113_test.go new file mode 100644 index 00000000..10666c0a --- /dev/null +++ b/errors/go113_test.go @@ -0,0 +1,59 @@ +//go:build go1.13 +// +build go1.13 + +package errors + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestIs(t *testing.T) { + target := New("wrapped") + + tests := []struct { + name string + target error + err error + }{ + { + name: "holster_wrap", + target: target, + err: Wrap(target, "some reason"), + }, + { + name: "holster_double_wrap", + target: target, + err: Wrap(Wrap(target, "reason 2"), "reason 1"), + }, + { + name: "holster_triple_wrap", + target: target, + err: Wrap(Wrap(Wrap(target, "reason 3"), "reason 2"), "reason 1"), + }, + { + name: "holster_triple_wrapf", + target: target, + err: Wrapf(Wrapf(Wrapf(target, "reason %d", 3), "reason %d", 2), "reason %d", 1), + }, + { + name: "std_wrap", + target: target, + err: fmt.Errorf("some reason: %w", target), + }, + { + name: "std_double_wrap", + target: target, + err: fmt.Errorf("reason 1: %w", fmt.Errorf("reason 2: %w", target)), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + assert.True(t, Is(tt.err, tt.target)) + assert.ErrorIs(t, tt.err, tt.target) + }) + } +}