From fb683fe88ba5e044b5681767fee0495ff125fe0f Mon Sep 17 00:00:00 2001 From: karribalu Date: Thu, 20 Jun 2024 23:55:09 +0100 Subject: [PATCH] Fix bad http status and error message on wrong payload --- meilisearch/src/error.rs | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/meilisearch/src/error.rs b/meilisearch/src/error.rs index 5a0b04020..84feea995 100644 --- a/meilisearch/src/error.rs +++ b/meilisearch/src/error.rs @@ -98,14 +98,27 @@ impl From for aweb::Error { impl From for MeilisearchHttpError { fn from(error: aweb::error::PayloadError) -> Self { - MeilisearchHttpError::Payload(PayloadError::Payload(error)) + match error { + aweb::error::PayloadError::Incomplete(_) => { + MeilisearchHttpError::Payload(PayloadError::Payload(ActixPayloadError::IncompleteError)) + } + _ => MeilisearchHttpError::Payload(PayloadError::Payload(ActixPayloadError::OtherError(error))) + } } } +#[derive(Debug, thiserror::Error)] +pub enum ActixPayloadError { + #[error("The provided payload is incomplete and cannot be decompressed")] + IncompleteError, + #[error(transparent)] + OtherError(aweb::error::PayloadError) +} + #[derive(Debug, thiserror::Error)] pub enum PayloadError { #[error(transparent)] - Payload(aweb::error::PayloadError), + Payload(ActixPayloadError), #[error(transparent)] Json(JsonPayloadError), #[error(transparent)] @@ -122,13 +135,15 @@ impl ErrorCode for PayloadError { fn error_code(&self) -> Code { match self { PayloadError::Payload(e) => match e { - aweb::error::PayloadError::Incomplete(_) => Code::Internal, - aweb::error::PayloadError::EncodingCorrupted => Code::Internal, - aweb::error::PayloadError::Overflow => Code::PayloadTooLarge, - aweb::error::PayloadError::UnknownLength => Code::Internal, - aweb::error::PayloadError::Http2Payload(_) => Code::Internal, - aweb::error::PayloadError::Io(_) => Code::Internal, - _ => todo!(), + ActixPayloadError::IncompleteError => Code::BadRequest, + ActixPayloadError::OtherError(error) => match error { + aweb::error::PayloadError::EncodingCorrupted => Code::Internal, + aweb::error::PayloadError::Overflow => Code::PayloadTooLarge, + aweb::error::PayloadError::UnknownLength => Code::Internal, + aweb::error::PayloadError::Http2Payload(_) => Code::Internal, + aweb::error::PayloadError::Io(_) => Code::Internal, + _ => todo!(), + } }, PayloadError::Json(err) => match err { JsonPayloadError::Overflow { .. } => Code::PayloadTooLarge,