Skip to content

<feature>(IS): Use original errors.Is as final test for Is function #26

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

Merged
merged 1 commit into from
May 29, 2020
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
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ packages by Facebook and Dropbox, it was moved to one canonical location so
everyone can benefit.

This package is licensed under the MIT license, see LICENSE.MIT for details.

## Changelog
* v1.1.0 updated to use go1.13's standard-library errors.Is method instead of == in errors.Is
8 changes: 4 additions & 4 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ package errors

import (
"bytes"
baseErrors "errors"
"fmt"
"reflect"
"runtime"
Expand Down Expand Up @@ -140,11 +141,10 @@ func WrapPrefix(e interface{}, prefix string, skip int) *Error {
}

// Is detects whether the error is equal to a given error. Errors
// are considered equal by this function if they are the same object,
// or if they both contain the same error inside an errors.Error.
// are considered equal by this function if they are matched by errors.Is
// or if their contained errors are matched through errors.Is
func Is(e error, original error) bool {

if e == original {
if baseErrors.Is(e, original) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

N.B. This will only compile on go1.13 and above – given that go only supports the two most recent major releases, I think that it's ok for us to drop go1.12 support; but we should add something to the README to let people know that this has happened.

Could you please add a section

## Changelog
* v1.1.0 updated to use go1.13's standard-library errors.Is method instead of == in errors.Is

return true
}

Expand Down
58 changes: 58 additions & 0 deletions error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,50 @@ func TestIs(t *testing.T) {
t.Errorf("io.EOF is fmt.Errorf")
}

custErr := errorWithCustomIs{
Key: "TestForFun",
Err: io.EOF,
}

shouldMatch := errorWithCustomIs{
Key: "TestForFun",
}

shouldNotMatch := errorWithCustomIs{Key: "notOk"}

if !Is(custErr, shouldMatch) {
t.Errorf("custErr is not a TestForFun customError")
}

if Is(custErr, shouldNotMatch) {
t.Errorf("custErr is a notOk customError")
}

if !Is(custErr, New(shouldMatch)) {
t.Errorf("custErr is not a New(TestForFun customError)")
}

if Is(custErr, New(shouldNotMatch)) {
t.Errorf("custErr is a New(notOk customError)")
}

if !Is(New(custErr), shouldMatch) {
t.Errorf("New(custErr) is not a TestForFun customError")
}

if Is(New(custErr), shouldNotMatch) {
t.Errorf("New(custErr) is a notOk customError")
}

if !Is(New(custErr), New(shouldMatch)) {
t.Errorf("New(custErr) is not a New(TestForFun customError)")
}

if Is(New(custErr), New(shouldNotMatch)) {
t.Errorf("New(custErr) is a New(notOk customError)")
}


}

func TestWrapError(t *testing.T) {
Expand Down Expand Up @@ -317,3 +361,17 @@ func callersToFrames(callers []uintptr) []runtime.Frame {
}
}
}

type errorWithCustomIs struct {
Key string
Err error
}

func (ewci errorWithCustomIs) Error() string {
return "["+ewci.Key+"]: " + ewci.Err.Error()
}

func (ewci errorWithCustomIs) Is(target error) bool {
matched, ok := target.(errorWithCustomIs)
return ok && matched.Key == ewci.Key
}