diff --git a/src/client.rs b/src/client.rs index 247954c2..c7d2ebea 100644 --- a/src/client.rs +++ b/src/client.rs @@ -1,3 +1,4 @@ +use serde::de::Error as SerdeError; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde_json::{json, Value}; use std::{collections::HashMap, time::Duration}; @@ -79,16 +80,36 @@ impl Client { &self, value: &Value, ) -> Result, Error> { - let raw_indexes = value["results"].as_array().unwrap(); + let raw_indexes = value["results"] + .as_array() + .ok_or_else(|| serde_json::Error::custom("Missing or invalid 'results' field")) + .map_err(Error::ParseError)?; + + let limit = value["limit"] + .as_u64() + .ok_or_else(|| serde_json::Error::custom("Missing or invalid 'limit' field")) + .map_err(Error::ParseError)? as u32; + + let offset = value["offset"] + .as_u64() + .ok_or_else(|| serde_json::Error::custom("Missing or invalid 'offset' field")) + .map_err(Error::ParseError)? as u32; + + let total = value["total"] + .as_u64() + .ok_or_else(|| serde_json::Error::custom("Missing or invalid 'total' field")) + .map_err(Error::ParseError)? as u32; + + let results = raw_indexes + .iter() + .map(|raw_index| Index::from_value(raw_index.clone(), self.clone())) + .collect::>()?; let indexes_results = IndexesResults { - limit: value["limit"].as_u64().unwrap() as u32, - offset: value["offset"].as_u64().unwrap() as u32, - total: value["total"].as_u64().unwrap() as u32, - results: raw_indexes - .iter() - .map(|raw_index| Index::from_value(raw_index.clone(), self.clone())) - .collect::>()?, + limit, + offset, + total, + results, }; Ok(indexes_results) diff --git a/src/errors.rs b/src/errors.rs index 7faa5b9a..9e9c7eae 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -17,6 +17,7 @@ pub enum Error { /// The Meilisearch server returned an invalid JSON for a request. #[error("Error parsing response JSON: {}", .0)] ParseError(#[from] serde_json::Error), + /// A timeout happened while waiting for an update to complete. #[error("A task did not succeed in time.")] Timeout,