~xenrox/ntfy-alertmanager

dc9078c3f524d3e802f7fab43e8a539e249a224f — Thorben Günther 10 months ago 7efc6cd
publish: Try to decode ntfy error string
1 files changed, 11 insertions(+), 1 deletions(-)

M main.go
M main.go => main.go +11 -1
@@ 58,6 58,10 @@ type notification struct {
	status       string
}

type ntfyError struct {
	Error string `json:"error"`
}

func (br *bridge) singleAlertNotifications(p *payload) []*notification {
	var notifications []*notification
	for _, alert := range p.Alerts {


@@ 321,7 325,13 @@ func (br *bridge) publish(n *notification) error {

	defer resp.Body.Close()
	if resp.StatusCode != http.StatusOK {
		return fmt.Errorf("ntfy: received status code %d", resp.StatusCode)
		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: %s (status code %d)", ntfyError.Error, resp.StatusCode)
	}

	return nil