diff --git a/errors.go b/errors.go index 7f15c36..5c9ab9b 100644 --- a/errors.go +++ b/errors.go @@ -25,6 +25,10 @@ type customError struct { displayMessage string } +func (ce customError) Unwrap() error { + return ce.originalError +} + // New creates a new customError func (code ErrorCode) New(msg string) error { return customError{code: code, originalError: errors.New(msg)} diff --git a/errors_test.go b/errors_test.go index 207c8d3..25ce67b 100644 --- a/errors_test.go +++ b/errors_test.go @@ -1,6 +1,7 @@ package errors_test import ( + stderrors "errors" "fmt" "io" @@ -156,4 +157,24 @@ var _ = Describe("Errors", func() { Expect(errors.Code(err)).To(Equal(errors.InvalidArgument)) }) }) + + Describe("#Unwrap", func() { + It("returns a non-nil inner error", func() { + err := errors.NotFound.New("not found") + Expect(stderrors.Unwrap(err)).NotTo(BeNil()) + }) + + It("allows errors.Is to find a wrapped sentinel error", func() { + err := errors.NotFound.Wrap(io.EOF, "wrapped") + Expect(stderrors.Is(err, io.EOF)).To(BeTrue()) + }) + + It("allows stdlib errors.Is to find the error when wrapped with fmt.Errorf", func() { + sentinel := stderrors.New("sentinel") + custom := errors.InternalError.Wrap(sentinel, "outer") + wrapped := fmt.Errorf("additional context: %w", custom) + + Expect(stderrors.Is(wrapped, sentinel)).To(BeTrue()) + }) + }) })