diff --git a/v2/errors/error_unwrap.go b/v2/errors/error_unwrap.go new file mode 100644 index 00000000..dd4ab541 --- /dev/null +++ b/v2/errors/error_unwrap.go @@ -0,0 +1,12 @@ +// +build go1.13 + +package errors + +import ( + "github.com/pkg/errors" +) + +// Unwrap returns the result of calling errors.Unwrap on the underlying error +func (err *Error) Unwrap() error { + return errors.Unwrap(err.Err) +} diff --git a/v2/errors/error_unwrap_test.go b/v2/errors/error_unwrap_test.go new file mode 100644 index 00000000..93970c44 --- /dev/null +++ b/v2/errors/error_unwrap_test.go @@ -0,0 +1,32 @@ +// +build go1.13 + +package errors + +import ( + "fmt" + "testing" + + "github.com/pkg/errors" +) + +func TestFindingErrorInChain(t *testing.T) { + baseErr := errors.New("base error") + wrappedErr := errors.Wrap(baseErr, "failed") + err := New(wrappedErr, 0) + + if !errors.Is(err, baseErr) { + t.Errorf("Failed to find base error: %s", err.Error()) + } +} + +func TestErrorUnwrapping(t *testing.T) { + baseErr := errors.New("base error") + wrappedErr := fmt.Errorf("failed: %w", baseErr) + err := New(wrappedErr, 0) + + unwrapped := errors.Unwrap(err) + + if unwrapped != baseErr { + t.Errorf("Failed to find base error: %s", unwrapped.Error()) + } +}