add error handler for query params error

This commit is contained in:
mpostma 2020-06-03 18:21:03 +02:00
parent aabfe73b38
commit 6df56c4ec5
2 changed files with 17 additions and 5 deletions

View File

@ -3,7 +3,7 @@ use std::fmt;
use actix_http::ResponseBuilder; use actix_http::ResponseBuilder;
use actix_web as aweb; use actix_web as aweb;
use actix_web::error::JsonPayloadError; use actix_web::error::{JsonPayloadError, QueryPayloadError};
use actix_web::http::StatusCode; use actix_web::http::StatusCode;
use serde_json::json; use serde_json::json;
@ -260,7 +260,15 @@ impl From<JsonPayloadError> for Error {
} }
} }
pub fn json_error_handler(err: JsonPayloadError) -> ResponseError { impl From<QueryPayloadError> for Error {
let error = Error::from(err); 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() error.into()
} }

View File

@ -18,7 +18,7 @@ use meilisearch_core::ProcessedUpdateResult;
pub use option::Opt; pub use option::Opt;
pub use self::data::Data; pub use self::data::Data;
use self::error::{json_error_handler, ResponseError}; use self::error::{payload_error_handler, ResponseError};
pub fn create_app( pub fn create_app(
data: &Data, data: &Data,
@ -38,7 +38,11 @@ pub fn create_app(
web::JsonConfig::default() web::JsonConfig::default()
.limit(data.http_payload_size_limit) .limit(data.http_payload_size_limit)
.content_type(|_mime| true) // Accept all mime types .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_html)
.service(routes::load_css) .service(routes::load_css)