{"id":"52565a3794e3beea","repo":"gorilla/mux","slug":"mux-route-doesn-t-have-a-host","errorCode":null,"errorMessage":"mux: route doesn't have a host","messagePattern":"mux: route doesn't have a host","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"route.go","lineNumber":653,"sourceCode":"\t\tqueries = append(queries, query)\n\t}\n\treturn &url.URL{\n\t\tScheme:   scheme,\n\t\tHost:     host,\n\t\tPath:     path,\n\t\tRawQuery: strings.Join(queries, \"&\"),\n\t}, nil\n}\n\n// URLHost builds the host part of the URL for a route. See Route.URL().\n//\n// The route must have a host defined.\nfunc (r *Route) URLHost(pairs ...string) (*url.URL, error) {\n\tif r.err != nil {\n\t\treturn nil, r.err\n\t}\n\tif r.regexp.host == nil {\n\t\treturn nil, errors.New(\"mux: route doesn't have a host\")\n\t}\n\tvalues, err := r.prepareVars(pairs...)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\thost, err := r.regexp.host.url(values)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tu := &url.URL{\n\t\tScheme: \"http\",\n\t\tHost:   host,\n\t}\n\tif r.buildScheme != \"\" {\n\t\tu.Scheme = r.buildScheme\n\t}\n\treturn u, nil\n}","sourceCodeStart":635,"sourceCodeEnd":671,"githubUrl":"https://github.com/gorilla/mux/blob/db9d1d0073d27a0a2d9a8c1bc52aa0af4374d265/route.go#L635-L671","documentation":"Returned by Route.URLHost (route.go:648-653) when building a URL but the route has no host matcher (r.regexp.host == nil). URLHost constructs only the host (and scheme), so a host matcher is mandatory. Route.URL() works without one and just returns the path.","triggerScenarios":"r.NewRoute().Path(\"/x\").Name(\"x\"); then r.Get(\"x\").URLHost(...) is called but Host() was never invoked on the route.","commonSituations":"Calling URLHost on a path-only route during link generation; refactor that removed a Host matcher but left URLHost callers; subrouter routes that inherit path but not host.","solutions":["Use Route.URL() if you only need the path (it tolerates a missing host).","Add .Host(...) to the route definition before calling URLHost.","Probe with GetHostTemplate() first; if it errors, fall back to URL()."],"exampleFix":"// before\nu, err := r.Get(\"x\").URLHost() // route has no Host\n\n// after\nu, err := r.Get(\"x\").URL() // returns a path-only url","handlingStrategy":"validation","validationCode":"if _, err := rt.GetHostTemplate(); err != nil {\n    return rt.URL(pairs...) // path-only fallback\n}","typeGuard":"func hasHost(rt *mux.Route) bool { _, err := rt.GetHostTemplate(); return err == nil }","tryCatchPattern":"u, err := rt.URLHost(pairs...)\nif err != nil {\n    if strings.Contains(err.Error(), \"host\") {\n        u, err = rt.URL(pairs...)\n    }\n}","preventionTips":["Prefer URL() unless you specifically need the host.","Wrap URLHost callers with a hasHost probe.","Document host-bearing routes."],"tags":["routing","url-building","config"],"analyzedSha":"db9d1d0073d27a0a2d9a8c1bc52aa0af4374d265","analyzedAt":"2026-08-04T21:35:47.097Z","schemaVersion":2}