Move methods from ServiceExt to RoutingDsl (#160)

Previously, on `main`, this wouldn't compile:

```rust
let app = route("/", get(handler))
    .layer(
        ServiceBuilder::new()
            .timeout(Duration::from_secs(10))
            .into_inner(),
    )
    .handle_error(...)
    .route(...); // <-- doesn't work
```

That is because `handle_error` would be
`axum::service::ServiceExt::handle_error` which returns `HandleError<_,
_, _, HandleErrorFromService>` which does _not_ implement `RoutingDsl`.
So you couldn't call `route`. This was caused by
https://github.com/tokio-rs/axum/pull/120.

Basically `handle_error` when called on a `RoutingDsl`, the resulting
service should also implement `RoutingDsl`, but if called on another
random service it should _not_ implement `RoutingDsl`.

I don't think thats possible by having `handle_error` on `ServiceExt`
which is implemented for any service, since all axum routers are also
services by design.

This resolves the issue by removing `ServiceExt` and moving its methods
to `RoutingDsl`. Then we have more tight control over what has a
`handle_error` method.

`service::OnMethod` now also has a `handle_error` so you can still
handle errors from random services, by doing
`service::any(svc).handle_error(...)`.
This commit is contained in:
David Pedersen
2021-08-08 14:30:51 +02:00
committed by GitHub
parent 9b3f3c9bdf
commit 8013165908
12 changed files with 346 additions and 297 deletions
-1
View File
@@ -12,7 +12,6 @@ use axum::{
prelude::*,
response::IntoResponse,
routing::BoxRoute,
service::ServiceExt,
};
use bytes::Bytes;
use http::StatusCode;
+13 -16
View File
@@ -4,7 +4,7 @@
//! cargo run --example sse --features=headers
//! ```
use axum::{extract::TypedHeader, prelude::*, routing::nest, service::ServiceExt, sse::Event};
use axum::{extract::TypedHeader, prelude::*, routing::nest, sse::Event};
use futures::stream::{self, Stream};
use http::StatusCode;
use std::{convert::Infallible, net::SocketAddr, time::Duration};
@@ -19,22 +19,19 @@ async fn main() {
}
tracing_subscriber::fmt::init();
let static_files_service =
axum::service::get(ServeDir::new("examples/sse").append_index_html_on_directories(true))
.handle_error(|error: std::io::Error| {
Ok::<_, std::convert::Infallible>((
StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
))
});
// build our application with a route
let app = nest(
"/",
axum::service::get(
ServeDir::new("examples/sse")
.append_index_html_on_directories(true)
.handle_error(|error: std::io::Error| {
Ok::<_, std::convert::Infallible>((
StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
))
}),
),
)
.route("/sse", axum::sse::sse(make_stream))
.layer(TraceLayer::new_for_http());
let app = nest("/", static_files_service)
.route("/sse", axum::sse::sse(make_stream))
.layer(TraceLayer::new_for_http());
// run it
let addr = SocketAddr::from(([127, 0, 0, 1], 3000));
+3 -3
View File
@@ -4,7 +4,7 @@
//! cargo run --example static_file_server
//! ```
use axum::{prelude::*, routing::nest, service::ServiceExt};
use axum::{prelude::*, routing::nest};
use http::StatusCode;
use std::net::SocketAddr;
use tower_http::{services::ServeDir, trace::TraceLayer};
@@ -19,12 +19,12 @@ async fn main() {
let app = nest(
"/static",
axum::service::get(ServeDir::new(".").handle_error(|error: std::io::Error| {
axum::service::get(ServeDir::new(".")).handle_error(|error: std::io::Error| {
Ok::<_, std::convert::Infallible>((
StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
))
})),
}),
)
.layer(TraceLayer::new_for_http());
-1
View File
@@ -17,7 +17,6 @@ use axum::{
extract::{Extension, Json, Path, Query},
prelude::*,
response::IntoResponse,
service::ServiceExt,
};
use http::StatusCode;
use serde::{Deserialize, Serialize};
+8 -10
View File
@@ -14,7 +14,6 @@ use axum::{
prelude::*,
response::IntoResponse,
routing::nest,
service::ServiceExt,
};
use http::StatusCode;
use std::net::SocketAddr;
@@ -35,15 +34,14 @@ async fn main() {
let app = nest(
"/",
axum::service::get(
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 internal error: {}", error),
))
}),
),
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 internal error: {}", error),
))
}),
)
// routes are matched from bottom to top, so we have to put `nest` at the
// top since it matches all routes