1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-22 17:41:11 +00:00

return bad request for param parse error

This commit is contained in:
Nikolay Kim 2017-12-02 11:03:41 -08:00
parent ebfd3ac275
commit 0fc01c48d1

View file

@ -4,8 +4,13 @@ use std::path::PathBuf;
use std::ops::Index; use std::ops::Index;
use std::str::FromStr; use std::str::FromStr;
use std::collections::HashMap; use std::collections::HashMap;
use failure::Fail;
use http::{StatusCode};
use regex::{Regex, RegexSet, Captures}; use regex::{Regex, RegexSet, Captures};
use body::Body;
use httpresponse::HttpResponse;
use error::{ResponseError, UriSegmentError}; use error::{ResponseError, UriSegmentError};
/// A trait to abstract the idea of creating a new instance of a type from a path parameter. /// A trait to abstract the idea of creating a new instance of a type from a path parameter.
@ -73,7 +78,7 @@ impl Params {
/// ///
/// ```rust,ignore /// ```rust,ignore
/// fn index(req: HttpRequest) -> String { /// fn index(req: HttpRequest) -> String {
/// let ivalue: isize = req.match_info().query()?; /// let ivalue: isize = req.match_info().query("val")?;
/// format!("isuze value: {:?}", ivalue) /// format!("isuze value: {:?}", ivalue)
/// } /// }
/// ``` /// ```
@ -142,13 +147,32 @@ impl FromParam for PathBuf {
} }
} }
#[derive(Fail, Debug)]
#[fail(display="Error")]
pub struct BadRequest<T>(T);
impl<T> BadRequest<T> {
pub fn cause(&self) -> &T {
&self.0
}
}
impl<T> ResponseError for BadRequest<T>
where T: Send + Sync + std::fmt::Debug +std::fmt::Display + 'static,
BadRequest<T>: Fail
{
fn error_response(&self) -> HttpResponse {
HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
}
}
macro_rules! FROM_STR { macro_rules! FROM_STR {
($type:ty) => { ($type:ty) => {
impl FromParam for $type { impl FromParam for $type {
type Err = <$type as FromStr>::Err; type Err = BadRequest<<$type as FromStr>::Err>;
fn from_param(val: &str) -> Result<Self, Self::Err> { fn from_param(val: &str) -> Result<Self, Self::Err> {
<$type as FromStr>::from_str(val) <$type as FromStr>::from_str(val).map_err(|e| BadRequest(e))
} }
} }
} }