{"record":{"id":"96b40cffcae68498","repo":"googleapis/mcp-toolbox","slug":"error-executing-template-s-w","errorCode":null,"errorMessage":"error executing template '%s': %w","messagePattern":"error executing template '(.+?)': %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/util/parameters/common.go","lineNumber":107,"sourceCode":"func PopulateTemplate(templateName, templateString string, data map[string]any) (string, error) {\n\treturn PopulateTemplateWithFunc(templateName, templateString, data, nil)\n}\n\n// PopulateTemplateWithFunc populate a Go template with provided functions\nfunc PopulateTemplateWithFunc(templateName, templateString string, data map[string]any, funcMap template.FuncMap) (string, error) {\n\ttmpl := template.New(templateName)\n\tif funcMap != nil {\n\t\ttmpl = tmpl.Funcs(funcMap)\n\t}\n\n\tparsedTmpl, err := tmpl.Parse(templateString)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"error parsing template '%s': %w\", templateName, err)\n\t}\n\n\tvar result bytes.Buffer\n\tif err := parsedTmpl.Execute(&result, data); err != nil {\n\t\treturn \"\", fmt.Errorf(\"error executing template '%s': %w\", templateName, err)\n\t}\n\treturn result.String(), nil\n}\n\n// CheckDuplicateParameters verify there are no duplicate parameter names\nfunc CheckDuplicateParameters(ps Parameters) error {\n\tseenNames := make(map[string]bool)\n\tfor _, p := range ps {\n\t\tpName := p.GetName()\n\t\tif _, exists := seenNames[pName]; exists {\n\t\t\treturn fmt.Errorf(\"parameter name must be unique across all parameter fields. Duplicate parameter: %s\", pName)\n\t\t}\n\t\tseenNames[pName] = true\n\t}\n\treturn nil\n}\n","sourceCodeStart":89,"sourceCodeEnd":124,"githubUrl":"https://github.com/googleapis/mcp-toolbox/blob/8cc6e09de2ad7b8bffc77751799585a1401a48eb/internal/util/parameters/common.go#L89-L124","documentation":"After successfully parsing, PopulateTemplateWithFunc executes the template against the provided data map. This error wraps template.Execute failures: nil pointer dereferences on missing data, calling methods on wrong types, index out of range on pipelines, or custom funcs returning errors (like convertParamToJSON failures).","triggerScenarios":"Executing a syntactically valid template whose actions reference fields/functions incompatible with the supplied data — e.g. `{{param 'x'}}` where the json helper fails, or indexing a nil map entry.","commonSituations":"Runtime data shape differing from what the template expects; tool invoked without required params so data map keys are nil; custom template funcs returning errors for malformed params.","solutions":["Inspect the wrapped Execute error to find the failing action and data field","Ensure all params referenced in the template are present and non-nil in the data map","Fix custom function usage (e.g. json helper) so it receives marshalable values","Test the template with representative data to reproduce the failure locally"],"exampleFix":"// before\ntmpl := \"{{.missing.field}}\" // data has no \"missing\"\n// after\ntmpl := \"{{if .missing}}{{.missing.field}}{{end}}\"","handlingStrategy":"try-catch","validationCode":"func hasRequiredFields(tmplStr string, data map[string]any) error {\n    missing := []string{}\n    for _, f := range extractFieldRefs(tmplStr) {\n        if _, ok := data[f]; !ok {\n            missing = append(missing, f)\n        }\n    }\n    if len(missing) > 0 {\n        return fmt.Errorf(\"missing template data: %v\", missing)\n    }\n    return nil\n}","typeGuard":"null","tryCatchPattern":"out, err := PopulateTemplate(name, tmplStr, data)\nif err != nil {\n    if strings.Contains(err.Error(), \"executing template\") {\n        return nil, fmt.Errorf(\"template failed against supplied data: %w\", err)\n    }\n    return nil, err\n}","preventionTips":["Ensure all referenced params exist and are non-nil in the data map","Guard nil-able fields with {{if}} in templates","Render templates with representative test data before deployment"],"tags":["templates","runtime","parameters"],"backgroundTag":"template-execution-failed","analyzedSha":"8cc6e09de2ad7b8bffc77751799585a1401a48eb","analyzedAt":"2026-09-05T01:10:36.887Z","contentChangedAt":"2026-09-05T01:10:36.887Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}