-
Notifications
You must be signed in to change notification settings - Fork 92
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from tankyouoss/master
- Loading branch information
Showing
6 changed files
with
119 additions
and
79 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 |
---|---|---|
|
@@ -3,3 +3,5 @@ language: go | |
go: | ||
- "1.8.x" | ||
- "1.10.x" | ||
- "1.13.x" | ||
- "1.14.x" |
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
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,26 @@ | ||
// +build go1.13 | ||
|
||
package errors | ||
|
||
import ( | ||
baseErrors "errors" | ||
) | ||
|
||
// Is detects whether the error is equal to a given error. Errors | ||
// 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 baseErrors.Is(e, original) { | ||
return true | ||
} | ||
|
||
if e, ok := e.(*Error); ok { | ||
return Is(e.Err, original) | ||
} | ||
|
||
if original, ok := original.(*Error); ok { | ||
return Is(e, original.Err) | ||
} | ||
|
||
return false | ||
} |
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,68 @@ | ||
// +build go1.13 | ||
|
||
package errors | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
) | ||
|
||
// This test should work only for go 1.13 and latter | ||
func TestIs113(t *testing.T) { | ||
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)") | ||
} | ||
} | ||
|
||
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 | ||
} |
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,22 @@ | ||
// +build !go1.13 | ||
|
||
package errors | ||
|
||
// 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. | ||
func Is(e error, original error) bool { | ||
if e == original { | ||
return true | ||
} | ||
|
||
if e, ok := e.(*Error); ok { | ||
return Is(e.Err, original) | ||
} | ||
|
||
if original, ok := original.(*Error); ok { | ||
return Is(e, original.Err) | ||
} | ||
|
||
return false | ||
} |
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