Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert error in Result extractor #2581

Merged
merged 4 commits into from Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -7,11 +7,14 @@

### Changed
- `HttpResponse` can now be used as a `Responder` with any body type. [#2567]
- `Result` extractor wrapper can now convert error types. [#2581]
- Associated types in `FromRequest` impl for `Option` and `Result` has changed. [#2581]
- Maximim number of extractors has changed from 10 to 12. [#2582]

[#1988]: https://github.com/actix/actix-web/pull/1988
[#2567]: https://github.com/actix/actix-web/pull/2567
[#2569]: https://github.com/actix/actix-web/pull/2569
[#2581]: https://github.com/actix/actix-web/pull/2581
[#2582]: https://github.com/actix/actix-web/pull/2582


Expand Down
41 changes: 25 additions & 16 deletions src/extract.rs
Expand Up @@ -3,6 +3,7 @@
use std::{
convert::Infallible,
future::Future,
marker::PhantomData,
pin::Pin,
task::{Context, Poll},
};
Expand Down Expand Up @@ -124,12 +125,11 @@ pub trait FromRequest: Sized {
/// );
/// }
/// ```
impl<T: 'static> FromRequest for Option<T>
impl<T> FromRequest for Option<T>
where
T: FromRequest,
T::Future: 'static,
{
type Error = Error;
type Error = Infallible;
type Future = FromRequestOptFuture<T::Future>;

#[inline]
Expand All @@ -152,7 +152,7 @@ where
Fut: Future<Output = Result<T, E>>,
E: Into<Error>,
{
type Output = Result<Option<T>, Error>;
type Output = Result<Option<T>, Infallible>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
Expand Down Expand Up @@ -211,40 +211,42 @@ where
/// );
/// }
/// ```
impl<T> FromRequest for Result<T, T::Error>
impl<T, E> FromRequest for Result<T, E>
where
T: FromRequest + 'static,
T::Error: 'static,
T::Future: 'static,
T: FromRequest,
T::Error: Into<E>,
{
type Error = Error;
type Future = FromRequestResFuture<T::Future>;
type Error = Infallible;
type Future = FromRequestResFuture<T::Future, E>;

#[inline]
fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
FromRequestResFuture {
fut: T::from_request(req, payload),
_phantom: PhantomData,
}
}
}

pin_project! {
pub struct FromRequestResFuture<Fut> {
pub struct FromRequestResFuture<Fut, E> {
#[pin]
fut: Fut,
_phantom: PhantomData<E>,
}
}

impl<Fut, T, E> Future for FromRequestResFuture<Fut>
impl<Fut, T, Ei, E> Future for FromRequestResFuture<Fut, E>
where
Fut: Future<Output = Result<T, E>>,
Fut: Future<Output = Result<T, Ei>>,
Ei: Into<E>,
{
type Output = Result<Result<T, E>, Error>;
type Output = Result<Result<T, E>, Infallible>;

fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.project();
let res = ready!(this.fut.poll(cx));
Poll::Ready(Ok(res))
Poll::Ready(Ok(res.map_err(Into::into)))
}
}

Expand Down Expand Up @@ -481,7 +483,14 @@ mod tests {
.set_payload(Bytes::from_static(b"bye=world"))
.to_http_parts();

let r = Result::<Form<Info>, Error>::from_request(&req, &mut pl)
struct MyError;
impl From<Error> for MyError {
fn from(_: Error) -> Self {
Self
}
}

let r = Result::<Form<Info>, MyError>::from_request(&req, &mut pl)
.await
.unwrap();
assert!(r.is_err());
Expand Down