From 6825d917f4609e48aa98251a21645b398e210307 Mon Sep 17 00:00:00 2001 From: Mubelotix Date: Mon, 21 Jul 2025 15:34:07 +0200 Subject: [PATCH] Remove panic --- crates/meilisearch/src/routes/indexes/render.rs | 9 ++++++++- crates/milli/src/prompt/document.rs | 14 ++++++-------- crates/milli/src/prompt/mod.rs | 8 +++++--- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/crates/meilisearch/src/routes/indexes/render.rs b/crates/meilisearch/src/routes/indexes/render.rs index 0a763ad62..f7908fc83 100644 --- a/crates/meilisearch/src/routes/indexes/render.rs +++ b/crates/meilisearch/src/routes/indexes/render.rs @@ -180,6 +180,8 @@ enum RenderError { FieldsUnavailable, FieldsAlreadyPresent, FieldsWithoutDocument, + + CouldNotHandleInput, } impl From for RenderError { @@ -330,6 +332,10 @@ impl From for ResponseError { String::from("Fields were requested but no document was provided.\n Hint: Provide a document ID or inline document."), Code::InvalidRenderInputFields, ), + CouldNotHandleInput => ResponseError::from_msg( + String::from("Could not handle the input provided."), + Code::InvalidRenderInput, + ), } } } @@ -519,7 +525,8 @@ async fn render(index: Index, query: RenderQuery) -> Result Self { - let to_string = serde_json::to_string(&value) - .expect("JsonDocument should only be created with valid JSON"); // TODO: Remove panic - let back_to_value: BTreeMap> = serde_json::from_str(&to_string) - .expect("JsonDocument should only be created with valid JSON"); - let object = - liquid::to_object(&value).expect("JsonDocument should only be created with valid JSON"); - Self { object, cached: back_to_value } + pub fn new(value: &serde_json::Value) -> Result { + let to_string = serde_json::to_string(&value).map_err(|_| ())?; + let back_to_value: BTreeMap> = + serde_json::from_str(&to_string).map_err(|_| ())?; + let object = liquid::to_object(&value).map_err(|_| ())?; + Ok(Self { object, cached: back_to_value }) } } diff --git a/crates/milli/src/prompt/mod.rs b/crates/milli/src/prompt/mod.rs index aa73877bf..910d01251 100644 --- a/crates/milli/src/prompt/mod.rs +++ b/crates/milli/src/prompt/mod.rs @@ -173,12 +173,14 @@ pub fn get_inline_document_fields( index: &Index, rtxn: &RoTxn<'_>, inline_doc: &serde_json::Value, -) -> Result { +) -> Result, crate::Error> { let fid_map_with_meta = index.fields_ids_map_with_metadata(rtxn)?; - let inline_doc = JsonDocument::new(inline_doc); + let Ok(inline_doc) = JsonDocument::new(inline_doc) else { + return Ok(Err(())); + }; let fields = OwnedFields::new(&inline_doc, &fid_map_with_meta); - Ok(fields.to_value()) + Ok(Ok(fields.to_value())) } pub fn get_document(