Raise errors instead of unwrapping in document (#106)

master
Chris Tam 2023-08-05 14:55:56 -04:00 committed by GitHub
parent 50809a186d
commit bbcd1f2aef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 2 deletions

View File

@ -427,10 +427,14 @@ impl Document {
/// Args: /// Args:
/// field_name (str): The field for which we are adding the bytes. /// field_name (str): The field for which we are adding the bytes.
/// value (str): The json object that will be added to the document. /// value (str): The json object that will be added to the document.
fn add_json(&mut self, field_name: String, json: &str) { ///
/// Raises a ValueError if the json is invalid.
fn add_json(&mut self, field_name: String, json: &str) -> PyResult<()> {
let json_object: serde_json::Value = let json_object: serde_json::Value =
serde_json::from_str(json).unwrap(); serde_json::from_str(json).map_err(to_pyerr)?;
self.add_value(field_name, json_object); self.add_value(field_name, json_object);
Ok(())
} }
/// Returns the number of added fields that have been added to the document /// Returns the number of added fields that have been added to the document