Skip to content

Commit

Permalink
notif: enhance error message for JSON decode response issues
Browse files Browse the repository at this point in the history
  • Loading branch information
crazy-max committed Dec 18, 2024
1 parent 0990a69 commit 3723a6f
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 11 deletions.
5 changes: 2 additions & 3 deletions internal/notif/gotify/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
5 changes: 2 additions & 3 deletions internal/notif/ntfy/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
7 changes: 2 additions & 5 deletions internal/notif/rocketchat/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 3723a6f

Please sign in to comment.