Skip to content

Commit

Permalink
return custom Rejection directly
Browse files Browse the repository at this point in the history
  • Loading branch information
hwchen committed Oct 29, 2019
1 parent fb1ec58 commit 10ed1dd
Showing 1 changed file with 35 additions and 13 deletions.
48 changes: 35 additions & 13 deletions src/filters/path.rs
Expand Up @@ -250,13 +250,11 @@ pub fn param<T: FromStr + Send + 'static>() -> impl Filter<Extract = One<T>, Err
/// segment, and if successful, the value is returned as the `Filter`'s
/// "extracted" value.
///
/// If the value could not be parsed, rejects with a user-defined [`Rejection`][].
/// If the value could not be parsed, rejects with a custom [`Rejection`][].
///
/// The associated `Err` on the `FromStr` must impl `From` for `Rejection`. See example.
///
/// If [`warp::reject::custom`][] is used to create a `Rejection`, a [`recover`][] filter
/// should convert this `Rejection` into a `Reply`, or else this will be returned as a
/// `500 Internal Server Error`.
/// Since [`warp::reject::custom`][] is used to create a `Rejection` under the hood,
/// a [`recover`][] filter should convert this `Rejection` into a `Reply`, or else
/// this will be returned as a `500 Internal Server Error`.
///
/// [`Rejection`]: ../../reject/struct.Rejection.html
/// [`recover`]: ../trait.Filter.html#method.recover
Expand All @@ -266,9 +264,17 @@ pub fn param<T: FromStr + Send + 'static>() -> impl Filter<Extract = One<T>, Err
/// # Example
///
/// ```
/// use futures::future;
/// use std::convert::From;
/// use std::str::FromStr;
/// use warp::{Filter, Rejection};
/// use warp::{
/// http::{
/// status,
/// Response,
/// },
/// Filter,
/// Rejection
/// };
///
/// #[derive(Debug)]
/// struct MyStruct {
Expand All @@ -285,24 +291,40 @@ pub fn param<T: FromStr + Send + 'static>() -> impl Filter<Extract = One<T>, Err
/// }
/// }
///
/// #[derive(Debug)]
/// struct MyError;
///
/// impl From<MyError> for Rejection {
/// fn from(err: MyError) -> Rejection {
/// warp::reject::custom("My Custom Rejection")
/// impl std::fmt::Display for MyError {
/// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
/// write!(f, "MyError")
/// }
/// }
///
/// impl std::error::Error for MyError {};
///
/// let route = warp::path::param_with_err()
/// .map(|id: MyStruct| {
/// format!("You asked for /{:?}", id)
/// });
/// })
/// .recover(|err: Rejection| {
/// let err = {
/// if let Some(e) = err.find_cause::<MyError>() {
/// Ok(Response::builder()
/// .status(status::StatusCode::from_u16(404).unwrap())
/// .body(e.to_string())
/// )
/// } else {
/// Err(err)
/// }
/// };
/// future::ready(err)
/// });
///
/// ```
pub fn param_with_err<T>() -> impl Filter<Extract = One<T>, Error = Rejection> + Copy
where
T: FromStr + Send + 'static,
T::Err: Into<Rejection>,
T::Err: Into<reject::Cause>,
{
segment(|seg| {
log::trace!("param?: {:?}", seg);
Expand All @@ -311,7 +333,7 @@ where
}
T::from_str(seg).map(one).map_err(|err| {
#[allow(deprecated)]
err.into()
reject::custom(err.into())
})
})
}
Expand Down

0 comments on commit 10ed1dd

Please sign in to comment.