Skip to content

Implicit 404 handlers cause panic if Use is called more than once #3047

Description

@aouji

Hi team. #2996 added 404 handlers to groups as long as they have any other middlewares. This currently means any of the following scenarios would try to add duplicate route handlers and panic:

// 1
apiGroup := e.Group("/api")
apiGroup.Use(Middleware1)
apiGroup.Use(Middleware2) // panic: echo_route_not_found /api: adding duplicate route (same method+path) is not allowed

// 2
apiGroup := e.Group("/api", Middlware1) // Internally `Use` is called since there is a middleware
apiGroup.Use(Middleware2) // panic: echo_route_not_found /api: adding duplicate route (same method+path) is not allowed

// 3 This is my real world scenario
apiGroup := e.Group("/api", Middlware1) // Internally `Use` is called since there is a middleware
resource1Group := apiGroup.Group("/resource") // Internally `Use` is called since there is a middleware inherited from parent group
resource1Group.Use(Middleware2) // panic: echo_route_not_found /api/resource: adding duplicate route (same method+path) is not allowed

Workaround is to avoid using Use if there are already any middlewares attached to a group or its parent, or to turn off the behvaior by setting NoGroupAutoRegister404Routes to true.

Edit: I just noticed that AllowOverwritingRoute is true by default. I would still argue it is buggy to have code panic if it is not set to true and user is not explicitly adding 404 handlers...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions