Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

notif: enhance error message for JSON decode response issues #1309

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading