From bb79e3441730c45fd94d750a83bc32a820a023e9 Mon Sep 17 00:00:00 2001 From: Pablo Ovelleiro Corral Date: Mon, 22 Apr 2019 20:34:05 +0200 Subject: [PATCH] error handling for missing template fields --- listener.go | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/listener.go b/listener.go index 485d7c7..7de4583 100644 --- a/listener.go +++ b/listener.go @@ -52,10 +52,18 @@ func PostHandler(w http.ResponseWriter, r *http.Request) { //TODO check for configured message type - messageText := generateTextMessage(postData, r.Header.Get("X-Gitea-Event")) + messageText, err := generateTextMessage(postData, r.Header.Get("X-Gitea-Event")) + if err != nil { + log.Warning(err) + log.Warning(postData) + } switch msgType { case "html": - messageHTML := generateHTMLMessage(postData, r.Header.Get("X-Gitea-Event")) + messageHTML, err := generateHTMLMessage(postData, r.Header.Get("X-Gitea-Event")) + if err != nil { + log.Warning(err) + log.Warning(postData) + } mygiteabot.SendHTMLToRoom(room, messageHTML, messageText) case "plain": mygiteabot.SendTextToRoom(room, messageText) @@ -73,7 +81,7 @@ func PostHandler(w http.ResponseWriter, r *http.Request) { } //generateTextMessage generates the message string for a given event -func generateHTMLMessage(data GiteaPostData, eventHeader string) string { +func generateHTMLMessage(data GiteaPostData, eventHeader string) (string, error) { templHeader := `

[{{.Repository.FullName}}]

` @@ -155,14 +163,15 @@ func generateHTMLMessage(data GiteaPostData, eventHeader string) string { } if err := templ.Execute(&tpl, data); err != nil { - log.Fatal(err) + log.Warningf("Failed to generate text-message for eventHeader %v", eventHeader) + return "", err } - return tpl.String() + return tpl.String(), nil } //generateTextMessage generates the message string for a given event -func generateTextMessage(data GiteaPostData, eventHeader string) string { +func generateTextMessage(data GiteaPostData, eventHeader string) (string, error) { templ := template.New("notification") var tpl bytes.Buffer @@ -231,8 +240,9 @@ func generateTextMessage(data GiteaPostData, eventHeader string) string { } if err := templ.Execute(&tpl, data); err != nil { - log.Fatal(err) + log.Warningf("Failed to generate text-message for eventHeader %v", eventHeader) + return "", err } - return tpl.String() + return tpl.String(), nil }