From 3723a6f12b51c5f1b1166a99a072b55885996b1a Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Thu, 19 Dec 2024 00:57:05 +0100 Subject: [PATCH] notif: enhance error message for JSON decode response issues --- internal/notif/gotify/client.go | 5 ++--- internal/notif/ntfy/client.go | 5 ++--- internal/notif/rocketchat/client.go | 7 ++----- 3 files changed, 6 insertions(+), 11 deletions(-) diff --git a/internal/notif/gotify/client.go b/internal/notif/gotify/client.go index 094ebe7f7..de51bd075 100644 --- a/internal/notif/gotify/client.go +++ b/internal/notif/gotify/client.go @@ -114,9 +114,8 @@ func (c *Client) Send(entry model.NotifEntry) error { ErrorCode int `json:"errorCode"` ErrorDescription string `json:"errorDescription"` } - err := json.NewDecoder(resp.Body).Decode(&errBody) - if err != nil { - return err + if err := json.NewDecoder(resp.Body).Decode(&errBody); err != nil { + return errors.Wrapf(err, "cannot decode JSON error response for HTTP %d %s status", resp.StatusCode, http.StatusText(resp.StatusCode)) } return errors.Errorf("%d %s: %s", errBody.ErrorCode, errBody.Error, errBody.ErrorDescription) } diff --git a/internal/notif/ntfy/client.go b/internal/notif/ntfy/client.go index c664f0de2..dc26f64ae 100644 --- a/internal/notif/ntfy/client.go +++ b/internal/notif/ntfy/client.go @@ -108,9 +108,8 @@ func (c *Client) Send(entry model.NotifEntry) error { ErrorCode int `json:"errorCode"` ErrorDescription string `json:"errorDescription"` } - err := json.NewDecoder(resp.Body).Decode(&errBody) - if err != nil { - return err + if err := json.NewDecoder(resp.Body).Decode(&errBody); err != nil { + return errors.Wrapf(err, "cannot decode JSON error response for HTTP %d %s status", resp.StatusCode, http.StatusText(resp.StatusCode)) } return errors.Errorf("%d %s: %s", errBody.ErrorCode, errBody.Error, errBody.ErrorDescription) } diff --git a/internal/notif/rocketchat/client.go b/internal/notif/rocketchat/client.go index 40d65fc2b..cb20e94e4 100644 --- a/internal/notif/rocketchat/client.go +++ b/internal/notif/rocketchat/client.go @@ -147,14 +147,11 @@ func (c *Client) Send(entry model.NotifEntry) error { Error string `json:"error,omitempty"` ErrorType string `json:"errorType,omitempty"` } - err = json.NewDecoder(resp.Body).Decode(&respBody) - if err == nil { - return err + if err = json.NewDecoder(resp.Body).Decode(&respBody); err != nil { + return errors.Wrapf(err, "cannot decode JSON body response for HTTP %d %s status", resp.StatusCode, http.StatusText(resp.StatusCode)) } - if resp.StatusCode != http.StatusOK { return errors.Errorf("unexpected HTTP error %d: %s", resp.StatusCode, respBody.ErrorType) } - return nil }