Skip to content

Commit

Permalink
Support errors.Unwrap and errors.Is (#194)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaycetde authored Oct 12, 2022
1 parent 4c45fd4 commit 9c08c2b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
12 changes: 12 additions & 0 deletions v2/errors/error_unwrap.go
Original file line number Diff line number Diff line change
@@ -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)
}
32 changes: 32 additions & 0 deletions v2/errors/error_unwrap_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
}

0 comments on commit 9c08c2b

Please sign in to comment.