From 6df56c4ec5af49b97b1ee962fcc45d8df2da85e0 Mon Sep 17 00:00:00 2001 From: mpostma Date: Wed, 3 Jun 2020 18:21:03 +0200 Subject: [PATCH 1/2] add error handler for query params error --- meilisearch-http/src/error.rs | 14 +++++++++++--- meilisearch-http/src/lib.rs | 8 ++++++-- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/meilisearch-http/src/error.rs b/meilisearch-http/src/error.rs index 3f57e3a92..953a5457e 100644 --- a/meilisearch-http/src/error.rs +++ b/meilisearch-http/src/error.rs @@ -3,7 +3,7 @@ use std::fmt; use actix_http::ResponseBuilder; use actix_web as aweb; -use actix_web::error::JsonPayloadError; +use actix_web::error::{JsonPayloadError, QueryPayloadError}; use actix_web::http::StatusCode; use serde_json::json; @@ -260,7 +260,15 @@ impl From for Error { } } -pub fn json_error_handler(err: JsonPayloadError) -> ResponseError { - let error = Error::from(err); +impl From for Error { + fn from(err: QueryPayloadError) -> Error { + match err { + QueryPayloadError::Deserialize(err) => Error::BadRequest(format!("Invalid query parameters: {}", err)), + } + } +} + +pub fn payload_error_handler>(err: E) -> ResponseError { + let error: Error = err.into(); error.into() } diff --git a/meilisearch-http/src/lib.rs b/meilisearch-http/src/lib.rs index 4b68c5120..486f4a39f 100644 --- a/meilisearch-http/src/lib.rs +++ b/meilisearch-http/src/lib.rs @@ -18,7 +18,7 @@ use meilisearch_core::ProcessedUpdateResult; pub use option::Opt; pub use self::data::Data; -use self::error::{json_error_handler, ResponseError}; +use self::error::{payload_error_handler, ResponseError}; pub fn create_app( data: &Data, @@ -38,7 +38,11 @@ pub fn create_app( web::JsonConfig::default() .limit(data.http_payload_size_limit) .content_type(|_mime| true) // Accept all mime types - .error_handler(|err, _req| json_error_handler(err).into()), + .error_handler(|err, _req| payload_error_handler(err).into()), + ) + .app_data( + web::QueryConfig::default() + .error_handler(|err, _req| payload_error_handler(err).into()) ) .service(routes::load_html) .service(routes::load_css) From cf6c6eb11761208bfa3420630fc151453a7cebf8 Mon Sep 17 00:00:00 2001 From: mpostma Date: Wed, 3 Jun 2020 18:27:20 +0200 Subject: [PATCH 2/2] test invalid query params --- meilisearch-http/tests/search.rs | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/meilisearch-http/tests/search.rs b/meilisearch-http/tests/search.rs index 559c1d0cc..4597ee7f1 100644 --- a/meilisearch-http/tests/search.rs +++ b/meilisearch-http/tests/search.rs @@ -1414,3 +1414,14 @@ async fn highlight_cropped_text() { .unwrap() , &Value::String(expected_response.to_owned())); } + +#[actix_rt::test] +async fn well_formated_error_with_bad_request_params() { + let mut server = common::Server::with_uid("test"); + let query = "foo=bar"; + let (response, _status_code) = server.search(query).await; + assert!(response.get("message").is_some()); + assert!(response.get("errorCode").is_some()); + assert!(response.get("errorType").is_some()); + assert!(response.get("errorLink").is_some()); +}