-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support errors.Unwrap and errors.Is (#194)
- Loading branch information
Showing
2 changed files
with
44 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
} | ||
} |