Remove panic

This commit is contained in:
Mubelotix
2025-07-21 15:34:07 +02:00
parent a4eb83e6a1
commit 6825d917f4
3 changed files with 19 additions and 12 deletions

View File

@ -180,6 +180,8 @@ enum RenderError {
FieldsUnavailable, FieldsUnavailable,
FieldsAlreadyPresent, FieldsAlreadyPresent,
FieldsWithoutDocument, FieldsWithoutDocument,
CouldNotHandleInput,
} }
impl From<heed::Error> for RenderError { impl From<heed::Error> for RenderError {
@ -330,6 +332,10 @@ impl From<RenderError> for ResponseError {
String::from("Fields were requested but no document was provided.\n Hint: Provide a document ID or inline document."), String::from("Fields were requested but no document was provided.\n Hint: Provide a document ID or inline document."),
Code::InvalidRenderInputFields, 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<RenderResult, Render
if let Some(doc) = media.get("doc") { if let Some(doc) = media.get("doc") {
if insert_fields { if insert_fields {
let fields = get_inline_document_fields(&index, &rtxn, doc)?; let fields = get_inline_document_fields(&index, &rtxn, doc)?
.map_err(|_| CouldNotHandleInput)?;
object.insert("fields".into(), fields.to_value()); object.insert("fields".into(), fields.to_value());
} }
} }

View File

@ -150,14 +150,12 @@ pub struct JsonDocument {
} }
impl JsonDocument { impl JsonDocument {
pub fn new(value: &serde_json::Value) -> Self { pub fn new(value: &serde_json::Value) -> Result<Self, ()> {
let to_string = serde_json::to_string(&value) let to_string = serde_json::to_string(&value).map_err(|_| ())?;
.expect("JsonDocument should only be created with valid JSON"); // TODO: Remove panic let back_to_value: BTreeMap<String, Box<RawValue>> =
let back_to_value: BTreeMap<String, Box<RawValue>> = serde_json::from_str(&to_string) serde_json::from_str(&to_string).map_err(|_| ())?;
.expect("JsonDocument should only be created with valid JSON"); let object = liquid::to_object(&value).map_err(|_| ())?;
let object = Ok(Self { object, cached: back_to_value })
liquid::to_object(&value).expect("JsonDocument should only be created with valid JSON");
Self { object, cached: back_to_value }
} }
} }

View File

@ -173,12 +173,14 @@ pub fn get_inline_document_fields(
index: &Index, index: &Index,
rtxn: &RoTxn<'_>, rtxn: &RoTxn<'_>,
inline_doc: &serde_json::Value, inline_doc: &serde_json::Value,
) -> Result<LiquidValue, crate::Error> { ) -> Result<Result<LiquidValue, ()>, crate::Error> {
let fid_map_with_meta = index.fields_ids_map_with_metadata(rtxn)?; 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); let fields = OwnedFields::new(&inline_doc, &fid_map_with_meta);
Ok(fields.to_value()) Ok(Ok(fields.to_value()))
} }
pub fn get_document( pub fn get_document(