Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ type Echo struct {
// If you are enabling this option, make sure you understand the security implications.
// See: https://github.com/labstack/echo/security/advisories/GHSA-vfp3-v2gw-7wfq
EnablePathUnescapingStaticFiles bool

// notFoundHandler is the user-provided RouteNotFound handler set on the root Echo instance.
// Groups propagate this handler so that catch-all 404 routes use the user's custom handler
// instead of the default NotFoundHandler.
notFoundHandler HandlerFunc
}

// Route contains a handler and information for matching against requests.
Expand Down Expand Up @@ -554,6 +559,7 @@ func (e *Echo) TRACE(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
//
// Example: `e.RouteNotFound("/*", func(c echo.Context) error { return c.NoContent(http.StatusNotFound) })`
func (e *Echo) RouteNotFound(path string, h HandlerFunc, m ...MiddlewareFunc) *Route {
e.notFoundHandler = h
return e.Add(RouteNotFound, path, h, m...)
}

Expand Down
9 changes: 7 additions & 2 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ func (g *Group) Use(middleware ...MiddlewareFunc) {
// 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)
// Use the root Echo's RouteNotFound handler if one was set, otherwise fall back to the default NotFoundHandler.
handler := NotFoundHandler
if g.echo.notFoundHandler != nil {
handler = g.echo.notFoundHandler
}
g.RouteNotFound("", handler)
g.RouteNotFound("/*", handler)
}

// CONNECT implements `Echo#CONNECT()` for sub-routes within the Group.
Expand Down
8 changes: 4 additions & 4 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,17 @@ func TestGroup_RouteNotFoundWithMiddleware(t *testing.T) {
expectCode: http.StatusNotFound,
},
{
name: "ok, default group 404 handler is called with middleware",
name: "ok, root 404 handler propagates to group with middleware",
givenCustom404: false,
whenURL: "/group/test3",
expectBody: "{\"message\":\"Not Found\"}\n",
expectBody: "GET /group/*",
expectCode: http.StatusNotFound,
},
{
name: "ok, (no slash) default group 404 handler is called with middleware",
name: "ok, (no slash) root 404 handler propagates to group with middleware",
givenCustom404: false,
whenURL: "/group",
expectBody: "{\"message\":\"Not Found\"}\n",
expectBody: "GET /group",
expectCode: http.StatusNotFound,
},
}
Expand Down