Skip to content

Commit 61dac9f

Browse files
authored
Merge pull request #1309 from crazy-max/notif-ret-http-error
notif: enhance error message for JSON decode response issues
2 parents 4db25fc + 3723a6f commit 61dac9f

File tree

3 files changed

+6
-11
lines changed

3 files changed

+6
-11
lines changed

internal/notif/gotify/client.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,8 @@ func (c *Client) Send(entry model.NotifEntry) error {
114114
ErrorCode int `json:"errorCode"`
115115
ErrorDescription string `json:"errorDescription"`
116116
}
117-
err := json.NewDecoder(resp.Body).Decode(&errBody)
118-
if err != nil {
119-
return err
117+
if err := json.NewDecoder(resp.Body).Decode(&errBody); err != nil {
118+
return errors.Wrapf(err, "cannot decode JSON error response for HTTP %d %s status", resp.StatusCode, http.StatusText(resp.StatusCode))
120119
}
121120
return errors.Errorf("%d %s: %s", errBody.ErrorCode, errBody.Error, errBody.ErrorDescription)
122121
}

internal/notif/ntfy/client.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,8 @@ func (c *Client) Send(entry model.NotifEntry) error {
108108
ErrorCode int `json:"errorCode"`
109109
ErrorDescription string `json:"errorDescription"`
110110
}
111-
err := json.NewDecoder(resp.Body).Decode(&errBody)
112-
if err != nil {
113-
return err
111+
if err := json.NewDecoder(resp.Body).Decode(&errBody); err != nil {
112+
return errors.Wrapf(err, "cannot decode JSON error response for HTTP %d %s status", resp.StatusCode, http.StatusText(resp.StatusCode))
114113
}
115114
return errors.Errorf("%d %s: %s", errBody.ErrorCode, errBody.Error, errBody.ErrorDescription)
116115
}

internal/notif/rocketchat/client.go

+2-5
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,11 @@ func (c *Client) Send(entry model.NotifEntry) error {
147147
Error string `json:"error,omitempty"`
148148
ErrorType string `json:"errorType,omitempty"`
149149
}
150-
err = json.NewDecoder(resp.Body).Decode(&respBody)
151-
if err == nil {
152-
return err
150+
if err = json.NewDecoder(resp.Body).Decode(&respBody); err != nil {
151+
return errors.Wrapf(err, "cannot decode JSON body response for HTTP %d %s status", resp.StatusCode, http.StatusText(resp.StatusCode))
153152
}
154-
155153
if resp.StatusCode != http.StatusOK {
156154
return errors.Errorf("unexpected HTTP error %d: %s", resp.StatusCode, respBody.ErrorType)
157155
}
158-
159156
return nil
160157
}

0 commit comments

Comments
 (0)