From 767553519d4a5b48a76788e69aad09e139f7fd2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Renault?= Date: Thu, 4 Jul 2024 17:55:47 +0200 Subject: [PATCH] Create errors for the HTTP route issues --- meilisearch-types/src/deserr/mod.rs | 1 + meilisearch-types/src/error.rs | 2 ++ meilisearch/src/routes/indexes/documents.rs | 17 +++++++++++------ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/meilisearch-types/src/deserr/mod.rs b/meilisearch-types/src/deserr/mod.rs index 1c1b0e987..462b356ab 100644 --- a/meilisearch-types/src/deserr/mod.rs +++ b/meilisearch-types/src/deserr/mod.rs @@ -151,6 +151,7 @@ make_missing_field_convenience_builder!(MissingApiKeyExpiresAt, missing_api_key_ make_missing_field_convenience_builder!(MissingApiKeyIndexes, missing_api_key_indexes); make_missing_field_convenience_builder!(MissingSwapIndexes, missing_swap_indexes); make_missing_field_convenience_builder!(MissingDocumentFilter, missing_document_filter); +make_missing_field_convenience_builder!(MissingDocumentFilter, missing_document_edition_function); make_missing_field_convenience_builder!( MissingFacetSearchFacetName, missing_facet_search_facet_name diff --git a/meilisearch-types/src/error.rs b/meilisearch-types/src/error.rs index 0640c74b5..3e1316955 100644 --- a/meilisearch-types/src/error.rs +++ b/meilisearch-types/src/error.rs @@ -337,6 +337,8 @@ UnsupportedMediaType , InvalidRequest , UNSUPPORTED_MEDIA // Experimental features VectorEmbeddingError , InvalidRequest , BAD_REQUEST ; NotFoundSimilarId , InvalidRequest , BAD_REQUEST ; +InvalidDocumentEditionContext , InvalidRequest , BAD_REQUEST ; +InvalidDocumentEditionFunctionFilter , InvalidRequest , BAD_REQUEST ; EditDocumentsByFunctionError , InvalidRequest , BAD_REQUEST } diff --git a/meilisearch/src/routes/indexes/documents.rs b/meilisearch/src/routes/indexes/documents.rs index 7403e45cd..cf9158743 100644 --- a/meilisearch/src/routes/indexes/documents.rs +++ b/meilisearch/src/routes/indexes/documents.rs @@ -580,9 +580,9 @@ pub async fn delete_documents_by_filter( pub struct DocumentEditionByFunction { #[deserr(default, error = DeserrJsonError)] filter: Option, - #[deserr(default, error = DeserrJsonError)] + #[deserr(default, error = DeserrJsonError)] context: Option, - #[deserr(error = DeserrJsonError, missing_field_error = DeserrJsonError::missing_document_filter)] + #[deserr(error = DeserrJsonError, missing_field_error = DeserrJsonError::missing_document_edition_function)] function: String, } @@ -617,10 +617,15 @@ pub async fn edit_documents_by_function( let task = KindWithContent::DocumentEdition { index_uid, filter_expr: filter, - context: context.map(|v| match v { - serde_json::Value::Object(m) => m, - _ => panic!("The context must be an Object"), - }), + context: match context { + Some(Value::Object(m)) => Some(m), + _ => { + return Err(ResponseError::from_msg( + "The context must be an object".to_string(), + Code::InvalidDocumentEditionContext, + )) + } + }, function, };