Closed rlopzc closed 3 years ago
Hi Romario,
this errors
library does not yet recognize Is()
methods on the error object themselves.
This seems like a reasonable feature request so we can probably accommodate it.
In the meantime, you can work around this by implementing a method ErrorKeyMarker() string
which returns the Type
field of your error payload, and then errors.Is()
should work ok for your type.
@romariolopezc something to keep in mind though -- this library's errors.Is()
function uses an "identity marker" which is composed of message + type.
So in your example, even if there is a Is()
method, when this method returns false
two errors can still be considered equal as per errors.Is()
if they have the same message. So in your case you cannot use a custom Is()
method to "make two errors different" based on the separate Type
field.
Said in a different way, the rule (as per the go doc) is "if a.Is(b) is true, then errors.Is(a, b) is true",
not "if a.Is(b) is false, then errors.Is(a, b) is false" .
To obtain this last behavior, you'll need a ErrorKeyMarker()
method as per my message above, or use errors.Mark()
.
@knz Hey! Thank you for the explanation about the errors.Is usage. It makes sense what you said about the ErrorKeyMarker()
Btw thank you for the PR and prompt response!
When using
errors.Is(..)
theIs
method implementation is not called.Code example
Using cockroach
errors.Is(..)
doesn't call the Is method from above. If I instead use the"errors"
package then it does get called and my assertion passes.Am I missing something?
Thank you