Docs improvements (#37)

This commit is contained in:
David Pedersen
2021-07-22 15:00:33 +02:00
committed by GitHub
parent a658c94f23
commit 8faed8120f
11 changed files with 263 additions and 291 deletions
+7 -6
View File
@@ -9,7 +9,6 @@ use futures_util::ready;
use http::Response;
use pin_project::pin_project;
use std::{
convert::Infallible,
future::Future,
pin::Pin,
task::{Context, Poll},
@@ -25,15 +24,15 @@ pub struct HandleErrorFuture<Fut, F> {
pub(super) f: Option<F>,
}
impl<Fut, F, E, B, Res> Future for HandleErrorFuture<Fut, F>
impl<Fut, F, E, E2, B, Res> Future for HandleErrorFuture<Fut, F>
where
Fut: Future<Output = Result<Response<B>, E>>,
F: FnOnce(E) -> Res,
F: FnOnce(E) -> Result<Res, E2>,
Res: IntoResponse,
B: http_body::Body<Data = Bytes> + Send + Sync + 'static,
B::Error: Into<BoxError> + Send + Sync + 'static,
{
type Output = Result<Response<BoxBody>, Infallible>;
type Output = Result<Response<BoxBody>, E2>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
@@ -42,8 +41,10 @@ where
Ok(res) => Ok(res.map(box_body)).into(),
Err(err) => {
let f = this.f.take().unwrap();
let res = f(err).into_response();
Ok(res.map(box_body)).into()
match f(err) {
Ok(res) => Ok(res.into_response().map(box_body)).into(),
Err(err) => Err(err).into(),
}
}
}
}
+17 -9
View File
@@ -507,16 +507,16 @@ where
}
}
impl<S, F, ReqBody, ResBody, Res> Service<Request<ReqBody>> for HandleError<S, F, ReqBody>
impl<S, F, ReqBody, ResBody, Res, E> Service<Request<ReqBody>> for HandleError<S, F, ReqBody>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>> + Clone,
F: FnOnce(S::Error) -> Res + Clone,
F: FnOnce(S::Error) -> Result<Res, E> + Clone,
Res: IntoResponse,
ResBody: http_body::Body<Data = Bytes> + Send + Sync + 'static,
ResBody::Error: Into<BoxError> + Send + Sync + 'static,
{
type Response = Response<BoxBody>;
type Error = Infallible;
type Error = E;
type Future = future::HandleErrorFuture<Oneshot<S, Request<ReqBody>>, F>;
fn poll_ready(&mut self, _cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
@@ -538,15 +538,16 @@ pub trait ServiceExt<ReqBody, ResBody>:
/// Handle errors from a service.
///
/// `handle_error` takes a closure that will map errors from the service
/// into responses. The closure's return type must implement
/// [`IntoResponse`].
/// into responses. The closure's return type must be `Result<T, E>` where
/// `T` implements [`IntoIntoResponse`](crate::response::IntoResponse).
///
/// # Example
///
/// ```rust,no_run
/// use axum::{service::{self, ServiceExt}, prelude::*};
/// use http::Response;
/// use http::{Response, StatusCode};
/// use tower::{service_fn, BoxError};
/// use std::convert::Infallible;
///
/// // A service that might fail with `std::io::Error`
/// let service = service_fn(|_: Request<Body>| async {
@@ -557,7 +558,10 @@ pub trait ServiceExt<ReqBody, ResBody>:
/// let app = route(
/// "/",
/// service.handle_error(|error: std::io::Error| {
/// // Handle error by returning something that implements `IntoResponse`
/// Ok::<_, Infallible>((
/// StatusCode::INTERNAL_SERVER_ERROR,
/// error.to_string(),
/// ))
/// }),
/// );
/// #
@@ -565,10 +569,14 @@ pub trait ServiceExt<ReqBody, ResBody>:
/// # hyper::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
/// # };
/// ```
fn handle_error<F, Res>(self, f: F) -> HandleError<Self, F, ReqBody>
///
/// It works similarly to [`routing::Layered::handle_error`]. See that for more details.
///
/// [`routing::Layered::handle_error`]: crate::routing::Layered::handle_error
fn handle_error<F, Res, E>(self, f: F) -> HandleError<Self, F, ReqBody>
where
Self: Sized,
F: FnOnce(Self::Error) -> Res,
F: FnOnce(Self::Error) -> Result<Res, E>,
Res: IntoResponse,
ResBody: http_body::Body<Data = Bytes> + Send + Sync + 'static,
ResBody::Error: Into<BoxError> + Send + Sync + 'static,