{"id":"6f88795ff34cb21d","repo":"gorilla/mux","slug":"no-matching-route-was-found","errorCode":null,"errorMessage":"no matching route was found","messagePattern":"no matching route was found","errorType":"error_code","errorClass":"ErrNotFound","httpStatus":null,"severity":"error","filePath":"mux.go","lineNumber":22,"sourceCode":"\npackage mux\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"fmt\"\n\t\"net/http\"\n\t\"net/url\"\n\t\"path\"\n\t\"regexp\"\n)\n\nvar (\n\t// ErrMethodMismatch is returned when the method in the request does not match\n\t// the method defined against the route.\n\tErrMethodMismatch = errors.New(\"method is not allowed\")\n\t// ErrNotFound is returned when no route match is found.\n\tErrNotFound = errors.New(\"no matching route was found\")\n\t// RegexpCompileFunc aliases regexp.Compile and enables overriding it.\n\t// Do not run this function from `init()` in importable packages.\n\t// Changing this value is not safe for concurrent use.\n\tRegexpCompileFunc = regexp.Compile\n\t// ErrMetadataKeyNotFound is returned when the specified metadata key is not present in the map\n\tErrMetadataKeyNotFound = errors.New(\"key not found in metadata\")\n)\n\n// NewRouter returns a new router instance.\nfunc NewRouter() *Router {\n\treturn &Router{namedRoutes: make(map[string]*Route)}\n}\n\n// Router registers routes to be matched and dispatches a handler.\n//\n// It implements the http.Handler interface, so it can be registered to serve\n// requests:\n//","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/gorilla/mux/blob/db9d1d0073d27a0a2d9a8c1bc52aa0af4374d265/mux.go#L4-L40","documentation":"Sentinel error mux.ErrNotFound (mux.go:22). Assigned to RouteMatch.MatchErr when no registered route matches the request. Also used internally to short-circuit subrouter matching when a query matcher fails on a matching path (route.go:68-73). With no NotFoundHandler set, Router.ServeHTTP falls back to http.NotFoundHandler (mux.go:224-226).","triggerScenarios":"A request whose path/host/scheme/headers/queries match none of the registered routes; or a path matches but a .Queries(...) matcher does not, which sets ErrNotFound and breaks out of the matcher loop (route.go:68-73).","commonSituations":"Client URL typo; trailing-slash mismatch (StrictSlash off, client hits /users/ but route is /users); route registration skipped during refactor; reverse proxy stripping a path prefix; env-specific routes missing in staging.","solutions":["Set router.NotFoundHandler to render the app's 404 JSON/page for clear diagnostics.","Verify the request path against registered templates; mind trailing slash (consider router.StrictSlash(true)).","If behind a proxy, confirm the path prefix is not being rewritten or stripped.","Dump all routes via router.Walk and compare with the failing URL."],"exampleFix":"// before\nr.HandleFunc(\"/users/{id}\", h)\n// GET /user/42 (typo) -> 404\n\n// after\nr.HandleFunc(\"/users/{id}\", h)\nr.StrictSlash(true)\nr.NotFoundHandler = http.HandlerFunc(notFoundJSON)","handlingStrategy":"fallback","validationCode":"// Pre-flight: does any route plausibly accept this path?\nok := false\n_ = r.Walk(func(rt *mux.Route, _ *mux.Router, _ []*mux.Route) error {\n    if tpl, err := rt.GetPathTemplate(); err == nil && matchesTemplate(tpl, req.URL.Path) {\n        ok = true\n    }\n    return nil\n})","typeGuard":"func isNotFound(m mux.RouteMatch) bool { return errors.Is(m.MatchErr, mux.ErrNotFound) }","tryCatchPattern":"var match mux.RouteMatch\nif !r.Match(req, &match) {\n    if errors.Is(match.MatchErr, mux.ErrNotFound) {\n        // render 404\n    }\n}","preventionTips":["Always set a NotFoundHandler.","Use StrictSlash consistently across the app.","Dump routes via Walk in tests to catch missing registrations."],"tags":["http","routing","not-found","runtime","sentinel"],"analyzedSha":"db9d1d0073d27a0a2d9a8c1bc52aa0af4374d265","analyzedAt":"2026-08-04T21:35:47.097Z","schemaVersion":2}