Skip to content

fix: propagate root RouteNotFound handler to route groups#3037

Open
mayank-dev-15 wants to merge 1 commit into
labstack:v4from
mayank-dev-15:fix-routenotfound-fallback
Open

fix: propagate root RouteNotFound handler to route groups#3037
mayank-dev-15 wants to merge 1 commit into
labstack:v4from
mayank-dev-15:fix-routenotfound-fallback

Conversation

@mayank-dev-15

Copy link
Copy Markdown

Summary

When a group has middleware, Group.Use() registers catch-all routes ("" and "/*") to guarantee middleware execution. Previously these routes always used the hardcoded NotFoundHandler, ignoring any custom RouteNotFound handler set on the root Echo instance.

Problem

e := echo.New()
e.RouteNotFound("/*", customHandler) // user's custom 404

v0 := e.Group("/v0", someMiddleware)
v0.POST("/resource", handler)

// Request to /v0/nonexistent uses NotFoundHandler instead of customHandler

Fix

Store the user's custom RouteNotFound handler on the Echo struct and use it when registering group catch-all routes, falling back to NotFoundHandler if none was set.

Changes

  • echo.go: Added notFoundHandler field to Echo struct; set it in RouteNotFound()
  • group.go: Use() now uses g.echo.notFoundHandler instead of hardcoded NotFoundHandler
  • group_test.go: Updated test expectations to verify root handler propagation

Fixes #2485

When a group has middleware, Group.Use() registers catch-all routes to guarantee middleware execution. Previously these routes always used the hardcoded NotFoundHandler, ignoring any custom RouteNotFound handler set on the root Echo instance. Store the user custom RouteNotFound handler on the Echo struct and use it when registering group catch-all routes, falling back to NotFoundHandler if none was set. Fixes labstack#2485
@aldas

aldas commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

you can already do this by

	custom404Handler := func(c echo.Context) error {
		return c.String(http.StatusNotFound, "Custom 404 message")
	}
	echo.NotFoundHandler = custom404Handler

	e.RouteNotFound("/*", custom404Handler) // user's custom 404

and groups will use that same customHandler as it echo.NotFoundHandler

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants