diff --git a/meilisearch-error/src/lib.rs b/meilisearch-error/src/lib.rs index 2e4b50ef6..bdb17ff22 100644 --- a/meilisearch-error/src/lib.rs +++ b/meilisearch-error/src/lib.rs @@ -83,6 +83,8 @@ pub enum Code { DumpAlreadyInProgress, DumpProcessFailed, + + MissingContentType, } impl Code { @@ -154,6 +156,7 @@ impl Code { DumpProcessFailed => { ErrCode::internal("dump_process_failed", StatusCode::INTERNAL_SERVER_ERROR) } + MissingContentType => ErrCode::invalid("missing_content_type", StatusCode::UNSUPPORTED_MEDIA_TYPE), } } diff --git a/meilisearch-http/src/error.rs b/meilisearch-http/src/error.rs index bb18ad6f4..bb0b06e27 100644 --- a/meilisearch-http/src/error.rs +++ b/meilisearch-http/src/error.rs @@ -9,6 +9,20 @@ use aweb::error::{JsonPayloadError, QueryPayloadError}; use meilisearch_error::{Code, ErrorCode}; use serde::{Deserialize, Serialize}; +#[derive(Debug, thiserror::Error)] +pub enum MeilisearchHttpError { + #[error("A Content-Type header is missing. Accepted values for the Content-Type header are: \"application/json\", \"application/x-ndjson\", \"test/csv\"")] + MissingContentType, +} + +impl ErrorCode for MeilisearchHttpError { + fn error_code(&self) -> Code { + match self { + MeilisearchHttpError::MissingContentType => Code::MissingContentType, + } + } +} + #[derive(Debug, Serialize, Deserialize, Clone)] #[serde(rename_all = "camelCase")] pub struct ResponseError { diff --git a/meilisearch-http/src/routes/indexes/documents.rs b/meilisearch-http/src/routes/indexes/documents.rs index 38aa4282f..ca17efad7 100644 --- a/meilisearch-http/src/routes/indexes/documents.rs +++ b/meilisearch-http/src/routes/indexes/documents.rs @@ -10,7 +10,7 @@ use serde::Deserialize; use serde_json::Value; use tokio::sync::mpsc; -use crate::error::ResponseError; +use crate::error::{MeilisearchHttpError, ResponseError}; use crate::extractors::authentication::{policies::*, GuardedData}; use crate::extractors::payload::Payload; use crate::routes::IndexParam; @@ -66,7 +66,7 @@ pub fn configure(cfg: &mut web::ServiceConfig) { .route( web::post() .guard(empty_application_type) - .to(HttpResponse::UnsupportedMediaType), + .to(missing_content_type_error), ) .route(web::post().guard(guard_json).to(add_documents_json)) .route(web::post().guard(guard_ndjson).to(add_documents_ndjson)) @@ -76,7 +76,7 @@ pub fn configure(cfg: &mut web::ServiceConfig) { .route( web::put() .guard(empty_application_type) - .to(HttpResponse::UnsupportedMediaType), + .to(missing_content_type_error), ) .route(web::put().guard(guard_json).to(update_documents_json)) .route(web::put().guard(guard_ndjson).to(update_documents_ndjson)) @@ -93,6 +93,10 @@ pub fn configure(cfg: &mut web::ServiceConfig) { ); } +async fn missing_content_type_error() -> Result { + Err(MeilisearchHttpError::MissingContentType.into()) +} + pub async fn get_document( meilisearch: GuardedData, path: web::Path,