From ddd40d87a70d703c38e854c13960cbd6073a683f Mon Sep 17 00:00:00 2001 From: mpostma Date: Thu, 30 Sep 2021 10:35:24 +0200 Subject: [PATCH] malformed payload error --- meilisearch-error/src/lib.rs | 2 ++ meilisearch-lib/src/document_formats.rs | 10 ++++++++++ meilisearch-lib/src/index_controller/updates/error.rs | 7 ++----- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/meilisearch-error/src/lib.rs b/meilisearch-error/src/lib.rs index bdb17ff22..1e71e1f78 100644 --- a/meilisearch-error/src/lib.rs +++ b/meilisearch-error/src/lib.rs @@ -85,6 +85,7 @@ pub enum Code { DumpProcessFailed, MissingContentType, + MalformedPayload, } impl Code { @@ -157,6 +158,7 @@ impl Code { ErrCode::internal("dump_process_failed", StatusCode::INTERNAL_SERVER_ERROR) } MissingContentType => ErrCode::invalid("missing_content_type", StatusCode::UNSUPPORTED_MEDIA_TYPE), + MalformedPayload => ErrCode::invalid("malformed_payload", StatusCode::BAD_REQUEST) } } diff --git a/meilisearch-lib/src/document_formats.rs b/meilisearch-lib/src/document_formats.rs index 0b0431e85..878d9465b 100644 --- a/meilisearch-lib/src/document_formats.rs +++ b/meilisearch-lib/src/document_formats.rs @@ -2,6 +2,7 @@ use std::fmt; use std::io::{self, Read, Result as IoResult, Seek, Write}; use csv::{Reader as CsvReader, StringRecordsIntoIter}; +use meilisearch_error::{Code, ErrorCode}; use milli::documents::DocumentBatchBuilder; use serde_json::{Deserializer, Map, Value}; @@ -35,6 +36,15 @@ pub enum DocumentFormatError { ), } +impl ErrorCode for DocumentFormatError { + fn error_code(&self) -> Code { + match self { + DocumentFormatError::Internal(_) => Code::Internal, + DocumentFormatError::MalformedPayload(_, _) => Code::MalformedPayload, + } + } +} + internal_error!(DocumentFormatError: milli::documents::Error, io::Error); macro_rules! malformed { diff --git a/meilisearch-lib/src/index_controller/updates/error.rs b/meilisearch-lib/src/index_controller/updates/error.rs index 4948ea164..31ebe84be 100644 --- a/meilisearch-lib/src/index_controller/updates/error.rs +++ b/meilisearch-lib/src/index_controller/updates/error.rs @@ -22,9 +22,7 @@ pub enum UpdateLoopError { )] FatalUpdateStoreError, #[error("{0}")] - InvalidPayload(#[from] DocumentFormatError), - #[error("{0}")] - MalformedPayload(Box), + DocumentFormatError(#[from] DocumentFormatError), // TODO: The reference to actix has to go. #[error("{0}")] PayloadError(#[from] actix_web::error::PayloadError), @@ -60,8 +58,7 @@ impl ErrorCode for UpdateLoopError { Self::Internal(_) => Code::Internal, //Self::IndexActor(e) => e.error_code(), Self::FatalUpdateStoreError => Code::Internal, - Self::InvalidPayload(_) => Code::BadRequest, - Self::MalformedPayload(_) => Code::BadRequest, + Self::DocumentFormatError(error) => error.error_code(), Self::PayloadError(error) => match error { actix_web::error::PayloadError::Overflow => Code::PayloadTooLarge, _ => Code::Internal,