Skip to content

Commit baa9282

Browse files
authored
Merge pull request #26 from tankyouoss/master
<feature>(IS): Use original errors.Is as final test for Is function
2 parents f104a1a + 4ac07ff commit baa9282

3 files changed

Lines changed: 65 additions & 4 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,6 @@ packages by Facebook and Dropbox, it was moved to one canonical location so
6464
everyone can benefit.
6565

6666
This package is licensed under the MIT license, see LICENSE.MIT for details.
67+
68+
## Changelog
69+
* v1.1.0 updated to use go1.13's standard-library errors.Is method instead of == in errors.Is

error.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ package errors
4747

4848
import (
4949
"bytes"
50+
baseErrors "errors"
5051
"fmt"
5152
"reflect"
5253
"runtime"
@@ -140,11 +141,10 @@ func WrapPrefix(e interface{}, prefix string, skip int) *Error {
140141
}
141142

142143
// Is detects whether the error is equal to a given error. Errors
143-
// are considered equal by this function if they are the same object,
144-
// or if they both contain the same error inside an errors.Error.
144+
// are considered equal by this function if they are matched by errors.Is
145+
// or if their contained errors are matched through errors.Is
145146
func Is(e error, original error) bool {
146-
147-
if e == original {
147+
if baseErrors.Is(e, original) {
148148
return true
149149
}
150150

error_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,50 @@ func TestIs(t *testing.T) {
129129
t.Errorf("io.EOF is fmt.Errorf")
130130
}
131131

132+
custErr := errorWithCustomIs{
133+
Key: "TestForFun",
134+
Err: io.EOF,
135+
}
136+
137+
shouldMatch := errorWithCustomIs{
138+
Key: "TestForFun",
139+
}
140+
141+
shouldNotMatch := errorWithCustomIs{Key: "notOk"}
142+
143+
if !Is(custErr, shouldMatch) {
144+
t.Errorf("custErr is not a TestForFun customError")
145+
}
146+
147+
if Is(custErr, shouldNotMatch) {
148+
t.Errorf("custErr is a notOk customError")
149+
}
150+
151+
if !Is(custErr, New(shouldMatch)) {
152+
t.Errorf("custErr is not a New(TestForFun customError)")
153+
}
154+
155+
if Is(custErr, New(shouldNotMatch)) {
156+
t.Errorf("custErr is a New(notOk customError)")
157+
}
158+
159+
if !Is(New(custErr), shouldMatch) {
160+
t.Errorf("New(custErr) is not a TestForFun customError")
161+
}
162+
163+
if Is(New(custErr), shouldNotMatch) {
164+
t.Errorf("New(custErr) is a notOk customError")
165+
}
166+
167+
if !Is(New(custErr), New(shouldMatch)) {
168+
t.Errorf("New(custErr) is not a New(TestForFun customError)")
169+
}
170+
171+
if Is(New(custErr), New(shouldNotMatch)) {
172+
t.Errorf("New(custErr) is a New(notOk customError)")
173+
}
174+
175+
132176
}
133177

134178
func TestWrapError(t *testing.T) {
@@ -317,3 +361,17 @@ func callersToFrames(callers []uintptr) []runtime.Frame {
317361
}
318362
}
319363
}
364+
365+
type errorWithCustomIs struct {
366+
Key string
367+
Err error
368+
}
369+
370+
func (ewci errorWithCustomIs) Error() string {
371+
return "["+ewci.Key+"]: " + ewci.Err.Error()
372+
}
373+
374+
func (ewci errorWithCustomIs) Is(target error) bool {
375+
matched, ok := target.(errorWithCustomIs)
376+
return ok && matched.Key == ewci.Key
377+
}

0 commit comments

Comments
 (0)