{"id":"aa7e03b288c6adeb","repo":"gorilla/mux","slug":"mux-route-doesn-t-have-a-path","errorCode":null,"errorMessage":"mux: route doesn't have a path","messagePattern":"mux: route doesn't have a path","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"route.go","lineNumber":681,"sourceCode":"\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}\n\n// URLPath builds the path part of the URL for a route. See Route.URL().\n//\n// The route must have a path defined.\nfunc (r *Route) URLPath(pairs ...string) (*url.URL, error) {\n\tif r.err != nil {\n\t\treturn nil, r.err\n\t}\n\tif r.regexp.path == nil {\n\t\treturn nil, errors.New(\"mux: route doesn't have a path\")\n\t}\n\tvalues, err := r.prepareVars(pairs...)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tpath, err := r.regexp.path.url(values)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn &url.URL{\n\t\tPath: path,\n\t}, nil\n}\n\n// GetPathTemplate returns the template used to build the\n// route match.\n// This is useful for building simple REST API documentation and for instrumentation\n// against third-party services.","sourceCodeStart":663,"sourceCodeEnd":699,"githubUrl":"https://github.com/gorilla/mux/blob/db9d1d0073d27a0a2d9a8c1bc52aa0af4374d265/route.go#L663-L699","documentation":"Returned by Route.URLPath (route.go:676-681) when building a URL but the route has no path matcher (r.regexp.path == nil). URLPath constructs only the path, so a path matcher is mandatory. Route.URL() without a path returns a URL with an empty Path.","triggerScenarios":"r.NewRoute().Host(\"{s}.com\").Name(\"x\"); then r.Get(\"x\").URLPath(...) is called but Path() was never invoked on the route.","commonSituations":"Host-only routes used for URL building; refactor that removed a path; BuildOnly routes used for host links.","solutions":["Add .Path(...) to the route if you want to build paths.","Use Route.URL() if you need host-only output.","Probe with GetPathTemplate() first."],"exampleFix":"// before\nu, err := r.Get(\"x\").URLPath() // route has no Path\n\n// after\nu, err := r.Get(\"x\").URL()","handlingStrategy":"validation","validationCode":"if _, err := rt.GetPathTemplate(); err != nil {\n    return rt.URL(pairs...)\n}","typeGuard":"func hasPath(rt *mux.Route) bool { _, err := rt.GetPathTemplate(); return err == nil }","tryCatchPattern":"u, err := rt.URLPath(pairs...)\nif err != nil { u, err = rt.URL(pairs...) }","preventionTips":["Prefer URL() unless path-only is required.","Guard URLPath with GetPathTemplate.","Keep host-only routes clearly labeled."],"tags":["routing","url-building","config"],"analyzedSha":"db9d1d0073d27a0a2d9a8c1bc52aa0af4374d265","analyzedAt":"2026-08-04T21:35:47.097Z","schemaVersion":2}