-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix: RouteNotFound handler does not falls back to root one #2859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
majiayu000
wants to merge
2
commits into
labstack:master
from
majiayu000:fix-2485-routenotfound-handler-does-not-0101-1658
Open
fix: RouteNotFound handler does not falls back to root one #2859
majiayu000
wants to merge
2
commits into
labstack:master
from
majiayu000:fix-2485-routenotfound-handler-does-not-0101-1658
+3
−0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
...iddleware When a group has middleware, the RouteNotFound handler registered at the root level is now properly used as a fallback. Previously, groups with middleware would always use the default NotFoundHandler instead of falling back to the root's custom RouteNotFound handler. This fix: - Adds a routeNotFoundHandler field to Echo to store the root handler - Modifies group.Use() to use a fallback handler that checks for the root's RouteNotFound handler at request time - Updates tests to reflect the new expected behavior Fixes labstack#2485 Signed-off-by: majiayu000 <1835304752@qq.com>
Contributor
Hi, happy new year!
This is interesting idea but I think you can achieve same by replacing default NotFoundHandler (assuming you do not deal with multiple echo instances which routes/groups are added in mixed order)
echo.NotFoundHandler = func(c echo.Context) error { return echo.NewHTTPError(http.StatusNotFound, "custom route not found") } e.RouteNotFound("*", echo.NotFoundHandler) g := e.Group("/group") // <--- will use replaced echo.NotFoundHandler
the root problem/cause here is that we have these 404 handlers added.
Lines 27 to 33 in d0f9d1e
// group level middlewares are different from Echo `Pre` and `Use` middlewares (those are global). Group level middlewares
// are only executed if they are added to the Router with route.
// So we register catch all route (404 is a safe way to emulate route match) for this group and now during routing the
// Router would find route to match our request path and therefore guarantee the middleware(s) will get executed.
g.RouteNotFound("", NotFoundHandler)
g.RouteNotFound("/*", NotFoundHandler)
}
Which is something we can not change but if we are looking for workaround I think replacing the 404handler is probably the easiest way.
Contributor
aldas
commented
Jan 1, 2026
also these group level 404 routes can be replaced
my404 := func(c echo.Context) error { return echo.NewHTTPError(http.StatusNotFound, "custom route not found") } g := e.Group("/group") g.RouteNotFound("", my404) g.RouteNotFound("/*", my404) echo.NotFoundHandler = my404 e.RouteNotFound("*", my404)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2485
Changes