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(), 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 { impl fmt::Display for Error {

View file

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

View file

@ -25,18 +25,7 @@ where
}, },
); );
let err = match req.try_into_request() { 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,
),
}
} }
} }