gohugoio/hugo · error

error at calling a method %s of type %s: %s

Error message

error at calling a method %s of type %s: %s

What it means

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.

Source

Thrown at tpl/collections/where.go:355

		}

		switch {
		case mt.PkgPath != "":
			return zero, fmt.Errorf("%s is an unexported method of type %s", elemName, typ)
		case mt.Type.NumIn() > maxNumIn:
			return zero, fmt.Errorf("%s is a method of type %s but requires more than %d parameter", elemName, typ, maxNumIn)
		case mt.Type.NumOut() == 0:
			return zero, fmt.Errorf("%s is a method of type %s but returns no output", elemName, typ)
		case mt.Type.NumOut() > 2:
			return zero, fmt.Errorf("%s is a method of type %s but returns more than 2 outputs", elemName, typ)
		case mt.Type.NumOut() == 1 && mt.Type.Out(0).Implements(errorType):
			return zero, fmt.Errorf("%s is a method of type %s but only returns an error type", elemName, typ)
		case mt.Type.NumOut() == 2 && !mt.Type.Out(1).Implements(errorType):
			return zero, fmt.Errorf("%s is a method of type %s returning two values but the second value is not an error type", elemName, typ)
		}
		res := mt.Func.Call(args)
		if len(res) == 2 && !res[1].IsNil() {
			return zero, fmt.Errorf("error at calling a method %s of type %s: %s", elemName, typ, res[1].Interface().(error))
		}
		return res[0], nil
	}

	// elemName isn't a method so next start to check whether it is
	// a struct field or a map value. In both cases, it mustn't be
	// a nil value
	if isNil {
		return zero, fmt.Errorf("can't evaluate a nil pointer of type %s by a struct field or map key name %s", typ, elemName)
	}
	obj = reflect.Indirect(obj)
	switch obj.Kind() {
	case reflect.Struct:
		ft, ok := obj.Type().FieldByName(elemName)
		if ok {
			if ft.PkgPath != "" && !ft.Anonymous {
				return zero, fmt.Errorf("%s is an unexported field of struct type %s", elemName, typ)
			}

View on GitHub (pinned to 52c9bd7908)

Solutions

  1. Make the called method nil/error-safe: return a zero value instead of an error where a missing value is expected.
  2. Ensure all elements in the collection have the data the method needs (pre-load before templating).
  3. Switch the where key to a plain field that is always populated, avoiding the failing method entirely.

Example fix

// before
{{ where $items "Load" "==" v }}  {{/* Load errors on some items */}}
// after
{{ where $items "LoadedValue" "==" v }}  {{/* field populated during data prep */}}
Defensive patterns

Strategy: fallback

Validate before calling

// Go-side: call the method once over the whole collection before templating,
// dropping or zeroing elements that error.
for i := range items {
    v, err := items[i].Load(ctx)
    if err != nil { items[i].LoadedValue = v /* zero */ }
    items[i].LoadedValue = v
}

Prevention

When it happens

Trigger: `{{ 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.

Common situations: 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.

Related errors


AI-assisted analysis of gohugoio/hugo@52c9bd7908 (2026-08-09). Data as JSON: /api/errors/9d589bde75899e46. Report an issue: GitHub.