{"record":{"id":"4ff9ca3d5db51004","repo":"glanceapp/glance","slug":"executing-template-w","errorCode":null,"errorMessage":"executing template: %w","messagePattern":"executing template: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/glance/utils.go","lineNumber":164,"sourceCode":"\treturn s\n}\n\nfunc fileServerWithCache(fs http.FileSystem, cacheDuration time.Duration) http.Handler {\n\tserver := http.FileServer(fs)\n\tcacheControlValue := fmt.Sprintf(\"public, max-age=%d\", int(cacheDuration.Seconds()))\n\n\treturn http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\t// TODO: fix always setting cache control even if the file doesn't exist\n\t\tw.Header().Set(\"Cache-Control\", cacheControlValue)\n\t\tserver.ServeHTTP(w, r)\n\t})\n}\n\nfunc executeTemplateToString(t *template.Template, data any) (string, error) {\n\tvar b bytes.Buffer\n\terr := t.Execute(&b, data)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"executing template: %w\", err)\n\t}\n\n\treturn b.String(), nil\n}\n\nfunc stringToBool(s string) bool {\n\treturn s == \"true\" || s == \"yes\"\n}\n\nfunc itemAtIndexOrDefault[T any](items []T, index int, def T) T {\n\tif index >= len(items) {\n\t\treturn def\n\t}\n\n\treturn items[index]\n}\n\nfunc ternary[T any](condition bool, a, b T) T {","sourceCodeStart":146,"sourceCodeEnd":182,"githubUrl":"https://github.com/glanceapp/glance/blob/91324e8de762702e97b0ac5c8e36271d644d8642/internal/glance/utils.go#L146-L182","documentation":"Thrown by executeTemplateToString in internal/glance/utils.go when template.Template.Execute fails while rendering a parsed template into a buffer. The template parsed fine, but at execution time it referenced a field or method that does not exist on the data value, dereferenced a nil pointer, indexed out of range, or a custom template function returned an error. The underlying text/template error identifies the approximate template line.","triggerScenarios":"Calling t.Execute(&b, data) where the template contains {{.Field}} but data's type lacks Field, {{range}} over a nil map/slice with a subsequent index operation, a nil pointer receiver method call, or a Funcs-registered function returning an error at render time.","commonSituations":"A glance widget template (page HTML or custom API widget template) edited by hand references a property that the widget struct does not expose; data passed as nil during a first render before update() populated it; template tested against one type but reused with another.","solutions":["Read the wrapped text/template error: it names the template line and the failing operation (bad pointer, wrong field name).","Grep the template for every {{.X}} / {{.X.Y}} and confirm each field exists on the struct passed as data.","Guard nil sub-structs in templates with {{if .Sub}} before dereferencing, or initialize them before render.","Reproduce locally with a tiny Go test: template.New(\"t\").Parse(tpl).Execute(&buf, data) to see the exact runtime error."],"exampleFix":"// before\n{{ if .Prices.USD }} ... {{ end }}\n\n// after\n{{ with .Prices }}{{ if .USD }} ... {{ end }}{{ end }}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"out, err := executeTemplateToString(tpl, data)\nif err != nil {\n    // wrapped text/template error names the failing template line\n    slog.Error(\"template render failed\", \"template\", tpl.Name(), \"error\", err)\n    return fallbackHTML // e.g. \"<div>render error</div>\"\n}","preventionTips":["Unit-test every template against the exact data type it will be executed with.","Prefer {{with}}/{{if}} guards before dereferencing optional sub-structs.","Keep custom template funcs total: never panic, always return error."],"tags":["go","template","rendering"],"backgroundTag":null,"analyzedSha":"91324e8de762702e97b0ac5c8e36271d644d8642","analyzedAt":"2026-08-15T14:12:54.279Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}