mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-28 23:38:20 +01:00
Remove duplicated HandleError
This commit is contained in:
parent
7620f17edc
commit
46f74895ce
3 changed files with 4 additions and 66 deletions
|
@ -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(
|
||||
|
|
|
@ -385,7 +385,7 @@ pub struct Layered<S>(S);
|
|||
impl<S> RoutingDsl for Layered<S> {}
|
||||
|
||||
impl<S> Layered<S> {
|
||||
pub fn handle_error<F, B, Res>(self, f: F) -> HandleError<S, F>
|
||||
pub fn handle_error<F, B, Res>(self, f: F) -> crate::service::HandleError<S, F>
|
||||
where
|
||||
S: Service<Request<Body>, Response = Response<B>> + Clone,
|
||||
F: FnOnce(S::Error) -> Res,
|
||||
|
@ -393,7 +393,7 @@ impl<S> Layered<S> {
|
|||
B: http_body::Body<Data = Bytes> + Send + Sync + 'static,
|
||||
B::Error: Into<BoxError> + 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<S, F> {
|
||||
inner: S,
|
||||
f: F,
|
||||
}
|
||||
|
||||
impl<S, F> RoutingDsl for HandleError<S, F> {}
|
||||
|
||||
impl<S, F, B, Res> Service<Request<Body>> for HandleError<S, F>
|
||||
where
|
||||
S: Service<Request<Body>, Response = Response<B>> + Clone,
|
||||
F: FnOnce(S::Error) -> Res + Clone,
|
||||
Res: IntoResponse,
|
||||
B: http_body::Body<Data = Bytes> + Send + Sync + 'static,
|
||||
B::Error: Into<BoxError> + Send + Sync + 'static,
|
||||
{
|
||||
type Response = Response<BoxBody>;
|
||||
type Error = Infallible;
|
||||
type Future = HandleErrorFuture<Oneshot<S, Request<Body>>, F>;
|
||||
|
||||
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Request<Body>) -> Self::Future {
|
||||
HandleErrorFuture {
|
||||
inner: self.inner.clone().oneshot(req),
|
||||
f: Some(self.f.clone()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[pin_project]
|
||||
pub struct HandleErrorFuture<Fut, F> {
|
||||
#[pin]
|
||||
inner: Fut,
|
||||
f: Option<F>,
|
||||
}
|
||||
|
||||
impl<Fut, F, B, E, Res> Future for HandleErrorFuture<Fut, F>
|
||||
where
|
||||
Fut: Future<Output = Result<Response<B>, E>>,
|
||||
F: FnOnce(E) -> Res,
|
||||
Res: IntoResponse,
|
||||
B: http_body::Body<Data = Bytes> + Send + Sync + 'static,
|
||||
B::Error: Into<BoxError> + Send + Sync + 'static,
|
||||
{
|
||||
type Output = Result<Response<BoxBody>, Infallible>;
|
||||
|
||||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
|
||||
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<S>(spec: &str, svc: S) -> Nested<S, EmptyRouter>
|
||||
|
|
|
@ -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(),
|
||||
)
|
||||
|
|
Loading…
Reference in a new issue