Change error type for RequestParts::try_into_request (#639)

This commit is contained in:
Nylonicious 2021-12-21 10:51:47 +01:00 committed by David Pedersen
parent 9b4232b786
commit 455fc46bf3
3 changed files with 8 additions and 30 deletions

View file

@ -14,16 +14,6 @@ impl Error {
inner: error.into(),
}
}
pub(crate) fn downcast<T>(self) -> Result<T, Self>
where
T: StdError + 'static,
{
match self.inner.downcast::<T>() {
Ok(t) => Ok(*t),
Err(err) => Err(*err.downcast().unwrap()),
}
}
}
impl fmt::Display for Error {

View file

@ -6,7 +6,6 @@
use self::rejection::*;
use crate::response::IntoResponse;
use crate::Error;
use async_trait::async_trait;
use http::{Extensions, HeaderMap, Method, Request, Uri, Version};
use std::convert::Infallible;
@ -128,7 +127,7 @@ impl<B> RequestParts<B> {
/// [`take_headers`]: RequestParts::take_headers
/// [`take_extensions`]: RequestParts::take_extensions
/// [`take_body`]: RequestParts::take_body
pub fn try_into_request(self) -> Result<Request<B>, Error> {
pub fn try_into_request(self) -> Result<Request<B>, RequestAlreadyExtracted> {
let Self {
method,
uri,
@ -141,9 +140,9 @@ impl<B> RequestParts<B> {
let mut req = if let Some(body) = body.take() {
Request::new(body)
} else {
return Err(Error::new(RequestAlreadyExtracted::BodyAlreadyExtracted(
return Err(RequestAlreadyExtracted::BodyAlreadyExtracted(
BodyAlreadyExtracted,
)));
));
};
*req.method_mut() = method;
@ -153,16 +152,16 @@ impl<B> RequestParts<B> {
if let Some(headers) = headers.take() {
*req.headers_mut() = headers;
} else {
return Err(Error::new(
RequestAlreadyExtracted::HeadersAlreadyExtracted(HeadersAlreadyExtracted),
return Err(RequestAlreadyExtracted::HeadersAlreadyExtracted(
HeadersAlreadyExtracted,
));
}
if let Some(extensions) = extensions.take() {
*req.extensions_mut() = extensions;
} else {
return Err(Error::new(
RequestAlreadyExtracted::ExtensionsAlreadyExtracted(ExtensionsAlreadyExtracted),
return Err(RequestAlreadyExtracted::ExtensionsAlreadyExtracted(
ExtensionsAlreadyExtracted,
));
}

View file

@ -25,18 +25,7 @@ where
},
);
let err = match req.try_into_request() {
Ok(req) => return Ok(req),
Err(err) => err,
};
match err.downcast::<RequestAlreadyExtracted>() {
Ok(err) => return Err(err),
Err(err) => unreachable!(
"Unexpected error type from `try_into_request`: `{:?}`. This is a bug in axum, please file an issue",
err,
),
}
req.try_into_request()
}
}