go 1.13: errors.As(...)
Use return values on errors behaviours when testing with errors.As(...)
For an overview of go1.13 errors package, read the go blog post. It describes two new functions errors.Is(...)
which operates like an equality check and errors.As(...)
which operates like a type assertion.
The example in the blog lists:
|
|
Dave Cheney describes error handling patterns in this blog post and recommends omitting constants and sentinel errors in favour of errors with behaviours.
Assert errors for behaviour, not type
|
|
But we can extend this example to use the go1.13 errors package.
|
|
It might be tempting to define the temporary
interface without a return type:
|
|
We shouldn’t do this as it reduces our ability to reuse a single error type for multiple cases.
|
|
We will not be able to differentiate between when MyError is being used for Temporary
errors and
when it’s being used for NotFound
errors, as the MyError
type will satisfy both of those
interfaces.
Including a return type in the method definitions allows us to check if the behaviour is satisfied on the error type, and if the error returned represents that behaviour.
|
|
Now we can reuse the single MyError
definition and specify if it represents a Temporary
or
NotFound
error.