Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stops panicking when getting indexes #603

Merged
merged 5 commits into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/client.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -79,16 +80,36 @@ impl<Http: HttpClient> Client<Http> {
&self,
value: &Value,
) -> Result<IndexesResults<Http>, 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::SerdeParseError)?;

let limit = value["limit"]
.as_u64()
.ok_or_else(|| serde_json::Error::custom("Missing or invalid 'limit' field"))
.map_err(Error::SerdeParseError)? as u32;

let offset = value["offset"]
.as_u64()
.ok_or_else(|| serde_json::Error::custom("Missing or invalid 'offset' field"))
.map_err(Error::SerdeParseError)? as u32;

let total = value["total"]
.as_u64()
.ok_or_else(|| serde_json::Error::custom("Missing or invalid 'total' field"))
.map_err(Error::SerdeParseError)? as u32;

let results = raw_indexes
.iter()
.map(|raw_index| Index::from_value(raw_index.clone(), self.clone()))
.collect::<Result<_, _>>()?;
irevoire marked this conversation as resolved.
Show resolved Hide resolved

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::<Result<_, _>>()?,
limit,
offset,
total,
results,
};

Ok(indexes_results)
Expand Down
7 changes: 4 additions & 3 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ pub enum Error {
#[error(transparent)]
MeilisearchCommunication(#[from] MeilisearchCommunicationError),
/// The Meilisearch server returned an invalid JSON for a request.
#[error("Error parsing response JSON: {}", .0)]
ParseError(#[from] serde_json::Error),
#[error(transparent)]
SerdeParseError(#[from] serde_json::Error),
postmeback marked this conversation as resolved.
Show resolved Hide resolved

/// A timeout happened while waiting for an update to complete.
#[error("A task did not succeed in time.")]
Timeout,
Expand Down Expand Up @@ -385,7 +386,7 @@ mod test {
"age": 43,
}"#;

let error = Error::ParseError(serde_json::from_str::<String>(data).unwrap_err());
let error = Error::SerdeParseError(serde_json::from_str::<String>(data).unwrap_err());
assert_eq!(
error.to_string(),
"Error parsing response JSON: invalid type: map, expected a string at line 2 column 8"
Expand Down
3 changes: 2 additions & 1 deletion src/indexes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ impl<Http: HttpClient> Index<Http> {
primaryKey: Option<String>,
}

let i: IndexFromSerde = serde_json::from_value(raw_index).map_err(Error::ParseError)?;
let i: IndexFromSerde =
serde_json::from_value(raw_index).map_err(Error::SerdeParseError)?;

Ok(Index {
uid: i.uid,
Expand Down
4 changes: 2 additions & 2 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub fn parse_response<Output: DeserializeOwned>(
}
Err(e) => {
error!("Request succeeded but failed to parse response");
Err(Error::ParseError(e))
Err(Error::SerdeParseError(e))
}
};
}
Expand All @@ -139,7 +139,7 @@ pub fn parse_response<Output: DeserializeOwned>(
},
));
}
Err(Error::ParseError(e))
Err(Error::SerdeParseError(e))
}
}
}
Expand Down
Loading