From 2be79168d8aca78506f6252c6ee5f49fbeb19a1c Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Fri, 6 Aug 2021 01:15:23 +0200 Subject: [PATCH] Handle `METHOD_NOT_ALLOWED` in 404 example (#131) If a route existed but had no handler for the method, the code used in the 404 example wouldn't catch it. --- examples/404.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/404.rs b/examples/404.rs index 76c9ce6d..f837caa1 100644 --- a/examples/404.rs +++ b/examples/404.rs @@ -39,12 +39,14 @@ async fn handler() -> response::Html<&'static str> { } fn map_404(response: Response) -> Response { - if response.status() != StatusCode::NOT_FOUND { - return response; + if response.status() == StatusCode::NOT_FOUND + || response.status() == StatusCode::METHOD_NOT_ALLOWED + { + return Response::builder() + .status(StatusCode::NOT_FOUND) + .body(box_body(Body::from("nothing to see here"))) + .unwrap(); } - Response::builder() - .status(StatusCode::NOT_FOUND) - .body(box_body(Body::from("nothing to see here"))) - .unwrap() + response }