{"record":{"id":"2a78c6eecf09f53b","repo":"thanos-io/thanos","slug":"failed-to-execute-the-template-w","errorCode":null,"errorMessage":"failed to execute the template: %w","messagePattern":"failed to execute the template: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/thanos/rule.go","lineNumber":1148,"sourceCode":"\t\treturn \"\", errors.Wrap(err, \"failed to parse template\")\n\t}\n\n\tvar buf bytes.Buffer\n\tif err := t.Execute(&buf, escapedExpr); err != nil {\n\t\treturn \"\", errors.Wrap(err, \"failed to execute template\")\n\t}\n\treturn buf.String(), nil\n}\n\nfunc validateTemplate(tmplStr string) error {\n\ttmpl, err := template.New(\"test\").Parse(tmplStr)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to parse the template: %w\", err)\n\t}\n\tvar buf bytes.Buffer\n\terr = tmpl.Execute(&buf, Expression{Expr: \"test_expr\"})\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to execute the template: %w\", err)\n\t}\n\treturn nil\n}\n\n// Filter out PromQL related warnings from warning response and keep store related warnings only.\nfunc filterOutPromQLWarnings(warns []string, logger log.Logger, query string) []string {\n\tstoreWarnings := make([]string, 0, len(warns))\n\tfor _, warn := range warns {\n\t\tif extannotations.IsPromQLAnnotation(warn) {\n\t\t\tlevel.Warn(logger).Log(\"warning\", warn, \"query\", query)\n\t\t\tcontinue\n\t\t}\n\t\tstoreWarnings = append(storeWarnings, warn)\n\t}\n\treturn storeWarnings\n}\n\n// ReadyScrapeManager allows a scrape manager to be retrieved. Even if it's set at a later point in time.","sourceCodeStart":1130,"sourceCodeEnd":1166,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/cmd/thanos/rule.go#L1130-L1166","documentation":"After successfully parsing the template, validateTemplate executes it with data Expression{Expr: \"test_expr\"} to ensure actions are valid against the actual data model. If Execute fails — typically because the template references fields that don't exist on Expression — it returns fmt.Errorf(\"failed to execute the template: %w\", err).","triggerScenarios":"validateTemplate receives a syntactically valid template that at execute time references a missing field (e.g. {{.Expr.Name}}, {{.Query}}), applies an incompatible pipeline, or calls a function with wrong argument types, causing tmpl.Execute(&buf, Expression{Expr: \"test_expr\"}) to error.","commonSituations":"Templates copied from Grafana or Prometheus rule templates that use different variables; typos in field names ({{.expr}} is a different, missing field since Go templates are case-sensitive and exported-field only); attempts to call methods Expression doesn't have.","solutions":["Restrict actions to {{.Expr}} — the only exported field on the Expression data passed in.","Remember Go template field access is case-sensitive and requires exported names: use {{.Expr}}, not {{.expr}} or {{.Expression}}.","Inspect the wrapped template.ExecError in the message to find the exact field/function that failed.","Simplify the template to a minimal {{.Expr}} version, confirm it validates, then add constructs back one at a time."],"exampleFix":"// before\ntmpl := \"/graph?g0.expr={{ .expr }}&g0.tab=1\"\n// after\ntmpl := \"/graph?g0.expr={{ .Expr }}&g0.tab=1\"","handlingStrategy":"validation","validationCode":"func executesOnTestData(tmplStr string) error {\n    t, err := template.New(\"test\").Parse(tmplStr)\n    if err != nil {\n        return err\n    }\n    var buf bytes.Buffer\n    return t.Execute(&buf, Expression{Expr: \"test_expr\"})\n}","typeGuard":"null","tryCatchPattern":"if err := executesOnTestData(tmplStr); err != nil {\n    var ee template.ExecError\n    if errors.As(err, &ee) {\n        log.Printf(\"template references invalid field/function: %v\", ee)\n    }\n}","preventionTips":["Only use {{.Expr}} in URL template actions","Execute the template against dummy data (Expression{Expr: \"test_expr\"}) in unit tests for any custom template","Avoid case mistakes: {{.expr}} fails while {{.Expr}} works"],"tags":["go","template","validation","thanos"],"backgroundTag":"template-execution-failed","analyzedSha":"35b8b991177def87ed52dcf10f9b6d87f07282c8","analyzedAt":"2026-09-07T01:49:59.689Z","contentChangedAt":"2026-09-07T01:49:59.689Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}