Skip to content

Commit

Permalink
Add Error.Is interface method on the custom error type APIError. (#162)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
oldztimer authored Jan 7, 2025
1 parent 525ebe5 commit df68605
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions unifi/unifi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit df68605

Please sign in to comment.