From f626da6576160ee62de57356db2f94ab0509ce82 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Fri, 1 Mar 2024 22:25:05 +0800 Subject: [PATCH] show fatal errors in production when mail template errors --- modules/templates/mailer.go | 31 +++++++++++++------------------ 1 file changed, 13 insertions(+), 18 deletions(-) diff --git a/modules/templates/mailer.go b/modules/templates/mailer.go index a728ba93da13c..cdaa8da19ec5a 100644 --- a/modules/templates/mailer.go +++ b/modules/templates/mailer.go @@ -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) } } } @@ -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) {