assertions: correct the type assertion of ErrorAs#183
Merged
Conversation
After bumping the go directive version in go.mod to 1.21 the vet tool
is complaining about the ErrorAs assertion.
```
➜ go vet ./...
internal/assertions/assertions.go:208:6: second argument to errors.As must be a non-nil pointer to either a type that implements error, or to any interface type
```
The linter warning is a bit of a red-herring - in fact the `Target`
parameter for our `ErrorAs` assertion has been incorrect this whole time.
It should be `any`. Currently it is restricting types to be a concrete
type that implements the error interface, which is not a requirement of
`errors.As`.
In fact you can use errors.As to assert your err satisfies any interface
you choose - but the examples in the Go docs do not make this clear (imo).
For example you could have some error type:
```
type NetworkError interface {
Error() error
Timeout() bool
}
```
And you want to check if your error implements `Timeout()`, which you
can do using errors.As, e.g.
```
type IO interface {
Timeout() bool
}
var io IO
if errors.As(networkError, &io) {
timeout := io.Timeout()
// ...
}
```
In this toy example we use `errors.As` detect if the underlying error
(which is NetworkError) satisfies the IO interface, and if so, populates
`io` with our error value, and call its `Timeout()` method.
|
This made me cleanup a bunch of |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After bumping the go directive version in go.mod to 1.21 the vet tool
is complaining about the ErrorAs assertion.
The linter warning is a bit of a red-herring - in fact the
Targetparameter for our
ErrorAsassertion has been incorrect this whole time.It should be
any. Currently it is restricting types to be a concretetype that implements the error interface, which is not a requirement of
errors.As.In fact you can use
errors.Asto assert yourerrsatisfies any interfaceyou choose - but the examples in the Go docs do not make this clear (imo).
For example you could have some error type:
And you want to check if your error implements
Timeout(), which youcan do using
errors.As, e.g.In this toy example we use
errors.Asdetect if the underlying error(which is
NetworkError) satisfies theIOinterface, and if so, populatesiowith our error value, and call itsTimeout()method.