Rename error type

This commit is contained in:
David Pedersen 2021-05-31 20:53:03 +02:00
parent d33be9683c
commit 19cbece1dc
3 changed files with 11 additions and 12 deletions

View file

@ -24,8 +24,8 @@ pub enum Error {
#[error("failed generating the response body")] #[error("failed generating the response body")]
ResponseBody(#[source] BoxError), ResponseBody(#[source] BoxError),
#[error("handler service returned an error")] #[error("some dynamic error happened")]
Service(#[source] BoxError), Dynamic(#[source] BoxError),
#[error("request extension of type `{type_name}` was not set")] #[error("request extension of type `{type_name}` was not set")]
MissingExtension { type_name: &'static str }, MissingExtension { type_name: &'static str },
@ -49,12 +49,11 @@ pub enum Error {
InvalidUtf8, InvalidUtf8,
} }
impl Error { impl From<BoxError> for Error {
/// Create an `Error` from a `BoxError` coming from a `Service` fn from(err: BoxError) -> Self {
pub(crate) fn from_service_error(error: BoxError) -> Error { match err.downcast::<Error>() {
match error.downcast::<Error>() {
Ok(err) => *err, Ok(err) => *err,
Err(err) => Error::Service(err), Err(err) => Error::Dynamic(err),
} }
} }
} }
@ -94,9 +93,9 @@ where
| Error::SerializeResponseBody(_) | Error::SerializeResponseBody(_)
| Error::UnknownUrlParam(_) => make_response(StatusCode::INTERNAL_SERVER_ERROR), | Error::UnknownUrlParam(_) => make_response(StatusCode::INTERNAL_SERVER_ERROR),
Error::Service(err) => match err.downcast::<Error>() { Error::Dynamic(err) => match err.downcast::<Error>() {
Ok(err) => Err(*err), Ok(err) => Err(*err),
Err(err) => Err(Error::Service(err)), Err(err) => Err(Error::Dynamic(err)),
}, },
err @ Error::ConsumeRequestBody(_) => Err(err), err @ Error::ConsumeRequestBody(_) => Err(err),

View file

@ -113,7 +113,7 @@ where
self.svc self.svc
.oneshot(req) .oneshot(req)
.await .await
.map_err(|err| Error::Service(err.into())) .map_err(|err| Error::Dynamic(err.into()))
} }
} }

View file

@ -381,7 +381,7 @@ where
#[inline] #[inline]
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.inner.poll_ready(cx).map_err(Error::from_service_error) self.inner.poll_ready(cx).map_err(Error::from)
} }
#[inline] #[inline]
@ -409,7 +409,7 @@ impl<B> Future for BoxServiceTreeResponseFuture<B> {
self.project() self.project()
.inner .inner
.poll(cx) .poll(cx)
.map_err(Error::from_service_error) .map_err(Error::from)
} }
} }