mirror of
https://github.com/tokio-rs/axum.git
synced 2026-09-02 00:00:22 +02:00
Move axum-handle-error-extract into axum (#534)
* Move `axum-handle-error-extract` into axum With 0.4 underway we can now nuke `axum-handle-error-extract` and move its code directly into axum. So this replaces the old `HandleErrorLayer` with one that supports async functions and extractors. * changelog * fix CI
This commit is contained in:
@@ -124,7 +124,7 @@ fn admin_routes() -> Router {
|
||||
.layer(RequireAuthorizationLayer::bearer("secret-token"))
|
||||
}
|
||||
|
||||
fn handle_error(error: BoxError) -> impl IntoResponse {
|
||||
async fn handle_error(error: BoxError) -> impl IntoResponse {
|
||||
if error.is::<tower::timeout::error::Elapsed>() {
|
||||
return (StatusCode::REQUEST_TIMEOUT, Cow::from("request timed out"));
|
||||
}
|
||||
|
||||
@@ -29,7 +29,7 @@ async fn main() {
|
||||
.route("/", post(|| async move { "Hello from `POST /`" }))
|
||||
.layer(
|
||||
ServiceBuilder::new()
|
||||
.layer(HandleErrorLayer::new(|error| {
|
||||
.layer(HandleErrorLayer::new(|error| async move {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled internal error: {}", error),
|
||||
|
||||
@@ -26,7 +26,7 @@ async fn main() {
|
||||
|
||||
let static_files_service =
|
||||
get_service(ServeDir::new("examples/sse/assets").append_index_html_on_directories(true))
|
||||
.handle_error(|error: std::io::Error| {
|
||||
.handle_error(|error: std::io::Error| async move {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled internal error: {}", error),
|
||||
|
||||
@@ -22,7 +22,7 @@ async fn main() {
|
||||
let app = Router::new()
|
||||
.nest(
|
||||
"/static",
|
||||
get_service(ServeDir::new(".")).handle_error(|error: std::io::Error| {
|
||||
get_service(ServeDir::new(".")).handle_error(|error: std::io::Error| async move {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled internal error: {}", error),
|
||||
|
||||
@@ -49,7 +49,7 @@ async fn main() {
|
||||
// Add middleware to all routes
|
||||
.layer(
|
||||
ServiceBuilder::new()
|
||||
.layer(HandleErrorLayer::new(|error: BoxError| {
|
||||
.layer(HandleErrorLayer::new(|error: BoxError| async move {
|
||||
if error.is::<tower::timeout::error::Elapsed>() {
|
||||
Ok(StatusCode::REQUEST_TIMEOUT)
|
||||
} else {
|
||||
|
||||
@@ -36,7 +36,7 @@ async fn main() {
|
||||
get_service(
|
||||
ServeDir::new("examples/websockets/assets").append_index_html_on_directories(true),
|
||||
)
|
||||
.handle_error(|error: std::io::Error| {
|
||||
.handle_error(|error: std::io::Error| async move {
|
||||
(
|
||||
StatusCode::INTERNAL_SERVER_ERROR,
|
||||
format!("Unhandled internal error: {}", error),
|
||||
|
||||
Reference in New Issue
Block a user