Skip to content

Commit d95baea

Browse files
authored
Improve error handling and move from MeiliSearch 0.11 to MeiliSearch 0.12 errors. (#36)
* Implement error code handling for Meilisearch 0.12 * Add additional errors, refactor error type * Extend MeiliSearch 0.12 error handling, remove support for 0.11 * Add documentation and make some formatting changes * Move ErrorCode::Unknown inner value to opaque type This ensures that adding new error types will not be a breaking change, as unknown error codes cannot be matched on. It is still possible to retrieve the inner value of the unknown error code by using the `Display` implementation or the `as_str` method, but those are much less likely to be used and should not be expected to be stable.
1 parent 44ac655 commit d95baea

File tree

4 files changed

+332
-69
lines changed

4 files changed

+332
-69
lines changed

src/client.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -182,16 +182,18 @@ impl<'a> Client<'a> {
182182
/// # Example
183183
///
184184
/// ```
185-
/// # use meilisearch_sdk::{client::*, indexes::*, errors::Error};
185+
/// # use meilisearch_sdk::{client::*, indexes::*, errors::{Error, ErrorCode}};
186186
/// #
187187
/// # #[tokio::main]
188188
/// # async fn main() {
189189
/// let client = Client::new("http://localhost:7700", "masterKey");
190190
///
191191
/// match client.get_health().await {
192-
/// Ok(()) => println!("server is operationnal"),
193-
/// Err(Error::ServerInMaintenance) => eprintln!("server is in maintenance"),
194-
/// _ => panic!("should never happen"),
192+
/// Ok(()) => println!("server is operational"),
193+
/// Err(Error::MeiliSearchError { error_code: ErrorCode::Maintenance, .. }) => {
194+
/// eprintln!("server is in maintenance")
195+
/// },
196+
/// Err(e) => panic!("should never happen: {}", e),
195197
/// }
196198
/// # }
197199
/// ```
@@ -201,10 +203,13 @@ impl<'a> Client<'a> {
201203
self.apikey,
202204
Method::Get,
203205
204,
204-
).await;
206+
)
207+
.await;
205208
match r {
206-
Err(Error::Unknown(m)) if &m == "null" => Ok(()),
207-
e => e
209+
// This shouldn't be an error; The status code is 200, but the request
210+
// function only supports one successful error code for some reason
211+
Err(Error::Empty) => Ok(()),
212+
e => e,
208213
}
209214
}
210215

@@ -234,10 +239,13 @@ impl<'a> Client<'a> {
234239
self.apikey,
235240
Method::Put(HealthBody { health }),
236241
204,
237-
).await;
242+
)
243+
.await;
238244
match r {
239-
Err(Error::Unknown(m)) if &m == "null" => Ok(()),
240-
e => e
245+
// This shouldn't be an error; The status code is 200, but the request
246+
// function only supports one successful error code for some reason
247+
Err(Error::Empty) => Ok(()),
248+
e => e,
241249
}
242250
}
243251

0 commit comments

Comments
 (0)