publish: Try to decode ntfy error string

This commit is contained in:
Thorben Günther 2023-07-07 23:20:03 +02:00
parent 7efc6cdbc2
commit dc9078c3f5
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED

10
main.go
View file

@ -58,6 +58,10 @@ type notification struct {
status string status string
} }
type ntfyError struct {
Error string `json:"error"`
}
func (br *bridge) singleAlertNotifications(p *payload) []*notification { func (br *bridge) singleAlertNotifications(p *payload) []*notification {
var notifications []*notification var notifications []*notification
for _, alert := range p.Alerts { for _, alert := range p.Alerts {
@ -321,9 +325,15 @@ func (br *bridge) publish(n *notification) error {
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
var ntfyError ntfyError
if err := json.NewDecoder(resp.Body).Decode(&ntfyError); err != nil {
br.logger.Debugf("Publish: failed to decode error: %v", err)
return fmt.Errorf("ntfy: received status code %d", resp.StatusCode) return fmt.Errorf("ntfy: received status code %d", resp.StatusCode)
} }
return fmt.Errorf("ntfy: %s (status code %d)", ntfyError.Error, resp.StatusCode)
}
return nil return nil
} }