Merge pull request #751 from MarinPostma/handle-path-error

Handle url params errors
This commit is contained in:
Clément Renault 2020-06-04 15:22:54 +02:00 committed by GitHub
commit 32843e9ade
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 5 deletions

View File

@ -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<JsonPayloadError> for Error {
}
}
pub fn json_error_handler(err: JsonPayloadError) -> ResponseError {
let error = Error::from(err);
impl From<QueryPayloadError> for Error {
fn from(err: QueryPayloadError) -> Error {
match err {
QueryPayloadError::Deserialize(err) => Error::BadRequest(format!("Invalid query parameters: {}", err)),
}
}
}
pub fn payload_error_handler<E: Into<Error>>(err: E) -> ResponseError {
let error: Error = err.into();
error.into()
}

View File

@ -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)

View File

@ -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());
}