mirror of
https://github.com/actix/actix-extras.git
synced 2025-06-25 09:59:21 +02:00
make ErrorBadRequest type useful
This commit is contained in:
44
src/error.rs
44
src/error.rs
@ -426,6 +426,50 @@ impl From<UrlParseError> for UrlGenerationError {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// Helper type that can wrap any error and generate *BAD REQUEST* response.
|
||||
///
|
||||
/// In following example any `io::Error` will be converted into "BAD REQUEST" response
|
||||
/// as oposite to *INNTERNAL SERVER ERROR* which is defined by default.
|
||||
///
|
||||
/// ```rust
|
||||
/// # extern crate actix_web;
|
||||
/// # use actix_web::*;
|
||||
/// use actix_web::fs::NamedFile;
|
||||
///
|
||||
/// fn index(req: HttpRequest) -> Result<fs::NamedFile> {
|
||||
/// let f = NamedFile::open("test.txt").map_err(error::ErrorBadRequest)?;
|
||||
/// Ok(f)
|
||||
/// }
|
||||
/// # fn main() {}
|
||||
/// ```
|
||||
#[derive(Debug)]
|
||||
pub struct ErrorBadRequest<T>(pub T);
|
||||
|
||||
unsafe impl<T> Sync for ErrorBadRequest<T> {}
|
||||
unsafe impl<T> Send for ErrorBadRequest<T> {}
|
||||
|
||||
impl<T> ErrorBadRequest<T> {
|
||||
pub fn cause(&self) -> &T {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug + 'static> Fail for ErrorBadRequest<T> {}
|
||||
impl<T: fmt::Debug + 'static> fmt::Display for ErrorBadRequest<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "BadRequest({:?})", self.0)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> ResponseError for ErrorBadRequest<T>
|
||||
where T: Send + Sync + fmt::Debug + 'static,
|
||||
{
|
||||
fn error_response(&self) -> HttpResponse {
|
||||
HttpResponse::new(StatusCode::BAD_REQUEST, Body::Empty)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::error::Error as StdError;
|
||||
|
@ -81,7 +81,7 @@ pub mod httpcodes;
|
||||
pub mod multipart;
|
||||
pub mod middlewares;
|
||||
pub mod pred;
|
||||
pub use error::{Error, Result};
|
||||
pub use error::{Error, Result, ResponseError};
|
||||
pub use body::{Body, Binary};
|
||||
pub use application::Application;
|
||||
pub use httprequest::HttpRequest;
|
||||
|
30
src/param.rs
30
src/param.rs
@ -2,14 +2,9 @@ use std;
|
||||
use std::ops::Index;
|
||||
use std::path::PathBuf;
|
||||
use std::str::FromStr;
|
||||
|
||||
use failure::Fail;
|
||||
use http::{StatusCode};
|
||||
use smallvec::SmallVec;
|
||||
|
||||
use body::Body;
|
||||
use httpresponse::HttpResponse;
|
||||
use error::{ResponseError, UriSegmentError};
|
||||
use error::{ResponseError, UriSegmentError, ErrorBadRequest};
|
||||
|
||||
|
||||
/// A trait to abstract the idea of creating a new instance of a type from a path parameter.
|
||||
@ -132,32 +127,13 @@ 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 {
|
||||
($type:ty) => {
|
||||
impl FromParam for $type {
|
||||
type Err = BadRequest<<$type as FromStr>::Err>;
|
||||
type Err = ErrorBadRequest<<$type as FromStr>::Err>;
|
||||
|
||||
fn from_param(val: &str) -> Result<Self, Self::Err> {
|
||||
<$type as FromStr>::from_str(val).map_err(BadRequest)
|
||||
<$type as FromStr>::from_str(val).map_err(ErrorBadRequest)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -169,6 +169,9 @@ mod tests {
|
||||
|
||||
let pred = Header("transfer-encoding", "other");
|
||||
assert!(!pred.check(&mut req));
|
||||
|
||||
let pred = Header("content-tye", "other");
|
||||
assert!(!pred.check(&mut req));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
Reference in New Issue
Block a user