{"record":{"id":"0a07208db572747b","repo":"gohugoio/hugo","slug":"failed-to-convert-s-to-string-w","errorCode":null,"errorMessage":"failed to convert s to string: %w","messagePattern":"failed to convert s to string: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/strings/strings.go","lineNumber":117,"sourceCode":"\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:\n\t\treturn template.HTML(res), nil\n\tdefault:\n\t\treturn res, nil\n\t}","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/gohugoio/hugo/blob/52c9bd7908b4d02d4d0ff8f82a888834d6ee10d2/tpl/strings/strings.go#L99-L135","documentation":"Thrown by strings.Count when cast.ToStringE fails on the second argument (s, the haystack). Mirrors error 732 but for the second positional argument.","triggerScenarios":"Calling {{ strings.Count $substr $s }} where $s (the haystack) is a non-stringifiable type such as a struct, map, slice, or nil.","commonSituations":"Passing a page object or params map as the haystack instead of .Content/.Plain, or passing nil from an unset variable.","solutions":["Pass a string as the haystack (e.g. .Plain or .Content).","Confirm the second argument is bound to a string before the call.","Default nil haystacks to an empty string."],"exampleFix":"// before:\n{{ strings.Count \"foo\" . }}\n// after:\n{{ strings.Count \"foo\" .Plain }}","handlingStrategy":"type-guard","validationCode":"{{ with .Plain }}{{ strings.Count $needle . }}{{ else }}0{{ end }}","typeGuard":"{{ if reflect.IsMap . }}0{{ else }}{{ strings.Count $needle . }}{{ end }}","tryCatchPattern":null,"preventionTips":["Pass a string haystack (.Plain/.Content), not a page object.","Use 'with' to guard nil/empty haystacks.","Confirm the variable holds a string before the call."],"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"}