From df6860511797328b1bb1f23041e26c95931148d8 Mon Sep 17 00:00:00 2001 From: oldztimer <71163993+oldztimer@users.noreply.github.com> Date: Mon, 6 Jan 2025 17:02:23 -0800 Subject: [PATCH] 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. --- unifi/unifi.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/unifi/unifi.go b/unifi/unifi.go index 41b7748..5e5f5d6 100644 --- a/unifi/unifi.go +++ b/unifi/unifi.go @@ -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