From 66805bc2b6152df071950a01674543ef61162af9 Mon Sep 17 00:00:00 2001 From: Ben Visness Date: Mon, 17 Jun 2024 20:01:52 -0500 Subject: [PATCH] Don't completely die on template syntax errors --- src/templates/templates.go | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/templates/templates.go b/src/templates/templates.go index a43c48f0..c8043805 100644 --- a/src/templates/templates.go +++ b/src/templates/templates.go @@ -10,6 +10,7 @@ import ( "net/http" "path/filepath" "regexp" + "sort" "strings" "time" @@ -38,8 +39,9 @@ var embeddedTemplates map[string]*template.Template //go:embed src/fishbowls var FishbowlFS embed.FS -func getTemplatesFromFS(templateFS fs.ReadDirFS) map[string]*template.Template { +func getTemplatesFromFS(templateFS fs.ReadDirFS) (map[string]*template.Template, map[string]error) { templates := make(map[string]*template.Template) + errs := make(map[string]error) files := utils.Must1(templateFS.ReadDir("src")) for _, f := range files { @@ -53,7 +55,8 @@ func getTemplatesFromFS(templateFS fs.ReadDirFS) map[string]*template.Template { "src/"+f.Name(), ) if err != nil { - logging.Fatal().Str("filename", f.Name()).Err(err).Msg("failed to parse template") + errs[f.Name()] = err + continue } templates[f.Name()] = t @@ -70,17 +73,40 @@ func getTemplatesFromFS(templateFS fs.ReadDirFS) map[string]*template.Template { } } - return templates + return templates, errs } func Init() { - embeddedTemplates = getTemplatesFromFS(embeddedTemplateFs) + var errs map[string]error + type errEntry struct { + name string + err error + } + + embeddedTemplates, errs = getTemplatesFromFS(embeddedTemplateFs) + if len(errs) > 0 { + var errsList []errEntry + for filename, err := range errs { + errsList = append(errsList, errEntry{filename, err}) + } + sort.Slice(errsList, func(i, j int) bool { + return strings.Compare(errsList[i].name, errsList[j].name) < 0 + }) + for _, err := range errsList { + logging.Error().Str("filename", err.name).Err(err.err).Msg("Failed to parse template") + } + panic("Failed to parse templates; see above") + } } func GetTemplate(name string) *template.Template { var templates map[string]*template.Template if config.Config.DevConfig.LiveTemplates { - templates = getTemplatesFromFS(utils.DirFS("src/templates").(fs.ReadDirFS)) + var errs map[string]error + templates, errs = getTemplatesFromFS(utils.DirFS("src/templates").(fs.ReadDirFS)) + if errs[name] != nil { + panic(oops.New(errs[name], "Error in template %s", name)) + } } else { templates = embeddedTemplates }