Revamp error handling model (#402)

* Revamp error handling model

* changelog improvements and typo fixes

* Fix a few more Infallible bounds

* minor docs fixes
This commit is contained in:
David Pedersen
2021-10-24 17:33:03 +00:00
committed by GitHub
parent 1a78a3f224
commit f10508db0b
19 changed files with 501 additions and 558 deletions
-26
View File
@@ -5,7 +5,6 @@ use crate::{
extract::{FromRequest, RequestParts},
response::IntoResponse,
routing::{EmptyRouter, MethodFilter},
service::HandleError,
util::Either,
BoxError,
};
@@ -264,9 +263,6 @@ pub trait Handler<B, T>: Clone + Send + Sized + 'static {
/// # axum::Server::bind(&"".parse().unwrap()).serve(app.into_make_service()).await.unwrap();
/// # };
/// ```
///
/// When adding middleware that might fail its recommended to handle those
/// errors. See [`Layered::handle_error`] for more details.
fn layer<L>(self, layer: L) -> Layered<L::Service, T>
where
L: Layer<OnMethod<Self, B, T, EmptyRouter>>,
@@ -426,28 +422,6 @@ impl<S, T> Layered<S, T> {
_input: PhantomData,
}
}
/// Create a new [`Layered`] handler where errors will be handled using the
/// given closure.
///
/// This is used to convert errors to responses rather than simply
/// terminating the connection.
///
/// It works similarly to [`routing::Router::handle_error`]. See that for more details.
///
/// [`routing::Router::handle_error`]: crate::routing::Router::handle_error
pub fn handle_error<F, ReqBody, ResBody, Res, E>(
self,
f: F,
) -> Layered<HandleError<S, F, ReqBody>, T>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
F: FnOnce(S::Error) -> Result<Res, E>,
Res: IntoResponse,
{
let svc = HandleError::new(self.svc, f);
Layered::new(svc)
}
}
/// A handler [`Service`] that accepts requests based on a [`MethodFilter`] and