mirror of
https://github.com/fafhrd91/actix-web
synced 2024-11-24 00:21:08 +01:00
rename PathPayloadError and test for path config
This commit is contained in:
parent
13e618b128
commit
e7ba67e1a8
@ -4,6 +4,8 @@
|
|||||||
|
|
||||||
### Add
|
### Add
|
||||||
|
|
||||||
|
* Add support for PathConfig #903
|
||||||
|
|
||||||
* Add `middleware::identity::RequestIdentity` trait to `get_identity` from `HttpMessage`.
|
* Add `middleware::identity::RequestIdentity` trait to `get_identity` from `HttpMessage`.
|
||||||
|
|
||||||
### Changes
|
### Changes
|
||||||
|
10
src/error.rs
10
src/error.rs
@ -94,19 +94,17 @@ impl ResponseError for JsonPayloadError {
|
|||||||
|
|
||||||
/// A set of errors that can occur during parsing request paths
|
/// A set of errors that can occur during parsing request paths
|
||||||
#[derive(Debug, Display, From)]
|
#[derive(Debug, Display, From)]
|
||||||
pub enum PathPayloadError {
|
pub enum PathError {
|
||||||
/// Deserialize error
|
/// Deserialize error
|
||||||
#[display(fmt = "Path deserialize error: {}", _0)]
|
#[display(fmt = "Path deserialize error: {}", _0)]
|
||||||
Deserialize(de::Error),
|
Deserialize(de::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Return `BadRequest` for `PathPayloadError`
|
/// Return `BadRequest` for `PathError`
|
||||||
impl ResponseError for PathPayloadError {
|
impl ResponseError for PathError {
|
||||||
fn error_response(&self) -> HttpResponse {
|
fn error_response(&self) -> HttpResponse {
|
||||||
match *self {
|
match *self {
|
||||||
PathPayloadError::Deserialize(_) => {
|
PathError::Deserialize(_) => HttpResponse::new(StatusCode::BAD_REQUEST),
|
||||||
HttpResponse::new(StatusCode::BAD_REQUEST)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ use actix_router::PathDeserializer;
|
|||||||
use serde::de;
|
use serde::de;
|
||||||
|
|
||||||
use crate::dev::Payload;
|
use crate::dev::Payload;
|
||||||
use crate::error::PathPayloadError;
|
use crate::error::PathError;
|
||||||
use crate::request::HttpRequest;
|
use crate::request::HttpRequest;
|
||||||
use crate::FromRequest;
|
use crate::FromRequest;
|
||||||
|
|
||||||
@ -178,7 +178,7 @@ where
|
|||||||
req.path()
|
req.path()
|
||||||
);
|
);
|
||||||
if let Some(error_handler) = error_handler {
|
if let Some(error_handler) = error_handler {
|
||||||
let e = PathPayloadError::Deserialize(e);
|
let e = PathError::Deserialize(e);
|
||||||
(error_handler)(e, req)
|
(error_handler)(e, req)
|
||||||
} else {
|
} else {
|
||||||
ErrorNotFound(e)
|
ErrorNotFound(e)
|
||||||
@ -190,48 +190,48 @@ where
|
|||||||
/// Path extractor configuration
|
/// Path extractor configuration
|
||||||
///
|
///
|
||||||
/// ```rust
|
/// ```rust
|
||||||
// #[macro_use]
|
/// # #[macro_use]
|
||||||
// extern crate serde_derive;
|
/// # extern crate serde_derive;
|
||||||
// use actix_web::web::PathConfig;
|
/// use actix_web::web::PathConfig;
|
||||||
// use actix_web::{error, web, App, FromRequest, HttpResponse};
|
/// use actix_web::{error, web, App, FromRequest, HttpResponse};
|
||||||
|
///
|
||||||
// #[derive(Deserialize, Debug)]
|
/// #[derive(Deserialize, Debug)]
|
||||||
// enum Folder {
|
/// enum Folder {
|
||||||
// #[serde(rename = "inbox")]
|
/// #[serde(rename = "inbox")]
|
||||||
// Inbox,
|
/// Inbox,
|
||||||
// #[serde(rename = "outbox")]
|
/// #[serde(rename = "outbox")]
|
||||||
// Outbox,
|
/// Outbox,
|
||||||
// }
|
/// }
|
||||||
|
///
|
||||||
// /// deserialize `Info` from request's path
|
/// // deserialize `Info` from request's path
|
||||||
// fn index(folder: web::Path<Folder>) -> String {
|
/// fn index(folder: web::Path<Folder>) -> String {
|
||||||
// format!("Selected folder: {}!", folder)
|
/// format!("Selected folder: {}!", folder)
|
||||||
// }
|
/// }
|
||||||
|
///
|
||||||
// fn main() {
|
/// fn main() {
|
||||||
// let app = App::new().service(
|
/// let app = App::new().service(
|
||||||
// web::resource("messages/{folder}")
|
/// web::resource("/messages/{folder}")
|
||||||
// .data(PathConfig::default().error_handler(|err, req| {
|
/// .data(PathConfig::default().error_handler(|err, req| {
|
||||||
// error::InternalError::from_response(
|
/// error::InternalError::from_response(
|
||||||
// err,
|
/// err,
|
||||||
// HttpResponse::Conflict().finish(),
|
/// HttpResponse::Conflict().finish(),
|
||||||
// )
|
/// )
|
||||||
// .into()
|
/// .into()
|
||||||
// }))
|
/// }))
|
||||||
// .route(web::post().to(index)),
|
/// .route(web::post().to(index)),
|
||||||
// );
|
/// );
|
||||||
// }
|
/// }
|
||||||
/// ```
|
/// ```
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct PathConfig {
|
pub struct PathConfig {
|
||||||
ehandler: Option<Arc<Fn(PathPayloadError, &HttpRequest) -> Error + Send + Sync>>,
|
ehandler: Option<Arc<Fn(PathError, &HttpRequest) -> Error + Send + Sync>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PathConfig {
|
impl PathConfig {
|
||||||
/// Set custom error handler
|
/// Set custom error handler
|
||||||
pub fn error_handler<F>(mut self, f: F) -> Self
|
pub fn error_handler<F>(mut self, f: F) -> Self
|
||||||
where
|
where
|
||||||
F: Fn(PathPayloadError, &HttpRequest) -> Error + Send + Sync + 'static,
|
F: Fn(PathError, &HttpRequest) -> Error + Send + Sync + 'static,
|
||||||
{
|
{
|
||||||
self.ehandler = Some(Arc::new(f));
|
self.ehandler = Some(Arc::new(f));
|
||||||
self
|
self
|
||||||
@ -252,6 +252,7 @@ mod tests {
|
|||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::test::{block_on, TestRequest};
|
use crate::test::{block_on, TestRequest};
|
||||||
|
use crate::{error, http, HttpResponse};
|
||||||
|
|
||||||
#[derive(Deserialize, Debug, Display)]
|
#[derive(Deserialize, Debug, Display)]
|
||||||
#[display(fmt = "MyStruct({}, {})", key, value)]
|
#[display(fmt = "MyStruct({}, {})", key, value)]
|
||||||
@ -347,4 +348,21 @@ mod tests {
|
|||||||
assert_eq!(res[1], "32".to_owned());
|
assert_eq!(res[1], "32".to_owned());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_custom_err_handler() {
|
||||||
|
let (req, mut pl) = TestRequest::with_uri("/name/user1/")
|
||||||
|
.data(PathConfig::default().error_handler(|err, _| {
|
||||||
|
error::InternalError::from_response(
|
||||||
|
err,
|
||||||
|
HttpResponse::Conflict().finish(),
|
||||||
|
)
|
||||||
|
.into()
|
||||||
|
}))
|
||||||
|
.to_http_parts();
|
||||||
|
|
||||||
|
let s = block_on(Path::<(usize,)>::from_request(&req, &mut pl)).unwrap_err();
|
||||||
|
let res: HttpResponse = s.into();
|
||||||
|
|
||||||
|
assert_eq!(res.status(), http::StatusCode::CONFLICT);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user