glanceapp/glance · error

executing template: %w

Error message

executing template: %w

What it means

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.

Source

Thrown at internal/glance/utils.go:164

	return s
}

func fileServerWithCache(fs http.FileSystem, cacheDuration time.Duration) http.Handler {
	server := http.FileServer(fs)
	cacheControlValue := fmt.Sprintf("public, max-age=%d", int(cacheDuration.Seconds()))

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// TODO: fix always setting cache control even if the file doesn't exist
		w.Header().Set("Cache-Control", cacheControlValue)
		server.ServeHTTP(w, r)
	})
}

func executeTemplateToString(t *template.Template, data any) (string, error) {
	var b bytes.Buffer
	err := t.Execute(&b, data)
	if err != nil {
		return "", fmt.Errorf("executing template: %w", err)
	}

	return b.String(), nil
}

func stringToBool(s string) bool {
	return s == "true" || s == "yes"
}

func itemAtIndexOrDefault[T any](items []T, index int, def T) T {
	if index >= len(items) {
		return def
	}

	return items[index]
}

func ternary[T any](condition bool, a, b T) T {

View on GitHub (pinned to 91324e8de7)

Solutions

  1. Read the wrapped text/template error: it names the template line and the failing operation (bad pointer, wrong field name).
  2. Grep the template for every {{.X}} / {{.X.Y}} and confirm each field exists on the struct passed as data.
  3. Guard nil sub-structs in templates with {{if .Sub}} before dereferencing, or initialize them before render.
  4. Reproduce locally with a tiny Go test: template.New("t").Parse(tpl).Execute(&buf, data) to see the exact runtime error.

Example fix

// before
{{ if .Prices.USD }} ... {{ end }}

// after
{{ with .Prices }}{{ if .USD }} ... {{ end }}{{ end }}
Defensive patterns

Strategy: try-catch

Try / catch

out, err := executeTemplateToString(tpl, data)
if err != nil {
    // wrapped text/template error names the failing template line
    slog.Error("template render failed", "template", tpl.Name(), "error", err)
    return fallbackHTML // e.g. "<div>render error</div>"
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of glanceapp/glance@91324e8de7 (2026-08-15). Data as JSON: /api/errors/4ff9ca3d5db51004. Report an issue: GitHub.