{"record":{"id":"21dda8d497ebc9cd","repo":"amir20/dozzle","slug":"failed-to-execute-template-w","errorCode":null,"errorMessage":"failed to execute template: %w","messagePattern":"failed to execute template: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/notification/dispatcher/webhook.go","lineNumber":286,"sourceCode":"\t}\n\n\treturn TestResult{Success: true, StatusCode: resp.StatusCode}\n}\n\n// executeJSONTemplate parses the template as JSON, resolves Go template placeholders\n// in string values, and marshals back to JSON. This ensures all values are properly\n// JSON-escaped regardless of their content (e.g., log messages containing quotes or braces).\nfunc executeJSONTemplate(templateText string, data any) ([]byte, error) {\n\tvar structure any\n\tif err := json.Unmarshal([]byte(templateText), &structure); err != nil {\n\t\t// Not valid JSON — fall back to raw text/template execution\n\t\ttmpl, parseErr := template.New(\"webhook\").Parse(templateText)\n\t\tif parseErr != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to parse template: %w\", parseErr)\n\t\t}\n\t\tvar buf bytes.Buffer\n\t\tif execErr := tmpl.Execute(&buf, data); execErr != nil {\n\t\t\treturn nil, fmt.Errorf(\"failed to execute template: %w\", execErr)\n\t\t}\n\t\treturn buf.Bytes(), nil\n\t}\n\n\tresolved, err := resolveTemplateValues(structure, data)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\treturn json.Marshal(resolved)\n}\n\n// resolveTemplateValues recursively walks a JSON structure and executes\n// Go template expressions found in string values.\nfunc resolveTemplateValues(v any, data any) (any, error) {\n\tswitch val := v.(type) {\n\tcase map[string]any:\n\t\tresult := make(map[string]any, len(val))","sourceCodeStart":268,"sourceCodeEnd":304,"githubUrl":"https://github.com/amir20/dozzle/blob/d9463cbe21874e44ab79db6fa63e746ca7d22928/internal/notification/dispatcher/webhook.go#L268-L304","documentation":"executeJSONTemplate builds a Go text/template from the user-provided webhook JSON template and executes it against the notification data. This error is returned when template.Parsed content fails at execution time (tmpl.Execute returns an error), e.g. when the template references a field or function not present in the data map, or an invokable value is not a function. Parsing succeeded but rendering did not, so the webhook payload could not be produced.","triggerScenarios":"A webhook dispatcher template contains {{ ... }} actions referencing missing keys in the data map (e.g. {{ .NonExistentField }}), calls a nonexistent template function, or invokes a non-function value. Triggered via SendTest and the other executeJSONTemplate callers (tests: TestExecuteJSONTemplate_EscapesQuotes, _EscapesNewlines, _EscapesBackslashes, _MultiplePlaceholders, _NestedObjects).","commonSituations":"Users hand-edit the webhook template in the notification UI and typo a field name, use Go pipeline syntax the data doesn't satisfy, or reference keys that were renamed in a newer Dozzle version.","solutions":["Check the wrapped execErr (the %w cause) to see which field or function the template referenced that does not exist.","Correct the template placeholders so every {{ .Field }} matches a key in the notification data structure.","Remove or replace calls to undefined template functions; only built-in text/template funcs are available.","Validate the template by sending a test notification (SendTest) before saving it to production config."],"exampleFix":"// before (template references a field that doesn't exist)\n{\"text\": \"Container {{ .ContainerNamee }} failed\"}\n// after\n{\"text\": \"Container {{ .ContainerName }} failed\"}","handlingStrategy":"try-catch","validationCode":"// Pre-check: every referenced field exists in data\nfor field := range referencedFields(templateText) {\n    if _, ok := data[field]; !ok {\n        return fmt.Errorf(\"template references missing field %q\", field)\n    }\n}","typeGuard":null,"tryCatchPattern":"payload, err := executeJSONTemplate(structure, data)\nif err != nil {\n    var wrapped *fmt.WrapError\n    if errors.As(err, &templateExecErr{}) {\n        log.Printf(\"fix webhook template placeholders: %v\", err)\n    }\n    return fmt.Errorf(\"webhook not sent: %w\", err)\n}","preventionTips":["Keep a documented list of available data fields next to the webhook config.","Always run SendTest after editing a template.","Avoid deep optional paths unless guarded with {{ if }}."],"tags":["webhook","template","notifications"],"backgroundTag":"template-execution-failed","analyzedSha":"d9463cbe21874e44ab79db6fa63e746ca7d22928","analyzedAt":"2026-09-07T10:08:55.855Z","contentChangedAt":"2026-09-07T10:08:55.855Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}