Proposal Details
Go 1.22 introduced an enhanced HTTP routing. The current implementation utilizes unexported handlers for 404 and 405 responses. However, if there is a need for a custom 404 response (e.g., in a REST API with JSON responses), it is no longer possible to use a 'catch-all' pattern /, as it prevents the ability to return a 405 response. This issue is elaborated further in this discussion.
To address this challenge, one can define a custom http.ResponseWriter to intercept responses and their status codes and handle them appropriately. Nonetheless, this approach precludes the ability to return a custom 404 response based on whether the http.ServeMux couldn't locate the appropriate route or if the user's handler returned a response with a 404 status code. An example scenario is when responding to GET user/{id} for a non-existent user in the system.
Given these challenges, I believe it would be valuable to register custom 404 and 405 handlers.
Proposal Details
Go 1.22 introduced an enhanced HTTP routing. The current implementation utilizes unexported handlers for 404 and 405 responses. However, if there is a need for a custom 404 response (e.g., in a REST API with JSON responses), it is no longer possible to use a 'catch-all' pattern
/, as it prevents the ability to return a 405 response. This issue is elaborated further in this discussion.To address this challenge, one can define a custom
http.ResponseWriterto intercept responses and their status codes and handle them appropriately. Nonetheless, this approach precludes the ability to return a custom 404 response based on whether thehttp.ServeMuxcouldn't locate the appropriate route or if the user's handler returned a response with a 404 status code. An example scenario is when responding toGET user/{id}for a non-existent user in the system.Given these challenges, I believe it would be valuable to register custom 404 and 405 handlers.