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
+2 -2
View File
@@ -366,9 +366,9 @@ impl<S, T> Layered<S, T> {
/// This is used to convert errors to responses rather than simply
/// terminating the connection.
///
/// It works similarly to [`routing::Layered::handle_error`]. See that for more details.
/// It works similarly to [`routing::RoutingDsl::handle_error`]. See that for more details.
///
/// [`routing::Layered::handle_error`]: crate::routing::Layered::handle_error
/// [`routing::RoutingDsl::handle_error`]: crate::routing::RoutingDsl::handle_error
pub fn handle_error<F, ReqBody, ResBody, Res, E>(
self,
f: F,