mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-26 00:00:23 +02:00
Docs improvements (#37)
This commit is contained in:
@@ -19,6 +19,7 @@ use http::StatusCode;
|
||||
use std::{
|
||||
borrow::Cow,
|
||||
collections::HashMap,
|
||||
convert::Infallible,
|
||||
net::SocketAddr,
|
||||
sync::{Arc, RwLock},
|
||||
time::Duration,
|
||||
@@ -152,20 +153,20 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
fn handle_error(error: BoxError) -> impl IntoResponse {
|
||||
fn handle_error(error: BoxError) -> Result<impl IntoResponse, Infallible> {
|
||||
if error.is::<tower::timeout::error::Elapsed>() {
|
||||
return (StatusCode::REQUEST_TIMEOUT, Cow::from("request timed out"));
|
||||
return Ok((StatusCode::REQUEST_TIMEOUT, Cow::from("request timed out")));
|
||||
}
|
||||
|
||||
if error.is::<tower::load_shed::error::Overloaded>() {
|
||||
return (
|
||||
return Ok((
|
||||
StatusCode::SERVICE_UNAVAILABLE,
|
||||
Cow::from("service is overloaded, try again later"),
|
||||
);
|
||||
));
|
||||
}
|
||||
|
||||
(
|
||||
Ok((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
Cow::from(format!("Unhandled internal error: {}", error)),
|
||||
)
|
||||
))
|
||||
}
|
||||
|
||||
@@ -10,10 +10,10 @@ async fn main() {
|
||||
let app = axum::routing::nest(
|
||||
"/static",
|
||||
axum::service::get(ServeDir::new(".").handle_error(|error: std::io::Error| {
|
||||
(
|
||||
Ok::<_, std::convert::Infallible>((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled interal error: {}", error),
|
||||
)
|
||||
))
|
||||
})),
|
||||
)
|
||||
.layer(TraceLayer::new_for_http());
|
||||
|
||||
@@ -33,10 +33,10 @@ async fn main() {
|
||||
ServeDir::new("examples/websocket")
|
||||
.append_index_html_on_directories(true)
|
||||
.handle_error(|error: std::io::Error| {
|
||||
(
|
||||
Ok::<_, std::convert::Infallible>((
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled interal error: {}", error),
|
||||
)
|
||||
))
|
||||
}),
|
||||
),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user