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 all 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::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::<Result<_, _>>()?;

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
1 change: 1 addition & 0 deletions src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading