This commit is contained in:
daddinuz 2019-05-08 23:59:34 +02:00
parent ca612eb28d
commit 938c4e28dc
1 changed files with 9 additions and 6 deletions

View File

@ -188,8 +188,8 @@ pub struct QueryConfig {
impl QueryConfig { impl QueryConfig {
/// 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(QueryPayloadError, &HttpRequest) -> Error + Send + Sync + 'static, F: Fn(QueryPayloadError, &HttpRequest) -> Error + Send + Sync + 'static,
{ {
self.ehandler = Some(Arc::new(f)); self.ehandler = Some(Arc::new(f));
self self
@ -240,12 +240,15 @@ mod tests {
#[test] #[test]
fn test_custom_error_responder() { fn test_custom_error_responder() {
let req = TestRequest::with_uri("/name/user1/").data(QueryConfig::default().error_handler(|e, _| { let req = TestRequest::with_uri("/name/user1/")
let resp = HttpResponse::UnprocessableEntity().finish(); .data(QueryConfig::default().error_handler(|e, _| {
InternalError::from_response(e, resp).into() let resp = HttpResponse::UnprocessableEntity().finish();
})).to_srv_request(); InternalError::from_response(e, resp).into()
})).to_srv_request();
let (req, mut pl) = req.into_parts(); let (req, mut pl) = req.into_parts();
let query = Query::<Id>::from_request(&req, &mut pl); let query = Query::<Id>::from_request(&req, &mut pl);
assert!(query.is_err()); assert!(query.is_err());
assert_eq!(query.unwrap_err().as_response_error().error_response().status(), StatusCode::UNPROCESSABLE_ENTITY); assert_eq!(query.unwrap_err().as_response_error().error_response().status(), StatusCode::UNPROCESSABLE_ENTITY);
} }