diff --git a/meilisearch-http/src/error.rs b/meilisearch-http/src/error.rs index e24a57bb8..1fa0c5499 100644 --- a/meilisearch-http/src/error.rs +++ b/meilisearch-http/src/error.rs @@ -4,6 +4,7 @@ use actix_http::ResponseBuilder; use actix_web as aweb; use actix_web::http::StatusCode; use serde_json::json; +use actix_web::error::JsonPayloadError; #[derive(Debug)] pub enum ResponseError { @@ -23,6 +24,8 @@ pub enum ResponseError { FilterParsing(String), RetrieveDocument(u64, String), SearchDocuments(String), + PayloadTooLarge, + UnsupportedMediaType, FacetExpression(String), } @@ -108,6 +111,8 @@ impl fmt::Display for ResponseError { Self::RetrieveDocument(id, err) => write!(f, "impossible to retrieve the document with id: {}; {}", id, err), Self::SearchDocuments(err) => write!(f, "impossible to search documents; {}", err), Self::FacetExpression(e) => write!(f, "error parsing facet filter expression: {}", e), + Self::PayloadTooLarge => f.write_str("Payload to large"), + Self::UnsupportedMediaType => f.write_str("Unsupported media type") } } } @@ -138,6 +143,8 @@ impl aweb::error::ResponseError for ResponseError { Self::MissingAuthorizationHeader => StatusCode::FORBIDDEN, Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR, Self::Maintenance => StatusCode::SERVICE_UNAVAILABLE, + Self::PayloadTooLarge => StatusCode::PAYLOAD_TOO_LARGE, + Self::UnsupportedMediaType => StatusCode::UNSUPPORTED_MEDIA_TYPE, } } } @@ -190,3 +197,19 @@ impl From for ResponseError { ResponseError::Internal(err.to_string()) } } + +impl From for ResponseError { + fn from(err: JsonPayloadError) -> ResponseError { + match err { + JsonPayloadError::Deserialize(err) => ResponseError::BadRequest(format!("BAD_JSON: {}", err)), + JsonPayloadError::Overflow => ResponseError::PayloadTooLarge, + JsonPayloadError::ContentType => ResponseError::UnsupportedMediaType, + JsonPayloadError::Payload(err) => ResponseError::BadRequest(format!("Problem decoding request: {}", err)), + } + } +} + + +pub fn json_error_handler(err: JsonPayloadError) -> ResponseError { + err.into() +} diff --git a/meilisearch-http/src/lib.rs b/meilisearch-http/src/lib.rs index d4f2822ad..c4445ed3d 100644 --- a/meilisearch-http/src/lib.rs +++ b/meilisearch-http/src/lib.rs @@ -8,6 +8,7 @@ pub mod option; pub mod routes; pub use self::data::Data; +use self::error::json_error_handler; use actix_http::Error; use actix_service::ServiceFactory; use actix_web::{dev, web, App}; @@ -28,7 +29,12 @@ pub fn create_app( > { App::new() .app_data(web::Data::new(data.clone())) - .app_data(web::JsonConfig::default().limit(1024 * 1024 * 10)) // Json Limit of 10Mb + .app_data( + web::JsonConfig::default() + .limit(1024 * 1024 * 10) + .content_type(|_mime| true) // Accept all mime types + .error_handler(|err, _req| json_error_handler(err).into()), + ) .service(routes::load_html) .service(routes::load_css) .configure(routes::document::services)