From f58e244cf60d00f7600972ef0240d612b05ba91d Mon Sep 17 00:00:00 2001 From: Toon Schoenmakers Date: Thu, 29 Oct 2020 17:33:55 +0100 Subject: [PATCH] Handle errors from the grafana api by displaying the error message (#1015) --- modules/grafana/client.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/modules/grafana/client.go b/modules/grafana/client.go index 02e5c7d8..63a678c2 100644 --- a/modules/grafana/client.go +++ b/modules/grafana/client.go @@ -3,6 +3,7 @@ package grafana import ( "bytes" "encoding/json" + "errors" "fmt" "net/http" "sort" @@ -91,6 +92,17 @@ func (client *Client) Alerts() ([]Alert, error) { } defer func() { _ = res.Body.Close() }() + if res.StatusCode != 200 { + msg := struct { + Msg string `json:"message"` + }{} + err = utils.ParseJSON(&msg, res.Body) + if err != nil { + return nil, err + } + return nil, errors.New(msg.Msg) + } + var out []Alert err = utils.ParseJSON(&out, res.Body) if err != nil {