{"record":{"id":"aafd21ba57f7a488","repo":"hashicorp/nomad","slug":"error-formatting-the-data-w","errorCode":null,"errorMessage":"Error formatting the data: %w","messagePattern":"Error formatting the data: %w","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"command/data_format.go","lineNumber":97,"sourceCode":"\tvar format string\n\tif json && len(template) > 0 {\n\t\treturn \"\", fmt.Errorf(\"Both json and template formatting are not allowed\")\n\t} else if json {\n\t\tformat = \"json\"\n\t} else if len(template) > 0 {\n\t\tformat = \"template\"\n\t} else {\n\t\treturn \"\", fmt.Errorf(\"no formatting option given\")\n\t}\n\n\tf, err := DataFormat(format, template)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tout, err := f.TransformData(data)\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"Error formatting the data: %w\", err)\n\t}\n\n\treturn out, nil\n}\n\nfunc makeFuncMap() template.FuncMap {\n\tfm := template.FuncMap{}\n\n\t// Add the Sprig functions to the funcmap. These functions are decorated\n\t// with `sprig_` to match how they are treated in consul-template\n\tfor k, v := range sprig.FuncMap() {\n\t\ttarget := \"sprig_\" + k\n\t\tfm[target] = v\n\t}\n\n\treturn fm\n}\n","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/hashicorp/nomad/blob/482b49bf1aec006f089bcfc7e632d8f6ac303e5e/command/data_format.go#L79-L115","documentation":"After Format successfully resolves a DataFormat, it applies f.TransformData(data) to render the output. If rendering fails (bad Go template syntax, unknown template function, or JSON marshaling problems), the underlying error is wrapped as \"Error formatting the data: %w\".","triggerScenarios":"Calling Format with template=true path where the -template string is invalid Go template syntax, references an undefined function, or the data cannot be transformed by the json/templating backend.","commonSituations":"Users hand-write go template strings with typos (e.g. {{.Job.IDX}}), use a sprig/template function name that isn't in makeFuncMap, or pipe output where the data shape differs from what the template expects.","solutions":["Read the wrapped cause after the colon and fix the template syntax/function name accordingly","Validate the template with `nomad job inspect -t '<template>'` or `go template` parse locally before scripting against it","Switch to -json output and post-process with jq if the template keeps failing"],"exampleFix":"// before\nnomad status -t '{{.JobIDX}}' <id>\n// after\nnomad status -t '{{ range . }}{{ .ID }}{{ end }}' <id>","handlingStrategy":"try-catch","validationCode":"// parse the template first in tests:\nif _, err := template.New(\"t\").Funcs(makeFuncMap()).Parse(tmpl); err != nil {\n    return err\n}","typeGuard":null,"tryCatchPattern":"out, err := Format(data, json, tmpl)\nif err != nil {\n    var wf interface{ Unwrap() error }\n    if errors.As(err, &target) { /* inspect wrapped cause */ }\n    return fmt.Errorf(\"check -template syntax: %w\", err)\n}","preventionTips":["Test templates against sample JSON before using them in scripts","Only use functions present in makeFuncMap","Prefer -json + jq for complex transformations"],"tags":["cli","templating","go-templates"],"backgroundTag":"template-render-failed","analyzedSha":"482b49bf1aec006f089bcfc7e632d8f6ac303e5e","analyzedAt":"2026-09-04T07:54:14.808Z","contentChangedAt":"2026-09-04T07:54:14.808Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}