Skip to content
Merged
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
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
21 changes: 21 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package errors_test

import (
stderrors "errors"
"fmt"
"io"

Expand Down Expand Up @@ -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())
})
})
})
Loading