diff --git a/meilisearch-http/src/error.rs b/meilisearch-http/src/error.rs index 5a1c2064c..1f2f4742d 100644 --- a/meilisearch-http/src/error.rs +++ b/meilisearch-http/src/error.rs @@ -30,6 +30,12 @@ impl ErrorCode for MeilisearchHttpError { } } +impl From for aweb::Error { + fn from(other: MeilisearchHttpError) -> Self { + aweb::Error::from(ResponseError::from(other)) + } +} + #[derive(Debug, Serialize, Deserialize, Clone)] #[serde(rename_all = "camelCase")] pub struct ResponseError { @@ -125,9 +131,8 @@ impl From for PayloadError { } } -pub fn payload_error_handler(err: E) -> ResponseError -where - E: Into, -{ - err.into().into() +impl From for aweb::Error { + fn from(other: PayloadError) -> Self { + aweb::Error::from(ResponseError::from(other)) + } } diff --git a/meilisearch-http/src/lib.rs b/meilisearch-http/src/lib.rs index 870162516..cbb417ab1 100644 --- a/meilisearch-http/src/lib.rs +++ b/meilisearch-http/src/lib.rs @@ -11,13 +11,14 @@ pub mod routes; use std::path::Path; use std::time::Duration; -use crate::error::{MeilisearchHttpError, ResponseError}; +use crate::error::MeilisearchHttpError; use crate::extractors::authentication::AuthConfig; use actix_web::error::JsonPayloadError; +use error::PayloadError; use http::header::CONTENT_TYPE; pub use option::Opt; -use actix_web::web; +use actix_web::{web, HttpRequest}; use extractors::authentication::policies::*; use extractors::payload::PayloadConfig; @@ -102,32 +103,24 @@ pub fn configure_data(config: &mut web::ServiceConfig, data: MeiliSearch, opt: & .app_data( web::JsonConfig::default() .content_type(|mime| mime == mime::APPLICATION_JSON) - .error_handler(|err, req| match err { - JsonPayloadError::ContentType if req.headers().get(CONTENT_TYPE).is_none() => { - ResponseError::from(MeilisearchHttpError::MissingContentType(vec![ - mime::APPLICATION_JSON.to_string(), - ])) - .into() - } - JsonPayloadError::ContentType => { - ResponseError::from(MeilisearchHttpError::InvalidContentType( - req.headers() - .get(CONTENT_TYPE) - .unwrap() - .to_str() - .unwrap_or("unknown") - .to_string(), + .error_handler(|err, req: &HttpRequest| match err { + JsonPayloadError::ContentType => match req.headers().get(CONTENT_TYPE) { + Some(content_type) => MeilisearchHttpError::InvalidContentType( + content_type.to_str().unwrap_or("unknown").to_string(), vec![mime::APPLICATION_JSON.to_string()], - )) - .into() - } - err => error::payload_error_handler(err).into(), + ) + .into(), + None => MeilisearchHttpError::MissingContentType(vec![ + mime::APPLICATION_JSON.to_string(), + ]) + .into(), + }, + err => PayloadError::from(err).into(), }), ) .app_data(PayloadConfig::new(http_payload_size_limit)) .app_data( - web::QueryConfig::default() - .error_handler(|err, _req| error::payload_error_handler(err).into()), + web::QueryConfig::default().error_handler(|err, _req| PayloadError::from(err).into()), ); } diff --git a/meilisearch-http/src/routes/indexes/documents.rs b/meilisearch-http/src/routes/indexes/documents.rs index de53c3b3f..b3163f543 100644 --- a/meilisearch-http/src/routes/indexes/documents.rs +++ b/meilisearch-http/src/routes/indexes/documents.rs @@ -6,6 +6,7 @@ use log::debug; use meilisearch_lib::index_controller::{DocumentAdditionFormat, Update}; use meilisearch_lib::milli::update::IndexDocumentsMethod; use meilisearch_lib::MeiliSearch; +use once_cell::sync::Lazy; use serde::Deserialize; use serde_json::Value; use tokio::sync::mpsc; @@ -176,6 +177,13 @@ async fn document_addition( body: Payload, method: IndexDocumentsMethod, ) -> Result { + const ACCEPTED_CONTENT_TYPE: Lazy> = Lazy::new(|| { + vec![ + "application/json".to_string(), + "application/x-ndjson".to_string(), + "application/csv".to_string(), + ] + }); let format = match content_type { Some("application/json") => DocumentAdditionFormat::Json, Some("application/x-ndjson") => DocumentAdditionFormat::Ndjson, @@ -183,21 +191,14 @@ async fn document_addition( Some(other) => { return Err(MeilisearchHttpError::InvalidContentType( other.to_string(), - vec![ - "application/json".to_string(), - "application/x-ndjson".to_string(), - "application/csv".to_string(), - ], + ACCEPTED_CONTENT_TYPE.clone(), ) .into()) } None => { - return Err(MeilisearchHttpError::MissingContentType(vec![ - "application/json".to_string(), - "application/x-ndjson".to_string(), - "application/csv".to_string(), - ]) - .into()) + return Err( + MeilisearchHttpError::MissingContentType(ACCEPTED_CONTENT_TYPE.clone()).into(), + ) } };