pavex/router/fallback.rs
1use pavex_macros::fallback;
2
3use crate::Response;
4use crate::http::header::ALLOW;
5
6use super::AllowedMethods;
7
8/// The default fallback handler for incoming requests that don't match
9/// any of the routes you registered.
10///
11/// It returns a `404 Not Found` response if the path doesn't match any of the
12/// registered route paths.
13/// It returns a `405 Method Not Allowed` response if the path matches a
14/// registered route path but the method doesn't match any of its associated
15/// handlers.
16///
17/// It also returns a `404 Not Found` response if the path allows all HTTP methods,
18/// including custom ones.
19#[fallback(pavex = crate)]
20pub async fn default_fallback(allowed_methods: &AllowedMethods) -> Response {
21 match allowed_methods.allow_header_value() {
22 Some(header_value) => Response::method_not_allowed().insert_header(ALLOW, header_value),
23 _ => Response::not_found(),
24 }
25}