Browse Source

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.
pull/276/merge
Richard van der Hoff 5 years ago
committed by GitHub
parent
commit
24819dabe1
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      src/github.com/matrix-org/go-neb/services/alertmanager/alertmanager.go

16
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

Loading…
Cancel
Save