How to catch "Invalid URL param." error? #333
Answered
by
davidpdrsn
shengsheng
asked this question in
Q&A
-
use axum::{Router, extract::Path, handler::get, response::Html};
use std::net::SocketAddr;
#[tokio::main]
async fn main() {
// build our application with a route
let app = Router::new().route("/:type/:id", get(handler));
// run it
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
println!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn handler(Path((ty, id)): Path<(String, u8)>) -> Html<String> {
let content = format!("<p>{}</p><p>{}</p>", ty, id);
Html(content)
}
How to catch the "Invalid URL param...." error? I want print a custom response to user. |
Beta Was this translation helpful? Give feedback.
Answered by
davidpdrsn
Sep 17, 2021
Replies: 1 comment 2 replies
-
Use You can find an example here https://docs.rs/axum/0.2.4/axum/extract/index.html#optional-extractors. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
davidpdrsn
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use
Result<Path<...>, PathParamsRejection>
as the extractor which allows you to handle errors.You can find an example here https://docs.rs/axum/0.2.4/axum/extract/index.html#optional-extractors.