mirror of
https://github.com/tokio-rs/axum.git
synced 2024-12-28 23:38:20 +01:00
Change error type for RequestParts::try_into_request
(#639)
This commit is contained in:
parent
9b4232b786
commit
455fc46bf3
3 changed files with 8 additions and 30 deletions
|
@ -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 {
|
||||
|
|
|
@ -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,
|
||||
));
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue