{"id":"8fc52a32a94a89d7","repo":"gohugoio/hugo","slug":"error-in-parse-w","errorCode":null,"errorMessage":"error in Parse: %w","messagePattern":"error in Parse: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"tpl/urls/urls.go","lineNumber":56,"sourceCode":"\tmultihost bool\n}\n\n// AbsURL takes the string s and converts it to an absolute URL.\nfunc (ns *Namespace) AbsURL(s any) (string, error) {\n\tss, err := cast.ToStringE(s)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\treturn ns.deps.PathSpec.AbsURL(ss, false), nil\n}\n\n// Parse parses rawurl into a URL structure. The rawurl may be relative or\n// absolute.\nfunc (ns *Namespace) Parse(rawurl any) (*url.URL, error) {\n\ts, err := cast.ToStringE(rawurl)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error in Parse: %w\", err)\n\t}\n\n\treturn url.Parse(s)\n}\n\n// RelURL takes the string s and prepends the relative path according to a\n// page's position in the project directory structure.\nfunc (ns *Namespace) RelURL(s any) (string, error) {\n\tss, err := cast.ToStringE(s)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\n\treturn ns.deps.PathSpec.RelURL(ss, false), nil\n}\n\n// URLize returns the strings s formatted as an URL.\nfunc (ns *Namespace) URLize(s any) (string, error) {","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/gohugoio/hugo/blob/8a468df065a75c1c7cf9f6850f32148746590ea5/tpl/urls/urls.go#L38-L74","documentation":"`urls.Parse` first coerces its argument to a string with `cast.ToStringE`; if the value can't be stringified (maps, slices, structs like Pages), the cast error is wrapped as 'error in Parse'. Note the actual URL parsing error from `url.Parse` is returned separately — this one is purely a type-conversion failure.","triggerScenarios":"`{{ urls.Parse .Params.links }}` where the param is a map or slice, `{{ urls.Parse . }}` inside a range over complex objects, or passing a Page/Resource object instead of its `.Permalink` string.","commonSituations":"Front matter fields that are lists (`urls: [a, b]`) passed whole to Parse; passing a Page object rather than `.Permalink`/`.RelPermalink`; unexpected data shapes from `site.Data`.","solutions":["Pass a string: `{{ $u := urls.Parse .Params.link }}` where link is a scalar string.","For Pages/Resources, parse the permalink: `{{ urls.Parse $p.Permalink }}`.","If the value is a list, range over it and Parse each element."],"exampleFix":"{{/* before */}}\n{{ $u := urls.Parse $page }}\n{{/* after */}}\n{{ $u := urls.Parse $page.Permalink }}","handlingStrategy":"try-catch","validationCode":"{{ $u := trim $input \" \" }}\n{{ if $u }}{{ $parsed := urls.Parse $u }}{{ end }}","typeGuard":null,"tryCatchPattern":"{{ with try (urls.Parse $raw) }}\n  {{ with .Err }}{{ warnf \"unparsable URL %q: %s\" $raw . }}{{ else }}{{ $u := .Value }}{{ end }}\n{{ end }}","preventionTips":["Trim whitespace and control characters from URLs before parsing — spaces and newlines from front matter are the usual cause","Don't pass raw user/front-matter values straight to urls.Parse; validate they look like URLs first (e.g. findRE `^https?://`)","Percent-encode illegal characters (querify/urlquery) before building URLs"],"tags":["hugo","templates","urls","type-error"],"analyzedSha":"8a468df065a75c1c7cf9f6850f32148746590ea5","analyzedAt":"2026-07-31T21:23:07.045Z","schemaVersion":2}