{"record":{"id":"9d589bde75899e46","repo":"gohugoio/hugo","slug":"error-at-calling-a-method-s-of-type-s-s","errorCode":null,"errorMessage":"error at calling a method %s of type %s: %s","messagePattern":"error at calling a method (.+?) of type (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/collections/where.go","lineNumber":355,"sourceCode":"\t\t}\n\n\t\tswitch {\n\t\tcase mt.PkgPath != \"\":\n\t\t\treturn zero, fmt.Errorf(\"%s is an unexported method of type %s\", elemName, typ)\n\t\tcase mt.Type.NumIn() > maxNumIn:\n\t\t\treturn zero, fmt.Errorf(\"%s is a method of type %s but requires more than %d parameter\", elemName, typ, maxNumIn)\n\t\tcase mt.Type.NumOut() == 0:\n\t\t\treturn zero, fmt.Errorf(\"%s is a method of type %s but returns no output\", elemName, typ)\n\t\tcase mt.Type.NumOut() > 2:\n\t\t\treturn zero, fmt.Errorf(\"%s is a method of type %s but returns more than 2 outputs\", elemName, typ)\n\t\tcase mt.Type.NumOut() == 1 && mt.Type.Out(0).Implements(errorType):\n\t\t\treturn zero, fmt.Errorf(\"%s is a method of type %s but only returns an error type\", elemName, typ)\n\t\tcase mt.Type.NumOut() == 2 && !mt.Type.Out(1).Implements(errorType):\n\t\t\treturn zero, fmt.Errorf(\"%s is a method of type %s returning two values but the second value is not an error type\", elemName, typ)\n\t\t}\n\t\tres := mt.Func.Call(args)\n\t\tif len(res) == 2 && !res[1].IsNil() {\n\t\t\treturn zero, fmt.Errorf(\"error at calling a method %s of type %s: %s\", elemName, typ, res[1].Interface().(error))\n\t\t}\n\t\treturn res[0], nil\n\t}\n\n\t// elemName isn't a method so next start to check whether it is\n\t// a struct field or a map value. In both cases, it mustn't be\n\t// a nil value\n\tif isNil {\n\t\treturn zero, fmt.Errorf(\"can't evaluate a nil pointer of type %s by a struct field or map key name %s\", typ, elemName)\n\t}\n\tobj = reflect.Indirect(obj)\n\tswitch obj.Kind() {\n\tcase reflect.Struct:\n\t\tft, ok := obj.Type().FieldByName(elemName)\n\t\tif ok {\n\t\t\tif ft.PkgPath != \"\" && !ft.Anonymous {\n\t\t\t\treturn zero, fmt.Errorf(\"%s is an unexported field of struct type %s\", elemName, typ)\n\t\t\t}","sourceCodeStart":337,"sourceCodeEnd":373,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/collections/where.go#L337-L373","documentation":"After passing all signature checks, `evaluateSubElem` calls the method via reflect (where.go:353) and, when the method has an error return that is non-nil, wraps and rethrows it with this message including the method name, type, and the underlying error. It means the chosen method executed but failed at runtime for at least one element in the collection.","triggerScenarios":"`{{ where $pages \"Load\" \"==\" v }}` where `Load(ctx) (T, error)` returns an error for some element — e.g. a missing file, parse failure, or nil receiver dereference inside the method.","commonSituations":"A lazy-loading method that fails for sparse data; environment differences (missing files in CI); a method that errors on nil sub-fields present in some elements.","solutions":["Make the called method nil/error-safe: return a zero value instead of an error where a missing value is expected.","Ensure all elements in the collection have the data the method needs (pre-load before templating).","Switch the where key to a plain field that is always populated, avoiding the failing method entirely."],"exampleFix":"// before\n{{ where $items \"Load\" \"==\" v }}  {{/* Load errors on some items */}}\n// after\n{{ where $items \"LoadedValue\" \"==\" v }}  {{/* field populated during data prep */}}","handlingStrategy":"fallback","validationCode":"// Go-side: call the method once over the whole collection before templating,\n// dropping or zeroing elements that error.\nfor i := range items {\n    v, err := items[i].Load(ctx)\n    if err != nil { items[i].LoadedValue = v /* zero */ }\n    items[i].LoadedValue = v\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Make template-called methods total: never error on missing optional data.","Pre-load all data in Go so the template path is error-free.","Log and skip failing elements during data prep rather than in templates."],"tags":["hugo","go-templates","where","reflection","runtime-error"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}