1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#[derive(Debug)]
pub enum Error {
UnreachableServer,
IndexAlreadyExist,
IndexNotFound,
InvalidIndexUid,
CantInferPrimaryKey,
ServerInMaintenance,
Unknown(String),
#[cfg(not(target_arch = "wasm32"))]
Http(reqwest::Error),
#[cfg(target_arch = "wasm32")]
Http(())
}
impl std::fmt::Display for Error {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
match self {
Error::UnreachableServer => write!(formatter, "Error::UnreachableServer: The MeiliSearch server can't be reached."),
Error::IndexAlreadyExist => write!(formatter, "Error::IndexAlreadyExist: The creation of an index failed because it already exists."),
Error::IndexNotFound => write!(formatter, "Error::IndexNotFound: The requested index does not exist."),
Error::InvalidIndexUid => write!(formatter, "Error::InvalidIndexUid: The requested UID is invalid. Index UID can only be composed of alphanumeric characters, hyphens (-), and underscores (_)."),
Error::CantInferPrimaryKey => write!(formatter, "Error::CantInferPrimaryKey: MeiliSearch was unable to infer the primary key of added documents."),
Error::Http(error) => write!(formatter, "Error::Http: The http request failed: {:?}.", error),
Error::ServerInMaintenance => write!(formatter, "Error::ServerInMaintenance: Server is in maintenance, please try again later."),
Error::Unknown(message) => write!(formatter, "Error::Unknown: An unknown error occured. Please open an issue (https://github.com/Mubelotix/meilisearch-sdk/issues). Message: {:?}", message),
}
}
}
impl std::error::Error for Error {}
impl From<&str> for Error {
fn from(message: &str) -> Error {
match message {
"{\"message\":\"Impossible to create index; index already exists\"}" => Error::IndexAlreadyExist,
"{\"message\":\"Index must have a valid uid; Index uid can be of type integer or string only composed of alphanumeric characters, hyphens (-) and underscores (_).\"}" => Error::InvalidIndexUid,
"{\"message\":\"Could not infer a primary key\"}" => Error::CantInferPrimaryKey,
m if m.starts_with("{\"message\":\"Server is in maintenance, please try again later\"") => Error::ServerInMaintenance,
m if m.starts_with("{\"message\":\"Index ") && m.ends_with(" not found\"}") => Error::IndexNotFound,
e => {
Error::Unknown(e.to_string())
},
}
}
}
#[cfg(not(target_arch = "wasm32"))]
impl From<reqwest::Error> for Error {
fn from(error: reqwest::Error) -> Error {
match error.status() {
None => {
Error::UnreachableServer
}
Some(_e) => Error::Http(error),
}
}
}