From 24819dabe1d276eb2f28c3696bd780c47dcdb4e8 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff <1389908+richvdh@users.noreply.github.com> Date: Thu, 2 Jan 2020 10:57:28 +0000 Subject: [PATCH] Better error handling for the alertmanager service (#306) * when there is a problem with the templates in the service, say what the problem is. * when there is a problem interpolating the template, log the error and return a 500. --- .../go-neb/services/alertmanager/alertmanager.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/github.com/matrix-org/go-neb/services/alertmanager/alertmanager.go b/src/github.com/matrix-org/go-neb/services/alertmanager/alertmanager.go index 1207772..64c8000 100644 --- a/src/github.com/matrix-org/go-neb/services/alertmanager/alertmanager.go +++ b/src/github.com/matrix-org/go-neb/services/alertmanager/alertmanager.go @@ -87,12 +87,20 @@ func (s *Service) OnReceiveWebhook(w http.ResponseWriter, req *http.Request, cli // we don't check whether the templates parse because we already did when storing them in the db textTemplate, _ := text.New("textTemplate").Parse(templates.TextTemplate) var bodyBuffer bytes.Buffer - textTemplate.Execute(&bodyBuffer, notif) + if err := textTemplate.Execute(&bodyBuffer, notif); err != nil { + log.WithError(err).Error("Alertmanager webhook failed to execute text template") + w.WriteHeader(500) + return + } if templates.HTMLTemplate != "" { // we don't check whether the templates parse because we already did when storing them in the db htmlTemplate, _ := html.New("htmlTemplate").Parse(templates.HTMLTemplate) var formattedBodyBuffer bytes.Buffer - htmlTemplate.Execute(&formattedBodyBuffer, notif) + if err := htmlTemplate.Execute(&formattedBodyBuffer, notif); err != nil { + log.WithError(err).Error("Alertmanager webhook failed to execute HTML template") + w.WriteHeader(500) + return + } msg = gomatrix.HTMLMessage{ Body: bodyBuffer.String(), MsgType: templates.MsgType, @@ -130,14 +138,14 @@ func (s *Service) Register(oldService types.Service, client *gomatrix.Client) er // validate the plain text template is valid _, err := text.New("textTemplate").Parse(templates.TextTemplate) if err != nil { - return fmt.Errorf("plain text template is invalid") + return fmt.Errorf("plain text template is invalid: %v", err) } if templates.HTMLTemplate != "" { // validate that the html template is valid _, err := html.New("htmlTemplate").Parse(templates.HTMLTemplate) if err != nil { - return fmt.Errorf("html template is invalid") + return fmt.Errorf("html template is invalid: %v", err) } } // validate that the msgtype is either m.notice or m.text