Skip to content

Commit 6c5f295

Browse files
meili-bors[bot]postmebackcurquiza
authored
Merge #603
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]>
2 parents 35f1bd5 + 503db65 commit 6c5f295

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

src/client.rs

+29-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use serde::de::Error as SerdeError;
12
use serde::{de::DeserializeOwned, Deserialize, Serialize};
23
use serde_json::{json, Value};
34
use std::{collections::HashMap, time::Duration};
@@ -79,16 +80,36 @@ impl<Http: HttpClient> Client<Http> {
7980
&self,
8081
value: &Value,
8182
) -> Result<IndexesResults<Http>, Error> {
82-
let raw_indexes = value["results"].as_array().unwrap();
83+
let raw_indexes = value["results"]
84+
.as_array()
85+
.ok_or_else(|| serde_json::Error::custom("Missing or invalid 'results' field"))
86+
.map_err(Error::ParseError)?;
87+
88+
let limit = value["limit"]
89+
.as_u64()
90+
.ok_or_else(|| serde_json::Error::custom("Missing or invalid 'limit' field"))
91+
.map_err(Error::ParseError)? as u32;
92+
93+
let offset = value["offset"]
94+
.as_u64()
95+
.ok_or_else(|| serde_json::Error::custom("Missing or invalid 'offset' field"))
96+
.map_err(Error::ParseError)? as u32;
97+
98+
let total = value["total"]
99+
.as_u64()
100+
.ok_or_else(|| serde_json::Error::custom("Missing or invalid 'total' field"))
101+
.map_err(Error::ParseError)? as u32;
102+
103+
let results = raw_indexes
104+
.iter()
105+
.map(|raw_index| Index::from_value(raw_index.clone(), self.clone()))
106+
.collect::<Result<_, _>>()?;
83107

84108
let indexes_results = IndexesResults {
85-
limit: value["limit"].as_u64().unwrap() as u32,
86-
offset: value["offset"].as_u64().unwrap() as u32,
87-
total: value["total"].as_u64().unwrap() as u32,
88-
results: raw_indexes
89-
.iter()
90-
.map(|raw_index| Index::from_value(raw_index.clone(), self.clone()))
91-
.collect::<Result<_, _>>()?,
109+
limit,
110+
offset,
111+
total,
112+
results,
92113
};
93114

94115
Ok(indexes_results)

src/errors.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ pub enum Error {
1717
/// The Meilisearch server returned an invalid JSON for a request.
1818
#[error("Error parsing response JSON: {}", .0)]
1919
ParseError(#[from] serde_json::Error),
20+
2021
/// A timeout happened while waiting for an update to complete.
2122
#[error("A task did not succeed in time.")]
2223
Timeout,

0 commit comments

Comments
 (0)