fix(router): keep group auto-404 in sync with root RouteNotFound#3052
Open
Solaris-star wants to merge 1 commit into
Open
fix(router): keep group auto-404 in sync with root RouteNotFound#3052Solaris-star wants to merge 1 commit into
Solaris-star wants to merge 1 commit into
Conversation
Group.Use auto-registers nil-handler RouteNotFound routes so group
middleware still runs on 404s. Those handlers were filled from
r.notFoundHandler at registration time, which stayed the default even
after e.RouteNotFound("/*", h). Groups with middleware therefore used
the stock 404 JSON body instead of the root catch-all (labstack#2485).
When a RouteNotFound is added for /* or "", update r.notFoundHandler so
later group auto-404 routes inherit the same handler.
Fixes labstack#2485
aldas
reviewed
Jul 22, 2026
aldas
left a comment
Contributor
There was a problem hiding this comment.
I do not think adding a route should have side-effects like that to Router.
you can achieve same (global 404 handler) when you create Router with your own NotFoundHandler
so this
e := echo.New()
e.RouteNotFound("/*", func(c *echo.Context) error {
return c.NoContent(http.StatusNotFound)
})should be
e := echo.NewWithConfig(echo.Config{
Router: echo.NewRouter(echo.RouterConfig{
NotFoundHandler: func(c *echo.Context) error {
return c.NoContent(http.StatusNotFound)
},
}),
})
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
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.
Summary
When a group has middleware, Echo auto-registers
RouteNotFoundroutes with a nil handler so the group middleware still runs on 404s. Those handlers are filled fromDefaultRouter.notFoundHandler.e.RouteNotFound("/*", h)only installedhon the root tree node. It did not updater.notFoundHandler, so later group auto-404 routes kept the default JSON{"message":"Not Found"}instead of the root catch-all.Fix
On
DefaultRouter.Add, if method isRouteNotFoundand path is/*or"", also setr.notFoundHandler = route.Handler.Test plan
TestGroup_RouteNotFoundFallsBackToRoot(issue repro:/foo,/v0/foo,/v1/foo)TestGroup_RouteNotFound*/TestGroup_UseMultipleTimesgo test .Fixes #2485