mirror of
https://github.com/pinpox/gitea-matrix-bot
synced 2024-11-26 08:43:47 +01:00
error handling for missing template fields
This commit is contained in:
parent
98f0043bfa
commit
bb79e34417
26
listener.go
26
listener.go
@ -52,10 +52,18 @@ func PostHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
//TODO check for configured message type
|
//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 {
|
switch msgType {
|
||||||
case "html":
|
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)
|
mygiteabot.SendHTMLToRoom(room, messageHTML, messageText)
|
||||||
case "plain":
|
case "plain":
|
||||||
mygiteabot.SendTextToRoom(room, messageText)
|
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
|
//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 := `<h3><a href="{{.Repository.HTMLURL}}"><b><span data-mx-color="#000000">[{{.Repository.FullName}}]</span></b></a></h3>`
|
templHeader := `<h3><a href="{{.Repository.HTMLURL}}"><b><span data-mx-color="#000000">[{{.Repository.FullName}}]</span></b></a></h3>`
|
||||||
|
|
||||||
@ -155,14 +163,15 @@ func generateHTMLMessage(data GiteaPostData, eventHeader string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := templ.Execute(&tpl, data); err != nil {
|
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
|
//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")
|
templ := template.New("notification")
|
||||||
var tpl bytes.Buffer
|
var tpl bytes.Buffer
|
||||||
@ -231,8 +240,9 @@ func generateTextMessage(data GiteaPostData, eventHeader string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := templ.Execute(&tpl, data); err != nil {
|
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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user