{"id":"73bf0167482c557c","repo":"gorilla/mux","slug":"mux-route-already-has-name-q-can-t-set-q","errorCode":null,"errorMessage":"mux: route already has name %q, can't set %q","messagePattern":"mux: route already has name %q, can't set %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"route.go","lineNumber":214,"sourceCode":"func (r *Route) GetHandlerWithMiddlewares() http.Handler {\n\thandler := r.handler\n\n\tif handler != nil && len(r.middlewares) > 0 {\n\t\tfor i := len(r.middlewares) - 1; i >= 0; i-- {\n\t\t\thandler = r.middlewares[i].Middleware(handler)\n\t\t}\n\t}\n\n\treturn handler\n}\n\n// Name -----------------------------------------------------------------------\n\n// Name sets the name for the route, used to build URLs.\n// It is an error to call Name more than once on a route.\nfunc (r *Route) Name(name string) *Route {\n\tif r.name != \"\" {\n\t\tr.err = fmt.Errorf(\"mux: route already has name %q, can't set %q\",\n\t\t\tr.name, name)\n\t}\n\tif r.err == nil {\n\t\tr.name = name\n\t\tr.namedRoutes[name] = r\n\t}\n\treturn r\n}\n\n// GetName returns the name for the route, if any.\nfunc (r *Route) GetName() string {\n\treturn r.name\n}\n\n// ----------------------------------------------------------------------------\n// Matchers\n// ----------------------------------------------------------------------------\n","sourceCodeStart":196,"sourceCodeEnd":232,"githubUrl":"https://github.com/gorilla/mux/blob/db9d1d0073d27a0a2d9a8c1bc52aa0af4374d265/route.go#L196-L232","documentation":"Set on r.err by Route.Name (route.go:212-216) when Name is called a second time on the same route. A route carries exactly one name (for URL building via Router.Get). Once r.err is set, later builder methods are no-ops and the route neither matches nor builds.","triggerScenarios":"r.HandleFunc(\"/x\", h).Name(\"a\").Name(\"b\"), or chaining through a helper that calls Name and then the caller also calling Name on the returned route.","commonSituations":"A route-builder helper that names routes colliding with an outer .Name(); copy-paste leaving a stale Name; refactor moving Name into a wrapper while leaving the old call.","solutions":["Call Name exactly once per route.","If a helper already names the route, do not name it again at the call site.","Check route.GetError() after construction."],"exampleFix":"// before\nr.HandleFunc(\"/x\", h).Name(\"x\").Name(\"y\")\n// -> mux: route already has name \"x\", can't set \"y\"\n\n// after\nr.HandleFunc(\"/x\", h).Name(\"x\")","handlingStrategy":"validation","validationCode":"func setNameOnce(r *mux.Route, name string) error {\n    if r.GetName() != \"\" { return errors.New(\"route already named\") }\n    r.Name(name)\n    return nil\n}","typeGuard":"func isUnnamed(r *mux.Route) bool { return r != nil && r.GetName() == \"\" }","tryCatchPattern":"rt := r.HandleFunc(\"/x\", h).Name(\"x\")\nif err := rt.GetError(); err != nil {\n    log.Fatal(err)\n}","preventionTips":["Centralize naming in one builder helper.","Never chain two Name calls.","Test that Router.Get(name) returns the route."],"tags":["routing","config","naming","build-time"],"analyzedSha":"db9d1d0073d27a0a2d9a8c1bc52aa0af4374d265","analyzedAt":"2026-08-04T21:35:47.097Z","schemaVersion":2}