{"record":{"id":"7e746e23d33ea1b7","repo":"gohugoio/hugo","slug":"failed-to-convert-substr-to-string-w","errorCode":null,"errorMessage":"failed to convert substr to string: %w","messagePattern":"failed to convert substr to string: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/strings/strings.go","lineNumber":113,"sourceCode":"\tcounter := 0\n\tfor word := range strings.FieldsSeq(tpl.StripHTML(ss)) {\n\t\truneCount := utf8.RuneCountInString(word)\n\t\tif len(word) == runeCount {\n\t\t\tcounter++\n\t\t} else {\n\t\t\tcounter += runeCount\n\t\t}\n\t}\n\n\treturn counter, nil\n}\n\n// Count counts the number of non-overlapping instances of substr in s.\n// If substr is an empty string, Count returns 1 + the number of Unicode code points in s.\nfunc (ns *Namespace) Count(substr, s any) (int, error) {\n\tsubstrs, err := cast.ToStringE(substr)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"failed to convert substr to string: %w\", err)\n\t}\n\tss, err := cast.ToStringE(s)\n\tif err != nil {\n\t\treturn 0, fmt.Errorf(\"failed to convert s to string: %w\", err)\n\t}\n\treturn strings.Count(ss, substrs), nil\n}\n\n// Chomp returns a copy of s with all trailing newline characters removed.\nfunc (ns *Namespace) Chomp(s any) (any, error) {\n\tss, err := cast.ToStringE(s)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\tres := text.Chomp(ss)\n\tswitch s.(type) {\n\tcase template.HTML:","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/strings/strings.go#L95-L131","documentation":"Thrown by strings.Count when cast.ToStringE fails on the first argument (substr). The function counts non-overlapping occurrences of substr in s, so both must be string-coercible.","triggerScenarios":"Calling {{ strings.Count $substr $s }} where $substr is a struct, map, slice, numeric that cast rejects, or nil.","commonSituations":"Passing an object as the needle, swapping argument order expecting (haystack, needle), or passing a nil variable.","solutions":["Ensure the first argument is a string needle.","Mind argument order: strings.Count substr s (needle first, haystack second).","Default nil needles to an empty string if appropriate."],"exampleFix":"// before (wrong order / wrong type):\n{{ strings.Count .Content \"foo\" }}\n// after:\n{{ strings.Count \"foo\" .Content }}","handlingStrategy":"type-guard","validationCode":"{{ with $needle }}{{ strings.Count $needle $haystack }}{{ else }}0{{ end }}","typeGuard":"{{ if reflect.IsMap $needle }}0{{ else }}{{ strings.Count $needle $haystack }}{{ end }}","tryCatchPattern":null,"preventionTips":["Remember argument order: substr first, s second.","Ensure the needle is a string literal or string variable.","Guard optional needles with 'with'."],"tags":["strings","count","type-coercion","template"],"backgroundTag":null,"analyzedSha":"52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2","analyzedAt":"2026-08-09T21:49:36.660Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}