Skip to content

Commit

Permalink
show fatal errors for first run when mail template errors
Browse files Browse the repository at this point in the history
  • Loading branch information
wxiaoguang committed Mar 1, 2024
1 parent 9bfa42a commit d2d1314
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions modules/templates/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package templates

import (
"context"
"fmt"
"html/template"
"regexp"
"strings"
Expand Down Expand Up @@ -33,7 +34,7 @@ func mailSubjectTextFuncMap() texttmpl.FuncMap {
}
}

func buildSubjectBodyTemplate(stpl *texttmpl.Template, btpl *template.Template, name string, content []byte) {
func buildSubjectBodyTemplate(stpl *texttmpl.Template, btpl *template.Template, name string, content []byte) error {
// Split template into subject and body
var subjectContent []byte
bodyContent := content
Expand All @@ -42,20 +43,13 @@ func buildSubjectBodyTemplate(stpl *texttmpl.Template, btpl *template.Template,
subjectContent = content[0:loc[0]]
bodyContent = content[loc[1]:]
}
if _, err := stpl.New(name).
Parse(string(subjectContent)); err != nil {
log.Error("Failed to parse template [%s/subject]: %v", name, err)
if !setting.IsProd {
log.Fatal("Please fix the mail template error")
}
if _, err := stpl.New(name).Parse(string(subjectContent)); err != nil {
return fmt.Errorf("failed to parse template [%s/subject]: %w", name, err)
}
if _, err := btpl.New(name).
Parse(string(bodyContent)); err != nil {
log.Error("Failed to parse template [%s/body]: %v", name, err)
if !setting.IsProd {
log.Fatal("Please fix the mail template error")
}
if _, err := btpl.New(name).Parse(string(bodyContent)); err != nil {
return fmt.Errorf("failed to parse template [%s/body]: %w", name, err)
}
return nil
}

// Mailer provides the templates required for sending notification mails.
Expand All @@ -64,14 +58,7 @@ func Mailer(ctx context.Context) (*texttmpl.Template, *template.Template) {
bodyTemplates := template.New("")

subjectTemplates.Funcs(mailSubjectTextFuncMap())
// To do the best to avoid serious breaking, add some functions back for body templates.
// Keep in mind that some behaviors have changed, for worse case, double-escaping.
// Custom template users should migrate to the new template system ASAP.
bodyTemplateFuncMap := NewFuncMap()
bodyTemplateFuncMap["Safe"] = SafeHTML
bodyTemplateFuncMap["Escape"] = HTMLEscape
bodyTemplateFuncMap["Str2html"] = SanitizeHTML
bodyTemplates.Funcs(bodyTemplateFuncMap)
bodyTemplates.Funcs(NewFuncMap())

assetFS := AssetFS()
refreshTemplates := func(firstRun bool) {
Expand All @@ -94,7 +81,13 @@ func Mailer(ctx context.Context) (*texttmpl.Template, *template.Template) {
if firstRun {
log.Trace("Adding mail template %s: %s by %s", tmplName, assetPath, layerName)
}
buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, tmplName, content)
if err = buildSubjectBodyTemplate(subjectTemplates, bodyTemplates, tmplName, content); err != nil {
if firstRun {
log.Fatal("Failed to parse mail template, err: %v", err)
} else {
log.Error("Failed to parse mail template, err: %v", err)
}
}
}
}

Expand Down

0 comments on commit d2d1314

Please sign in to comment.