Skip to content

Commit

Permalink
show fatal errors in production 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 f626da6
Showing 1 changed file with 13 additions and 18 deletions.
31 changes: 13 additions & 18 deletions modules/templates/mailer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,20 @@ 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 {
// do not tolerate errors in production, but tolerate them in development (just like web templates)
if setting.IsProd {
log.Fatal("Failed to parse template [%s/subject]: %v", name, err)
} else {
log.Error("Failed to parse template [%s/subject]: %v", 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 {
// do not tolerate errors in production, but tolerate them in development (just like web templates)
if setting.IsProd {
log.Fatal("Failed to parse template [%s/body]: %v", name, err)
} else {
log.Error("Failed to parse template [%s/body]: %v", name, err)
}
}
}
Expand All @@ -64,14 +66,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 Down

0 comments on commit f626da6

Please sign in to comment.