From 46f74895cea5dca5631d7728d3497cc4314a4b70 Mon Sep 17 00:00:00 2001 From: David Pedersen Date: Sun, 6 Jun 2021 22:43:53 +0200 Subject: [PATCH] Remove duplicated `HandleError` --- src/lib.rs | 2 +- src/routing.rs | 66 ++------------------------------------------------ src/tests.rs | 2 +- 3 files changed, 4 insertions(+), 66 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 5d07b600..41d820f3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -571,7 +571,7 @@ rust_2018_idioms, future_incompatible, nonstandard_style, - // missing_docs, + // missing_docs )] #![deny(unreachable_pub, broken_intra_doc_links, private_in_public)] #![allow( diff --git a/src/routing.rs b/src/routing.rs index e2acf70f..4ca3c965 100644 --- a/src/routing.rs +++ b/src/routing.rs @@ -385,7 +385,7 @@ pub struct Layered(S); impl RoutingDsl for Layered {} impl Layered { - pub fn handle_error(self, f: F) -> HandleError + pub fn handle_error(self, f: F) -> crate::service::HandleError where S: Service, Response = Response> + Clone, F: FnOnce(S::Error) -> Res, @@ -393,7 +393,7 @@ impl Layered { B: http_body::Body + Send + Sync + 'static, B::Error: Into + Send + Sync + 'static, { - HandleError { inner: self.0, f } + crate::service::HandleError { inner: self.0, f } } } @@ -416,68 +416,6 @@ where } } -#[derive(Clone, Copy)] -pub struct HandleError { - inner: S, - f: F, -} - -impl RoutingDsl for HandleError {} - -impl Service> for HandleError -where - S: Service, Response = Response> + Clone, - F: FnOnce(S::Error) -> Res + Clone, - Res: IntoResponse, - B: http_body::Body + Send + Sync + 'static, - B::Error: Into + Send + Sync + 'static, -{ - type Response = Response; - type Error = Infallible; - type Future = HandleErrorFuture>, F>; - - fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll> { - Poll::Ready(Ok(())) - } - - fn call(&mut self, req: Request) -> Self::Future { - HandleErrorFuture { - inner: self.inner.clone().oneshot(req), - f: Some(self.f.clone()), - } - } -} - -#[pin_project] -pub struct HandleErrorFuture { - #[pin] - inner: Fut, - f: Option, -} - -impl Future for HandleErrorFuture -where - Fut: Future, E>>, - F: FnOnce(E) -> Res, - Res: IntoResponse, - B: http_body::Body + Send + Sync + 'static, - B::Error: Into + Send + Sync + 'static, -{ - type Output = Result, Infallible>; - - fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let this = self.project(); - match ready!(this.inner.poll(cx)) { - Ok(res) => Ok(res.map(BoxBody::new)).into(), - Err(err) => { - let f = this.f.take().unwrap(); - let res = f(err).into_response(); - Ok(res.map(BoxBody::new)).into() - } - } - } -} - // ===== nesting ===== pub fn nest(spec: &str, svc: S) -> Nested diff --git a/src/tests.rs b/src/tests.rs index 4c73756d..03165a62 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -454,7 +454,7 @@ async fn handling_errors_from_layered_single_routes() { get(handle .layer( ServiceBuilder::new() - .timeout(Duration::from_secs(30)) + .timeout(Duration::from_millis(100)) .layer(TraceLayer::new_for_http()) .into_inner(), )