gohugoio/hugo · error
error in Parse: %w
Error message
error in Parse: %w
What it means
`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.
Source
Thrown at tpl/urls/urls.go:56
multihost bool
}
// AbsURL takes the string s and converts it to an absolute URL.
func (ns *Namespace) AbsURL(s any) (string, error) {
ss, err := cast.ToStringE(s)
if err != nil {
return "", err
}
return ns.deps.PathSpec.AbsURL(ss, false), nil
}
// Parse parses rawurl into a URL structure. The rawurl may be relative or
// absolute.
func (ns *Namespace) Parse(rawurl any) (*url.URL, error) {
s, err := cast.ToStringE(rawurl)
if err != nil {
return nil, fmt.Errorf("error in Parse: %w", err)
}
return url.Parse(s)
}
// RelURL takes the string s and prepends the relative path according to a
// page's position in the project directory structure.
func (ns *Namespace) RelURL(s any) (string, error) {
ss, err := cast.ToStringE(s)
if err != nil {
return "", err
}
return ns.deps.PathSpec.RelURL(ss, false), nil
}
// URLize returns the strings s formatted as an URL.
func (ns *Namespace) URLize(s any) (string, error) {View on GitHub ↗ (pinned to 8a468df065)
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.
Example fix
{{/* before */}}
{{ $u := urls.Parse $page }}
{{/* after */}}
{{ $u := urls.Parse $page.Permalink }} Defensive patterns
Strategy: try-catch
Validate before calling
{{ $u := trim $input " " }}
{{ if $u }}{{ $parsed := urls.Parse $u }}{{ end }} Try / catch
{{ with try (urls.Parse $raw) }}
{{ with .Err }}{{ warnf "unparsable URL %q: %s" $raw . }}{{ else }}{{ $u := .Value }}{{ end }}
{{ end }} Prevention
- 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
When it happens
Trigger: `{{ 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.
Common situations: 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`.
Related errors
- arguments to symdiff must be slices or arrays
- can't iterate over %T
- text must be a string
- first argument must be a map
- type %T not supported
AI-assisted analysis of gohugoio/hugo@8a468df065 (2026-07-31).
Data as JSON: /data/errors/8fc52a32a94a89d7.json.
Report an issue: GitHub ↗.