Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support errors.Unwrap and errors.Is #194

Merged
merged 3 commits into from
Oct 12, 2022
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
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())
}
}