8 ms·
A string check for an error type in a typed language is a shortcut/laziness really. There's more complex ways (which are less elegant) to typecast the error typ
by codebeaker 9y ago
A string check for an error type in a typed language is a shortcut/laziness really. There's more complex ways (which are less elegant) to typecast the error type into a "concrete" type and figure out if it's really the issue you are looking for.
In Go error is an interface type that is expected to implement the `Error() string {}` method. That makes it quite trivial to implement a ErrFunctionDoesNotExist type which wraps the native "error" types returned by the lower down functions, and compare against it with a type assertion.
Doing this a lot gets really old, however so maybe the author took a shortcut. If that's the case, at least it's greppable.
For what it's worth there's a technique in use throughout the Go stdlib which embues the `error` type with extra methods which allows you to check the specific fault with a url.Parse9), for example https://golang.org/pkg/net/url/#Error https://golang.org/pkg/net/url/#Error