{"id":"6ccbd92065fec36f","repo":"gohugoio/hugo","slug":"index-of-type-t-with-args-v-failed-s","errorCode":null,"errorMessage":"index of type %T with args %v failed: %s","messagePattern":"index of type %T with args (.+?) failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/index.go","lineNumber":37,"sourceCode":"\t\"reflect\"\n\n\t\"github.com/spf13/cast\"\n\n\t\"github.com/gohugoio/hugo/common/hmaps\"\n\t\"github.com/gohugoio/hugo/common/hreflect\"\n)\n\n// Index returns the result of indexing its first argument by the following\n// arguments. Thus \"index x 1 2 3\" is, in Go syntax, x[1][2][3]. Each\n// indexed item must be a map, slice, or array.\n//\n// Adapted from Go stdlib src/text/template/funcs.go.\n//\n// We deviate from the stdlib mostly because of https://github.com/golang/go/issues/14751.\nfunc (ns *Namespace) Index(item any, args ...any) (any, error) {\n\tv, err := ns.doIndex(item, args...)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"index of type %T with args %v failed: %s\", item, args, err)\n\t}\n\treturn v, nil\n}\n\nfunc (ns *Namespace) doIndex(item any, args ...any) (any, error) {\n\t// TODO(moorereason): merge upstream changes.\n\tv := reflect.ValueOf(item)\n\tif !v.IsValid() {\n\t\t// See issue 10489\n\t\t// This used to be an error.\n\t\treturn nil, nil\n\t}\n\n\tvar indices []any\n\n\tif len(args) == 1 {\n\t\tv := reflect.ValueOf(args[0])\n\t\tif v.Kind() == reflect.Slice {","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/collections/index.go#L19-L55","documentation":"This is the wrapper error for Hugo's `index` template function: any failure inside `doIndex` (unusable index type, unassignable map key, non-indexable value) is wrapped with the item's type and the arguments used. The underlying cause is in the trailing `%s`. Note that nil items and out-of-range indices deliberately return nil rather than erroring.","triggerScenarios":"`{{ index $item $key ... }}` where the key type is incompatible: indexing a slice with a string or float, indexing a map whose key type doesn't match the given key, or indexing a scalar (int, bool) that can't be indexed at all. Nested indexing like `index $x 1 \"a\"` fails at whichever level mismatches.","commonSituations":"Indexing a slice with a string from `.Params` (`index $list \"0\"` instead of 0), indexing site data with the wrong key type, passing a page or string where a map was expected, misusing `index` after `split`/`where` chains.","solutions":["Read the wrapped cause at the end of the message — it names the exact incompatibility.","Check the item type with `printf \"%T\"` and pass an index of the matching type (use `int` to convert string digits: `index $list (int $i)`).","For nested access into params/data, prefer dotted access or `index` with string keys only on maps.","Guard optional lookups with `with`/`isset` before indexing."],"exampleFix":"<!-- before -->\n{{ index $slice \"0\" }}\n<!-- after -->\n{{ index $slice 0 }}","handlingStrategy":"try-catch","validationCode":"{{ if isset $collection $key }}{{ index $collection $key }}{{ end }}","typeGuard":"{{ $indexable := or (reflect.IsMap $c) (reflect.IsSlice $c) }}","tryCatchPattern":"{{ with try (index $collection $key) }}{{ with .Err }}{{ warnf \"index failed: %s\" . }}{{ else }}{{ .Value }}{{ end }}{{ end }}","preventionTips":["Use isset to test the key/position exists before indexing","Ensure the key type matches the map's key type (string vs int)","Guard against indexing scalars or nil returned from .Param lookups"],"tags":["hugo","templates","index","type-mismatch"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}