@@ -28,12 +28,22 @@ import (
2828// Is determines whether one of the causes of the given error or any
2929// of its causes is equivalent to some reference error.
3030//
31+ // As in the Go standard library, an error is considered to match a
32+ // reference error if it is equal to that target or if it implements a
33+ // method Is(error) bool such that Is(reference) returns true.
34+ //
35+ // Note: the inverse is not true - making an Is(reference) method
36+ // return false does not imply that errors.Is() also returns
37+ // false. Errors can be equal because their network equality marker is
38+ // the same. To force errors to appear different to Is(), use
39+ // errors.Mark().
40+ //
3141// Note: if any of the error types has been migrated from a previous
3242// package location or a different type, ensure that
3343// RegisterTypeMigration() was called prior to Is().
3444func Is (err , reference error ) bool {
3545 if reference == nil {
36- return err == reference
46+ return err == nil
3747 }
3848
3949 // Direct reference comparison is the fastest, and most
@@ -42,6 +52,11 @@ func Is(err, reference error) bool {
4252 if c == reference {
4353 return true
4454 }
55+ // Compatibility with std go errors: if the error object itself
56+ // implements Is(), try to use that.
57+ if tryDelegateToIsMethod (c , reference ) {
58+ return true
59+ }
4560 }
4661
4762 if err == nil {
@@ -67,6 +82,13 @@ func Is(err, reference error) bool {
6782 return false
6883}
6984
85+ func tryDelegateToIsMethod (err , reference error ) bool {
86+ if x , ok := err .(interface { Is (error ) bool }); ok && x .Is (reference ) {
87+ return true
88+ }
89+ return false
90+ }
91+
7092// HasType returns true iff err contains an error whose concrete type
7193// matches that of referenceType.
7294func HasType (err error , referenceType error ) bool {
@@ -125,6 +147,11 @@ func IsAny(err error, references ...error) bool {
125147 if c == refErr {
126148 return true
127149 }
150+ // Compatibility with std go errors: if the error object itself
151+ // implements Is(), try to use that.
152+ if tryDelegateToIsMethod (c , refErr ) {
153+ return true
154+ }
128155 }
129156 if c == nil {
130157 // This special case is to support a comparison to a nil
0 commit comments