mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-27 00:00:24 +02:00
Revamp error handling model (#402)
* Revamp error handling model * changelog improvements and typo fixes * Fix a few more Infallible bounds * minor docs fixes
This commit is contained in:
@@ -6,12 +6,13 @@
|
||||
|
||||
use axum::{
|
||||
body::{Body, BoxBody, Bytes},
|
||||
error_handling::HandleErrorLayer,
|
||||
handler::post,
|
||||
http::{Request, Response},
|
||||
http::{Request, Response, StatusCode},
|
||||
Router,
|
||||
};
|
||||
use std::net::SocketAddr;
|
||||
use tower::{filter::AsyncFilterLayer, util::AndThenLayer, BoxError};
|
||||
use tower::{filter::AsyncFilterLayer, util::AndThenLayer, BoxError, ServiceBuilder};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
@@ -26,8 +27,17 @@ async fn main() {
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", post(|| async move { "Hello from `POST /`" }))
|
||||
.layer(AsyncFilterLayer::new(map_request))
|
||||
.layer(AndThenLayer::new(map_response));
|
||||
.layer(
|
||||
ServiceBuilder::new()
|
||||
.layer(HandleErrorLayer::new(|error| {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled internal error: {}", error),
|
||||
)
|
||||
}))
|
||||
.layer(AndThenLayer::new(map_response))
|
||||
.layer(AsyncFilterLayer::new(map_request)),
|
||||
);
|
||||
|
||||
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
|
||||
tracing::debug!("listening on {}", addr);
|
||||
|
||||
Reference in New Issue
Block a user