Add Error.Is interface method on the custom error type APIError. (#162)
This method supports doing "errors.Is" checks so downstream can handle API errors more specifically. In my case, since I copied your lazyClient example, but I'm using it in a server, the Login needs to be refreshed if an API call errors out with "api.err.LoginRequired". There was no clean way to check for this specific error in order to refresh the login and retry.
This commit is contained in:
@@ -44,6 +44,16 @@ func (err *APIError) Error() string {
|
||||
return err.Message
|
||||
}
|
||||
|
||||
func (err *APIError) Is(target error) bool {
|
||||
var apiError *APIError
|
||||
if errors.As(target, &apiError) {
|
||||
if err.RC == apiError.RC && err.Message == apiError.Message {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type Client struct {
|
||||
// single thread client calls for CSRF, etc.
|
||||
sync.Mutex
|
||||
|
||||
Reference in New Issue
Block a user