{"record":{"id":"cc5d9f759d321e62","repo":"thanos-io/thanos","slug":"failed-to-parse-the-template-w","errorCode":null,"errorMessage":"failed to parse the template: %w","messagePattern":"failed to parse the template: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cmd/thanos/rule.go","lineNumber":1143,"sourceCode":"\tescapedExpression := url.QueryEscape(expr)\n\n\tescapedExpr := Expression{Expr: escapedExpression}\n\tt, err := texttemplate.New(\"url\").Parse(tmpl)\n\tif err != nil {\n\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)","sourceCodeStart":1125,"sourceCodeEnd":1161,"githubUrl":"https://github.com/thanos-io/thanos/blob/35b8b991177def87ed52dcf10f9b6d87f07282c8/cmd/thanos/rule.go#L1125-L1161","documentation":"validateTemplate in cmd/thanos/rule.go parses a user-supplied URL template with template.New(\"test\").Parse to validate it at startup. If parsing fails it returns fmt.Errorf(\"failed to parse the template: %w\", err). This is the startup-time validation counterpart of the runtime URL-template parse error, meant to fail fast on bad templates.","triggerScenarios":"Calling validateTemplate(tmplStr) with a string that is not valid Go template syntax — unbalanced {{ }} delimiters, malformed actions, or unclosed comments — so Parse returns a parse error.","commonSituations":"Thanos rule starts with a bad --alert.query-template / URL template flag value; config management (Ansible/Helm) renders braces incorrectly; users mix Go template syntax with other templating dialects.","solutions":["Read the wrapped template.ParseError in the message: it gives the line/column and reason for the syntax failure.","Correct the template to valid Go text/template syntax with balanced {{ }} actions.","Reference only the .Expr field in actions so validation's Execute step (Expression{Expr: \"test_expr\"}) also passes.","Pre-validate the string with a quick `template.New(\"t\").Parse(s)` snippet in a Go playground before deploying."],"exampleFix":"// before\nvalidateTemplate(\"/graph?g0.expr={{ .Expr \")\n// after\nvalidateTemplate(\"/graph?g0.expr={{ .Expr }}\")","handlingStrategy":"validation","validationCode":"func validateTemplate(tmplStr string) error {\n    _, err := template.New(\"test\").Parse(tmplStr)\n    return err // nil means parseable\n}","typeGuard":"null","tryCatchPattern":"if err := validateTemplate(tmplStr); err != nil {\n    var pe *template.ParseError\n    if errors.As(err, &pe) {\n        log.Printf(\"template syntax error line %d col %d: %v\", pe.Line, pe.Column, pe)\n    }\n}","preventionTips":["Validate the template at config-load time, not just at startup failure","Store templates in config files with proper escaping rather than shell strings","Use errors.As with *template.ParseError to extract line/column diagnostics"],"tags":["go","template","validation","thanos"],"backgroundTag":"template-parse-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"}