Skip to content

Commit

Permalink
Merge #603
Browse files Browse the repository at this point in the history
603: Stops panicking when getting indexes r=irevoire a=postmeback

Sorry for the wrong heading. It actually enhances the request mentioned on [575](#575). 

Fixes: #575 

Requesting the maintainers to have a look at it.

Co-authored-by: postmeback <[email protected]>
Co-authored-by: Arka Poddar <[email protected]>
Co-authored-by: Clémentine <[email protected]>
  • Loading branch information
3 people authored Aug 1, 2024
2 parents 35f1bd5 + 503db65 commit 6c5f295
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
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

0 comments on commit 6c5f295

Please sign in to comment.