Skip to content

Commit c8e43ea

Browse files
authored
Update errors (#204)
1 parent 353a8e2 commit c8e43ea

File tree

4 files changed

+82
-78
lines changed

4 files changed

+82
-78
lines changed

src/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl Client {
179179
match self.delete_index(uid).await {
180180
Ok (_) => Ok(true),
181181
Err (Error::MeiliSearchError {
182-
message: _,
182+
error_message: _,
183183
error_code: ErrorCode::IndexNotFound,
184184
error_type: _,
185185
error_link: _,

src/errors.rs

Lines changed: 79 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
/// An enum representing the errors that can occur.
2+
23
#[derive(Debug)]
34
#[non_exhaustive]
45
pub enum Error {
6+
/// The exhaustive list of MeiliSearch errors: https://github.com/meilisearch/specifications/blob/main/text/0061-error-format-and-definitions.md
7+
/// Also check out: https://github.com/meilisearch/MeiliSearch/blob/main/meilisearch-error/src/lib.rs
58
MeiliSearchError {
69
/// The human readable error message
7-
message: String,
10+
error_message: String,
811
/// The error code of the error. Officially documented at
912
/// https://docs.meilisearch.com/errors.
1013
error_code: ErrorCode,
@@ -18,7 +21,7 @@ pub enum Error {
1821
/// There is no MeiliSearch server listening on the [specified host]
1922
/// (../client/struct.Client.html#method.new).
2023
UnreachableServer,
21-
/// The MeiliSearch server returned invalid JSON for a request.
24+
/// The MeiliSearch server returned an invalid JSON for a request.
2225
ParseError(serde_json::Error),
2326
/// This MeiliSearch SDK generated an invalid request (which was not sent).
2427
/// It probably comes from an invalid API key resulting in an invalid HTTP header.
@@ -50,64 +53,41 @@ pub enum ErrorType {
5053
#[derive(Debug, Clone)]
5154
#[non_exhaustive]
5255
pub enum ErrorCode {
53-
/// An error occurred while trying to create an index.
5456
IndexCreationFailed,
55-
/// An index with this UID already exists. You may want to use the
56-
/// [`get_or_create` method](../client/struct.Client.html#method.get_or_create).
5757
IndexAlreadyExists,
58-
/// No index was found with that UID. You may want to use the [get_or_create
59-
/// method](../client/struct.Client.html#method.get_or_create).
6058
IndexNotFound,
61-
/// There was an error in the provided index format. Index UIDs can only be
62-
/// composed of alphanumeric characters, hyphens (-), and underscores (_).
6359
InvalidIndexUid,
64-
/// An internal error occurred while trying to access the requested index.
65-
IndexNotAccessible,
66-
/// The database is in an invalid state. Deleting the database and
67-
/// re-indexing should solve the problem.
6860
InvalidState,
69-
/// MeiliSearch couldn't infer the primary key for the given documents.
70-
/// Consider specifying the key manually.
71-
MissingPrimaryKey,
72-
/// The index already has a set primary key which can't be changed.
73-
PrimaryKeyAlreadyPresent,
74-
/// A document was added with more than 65,535 fields.
61+
PrimaryKeyInferenceFailed,
62+
IndexPrimaryKeyAlreadyPresent,
63+
InvalidRankingRule,
64+
InvalidStoreFile,
7565
MaxFieldsLimitExceeded,
76-
/// A document is missing its primary key.
7766
MissingDocumentId,
78-
79-
/// The filter provided with the search was invalid.
67+
InvalidDocumentId,
8068
InvalidFilter,
81-
82-
/// The request is invalid, check the error message for more information.
69+
InvalidSort,
70+
BadParameter,
8371
BadRequest,
84-
/// The requested document can't be retrieved. Either it doesn't exist, or
85-
/// the database was left in an inconsistent state.
72+
DatabaseSizeLimitReached,
8673
DocumentNotFound,
87-
/// MeiliSearch experienced an internal error. Check the error message and
88-
/// open an issue if necessary.
8974
InternalError,
90-
/// The provided token is invalid.
91-
InvalidToken,
92-
/// The MeiliSearch instance is under maintenance.
93-
Maintenance,
94-
/// The requested resources are protected with an API key, which was not
95-
/// provided in the request header.
75+
InvalidGeoField,
76+
InvalidApiKey,
9677
MissingAuthorizationHeader,
97-
/// The requested resources could not be found.
98-
NotFound,
99-
/// The payload sent to the server was too large.
78+
TaskNotFound,
79+
DumpNotFound,
80+
NoSpaceLeftOnDevice,
10081
PayloadTooLarge,
101-
/// The document exists in store, but there was an error retrieving it. This
102-
/// is likely caused by an inconsistent state in the database.
10382
UnretrievableDocument,
104-
/// The payload content type is not supported by MeiliSearch. Currently,
105-
/// MeiliSearch only supports JSON payloads.
83+
SearchError,
10684
UnsupportedMediaType,
107-
/// A dump creation is already in progress and a new one can't be triggered until the previous dump creation is not finished.
108-
DumpAlreadyInProgress,
109-
/// An error occured during dump creation process, task aborted.
85+
DumpAlreadyProcessing,
11086
DumpProcessFailed,
87+
MissingContentType,
88+
MalformedPayload,
89+
InvalidContentType,
90+
MissingPayload,
11191

11292
/// That's unexpected. Please open a GitHub issue after ensuring you are
11393
/// using the supported version of the MeiliSearch server.
@@ -134,19 +114,19 @@ impl ErrorType {
134114
/// MeiliSearch.
135115
pub fn as_str(&self) -> &'static str {
136116
match self {
137-
ErrorType::InvalidRequest => "invalid_request_error",
138-
ErrorType::Internal => "internal_error",
139-
ErrorType::Authentication => "authentication_error",
117+
ErrorType::InvalidRequest => "invalid_request",
118+
ErrorType::Internal => "internal",
119+
ErrorType::Authentication => "authentication",
140120
}
141121
}
142122
/// Converts the error type string returned by MeiliSearch into an
143-
/// `ErrorType` enum. If the error type input is not recognized, None is
123+
/// `ErrorType` enum. If the error type input is not recognized, None is
144124
/// returned.
145125
pub fn parse(input: &str) -> Option<Self> {
146126
match input {
147-
"invalid_request_error" => Some(ErrorType::InvalidRequest),
148-
"internal_error" => Some(ErrorType::Internal),
149-
"authentication_error" => Some(ErrorType::Authentication),
127+
"invalid_request" => Some(ErrorType::InvalidRequest),
128+
"internal" => Some(ErrorType::Internal),
129+
"authentication" => Some(ErrorType::Authentication),
150130
_ => None,
151131
}
152132
}
@@ -161,57 +141,81 @@ impl ErrorCode {
161141
ErrorCode::IndexAlreadyExists => "index_already_exists",
162142
ErrorCode::IndexNotFound => "index_not_found",
163143
ErrorCode::InvalidIndexUid => "invalid_index_uid",
164-
ErrorCode::IndexNotAccessible => "index_not_accessible",
165144
ErrorCode::InvalidState => "invalid_state",
166-
ErrorCode::MissingPrimaryKey => "missing_primary_key",
167-
ErrorCode::PrimaryKeyAlreadyPresent => "primary_key_already_present",
145+
ErrorCode::PrimaryKeyInferenceFailed => "primary_key_inference_failed",
146+
ErrorCode::IndexPrimaryKeyAlreadyPresent => "index_primary_key_already_exists",
147+
ErrorCode::InvalidRankingRule => "invalid_ranking_rule",
148+
ErrorCode::InvalidStoreFile => "invalid_store_file",
168149
ErrorCode::MaxFieldsLimitExceeded => "max_field_limit_exceeded",
169150
ErrorCode::MissingDocumentId => "missing_document_id",
151+
ErrorCode::InvalidDocumentId => "invalid_document_id",
170152
ErrorCode::InvalidFilter => "invalid_filter",
153+
ErrorCode::InvalidSort => "invalid_sort",
154+
ErrorCode::BadParameter => "bad_parameter",
171155
ErrorCode::BadRequest => "bad_request",
156+
ErrorCode::DatabaseSizeLimitReached => "database_size_limit_reached",
172157
ErrorCode::DocumentNotFound => "document_not_found",
173158
ErrorCode::InternalError => "internal",
174-
ErrorCode::InvalidToken => "invalid_token",
175-
ErrorCode::Maintenance => "maintenance",
159+
ErrorCode::InvalidGeoField => "invalid_geo_field",
160+
ErrorCode::InvalidApiKey => "invalid_api_key",
176161
ErrorCode::MissingAuthorizationHeader => "missing_authorization_header",
177-
ErrorCode::NotFound => "not_found",
162+
ErrorCode::TaskNotFound => "task_not_found",
163+
ErrorCode::DumpNotFound => "dump_not_found",
164+
ErrorCode::NoSpaceLeftOnDevice => "no_space_left_on_device",
178165
ErrorCode::PayloadTooLarge => "payload_too_large",
179166
ErrorCode::UnretrievableDocument => "unretrievable_document",
167+
ErrorCode::SearchError => "search_error",
180168
ErrorCode::UnsupportedMediaType => "unsupported_media_type",
181-
ErrorCode::DumpAlreadyInProgress => "dump_already_in_progress",
169+
ErrorCode::DumpAlreadyProcessing => "dump_already_processing",
182170
ErrorCode::DumpProcessFailed => "dump_process_failed",
171+
ErrorCode::MissingContentType => "missing_content_type",
172+
ErrorCode::MalformedPayload => "malformed_payload",
173+
ErrorCode::InvalidContentType => "invalid_content_type",
174+
ErrorCode::MissingPayload => "missing_payload",
183175
// Other than this variant, all the other `&str`s are 'static
184176
ErrorCode::Unknown(inner) => &inner.0,
185177
}
186178
}
187179
/// Converts the error code string returned by MeiliSearch into an `ErrorCode`
188-
/// enum. If the error type input is not recognized, `ErrorCode::Unknown`
180+
/// enum. If the error type input is not recognized, `ErrorCode::Unknown`
189181
/// is returned.
190182
pub fn parse(input: &str) -> Self {
191183
match input {
192184
"index_creation_failed" => ErrorCode::IndexCreationFailed,
193185
"index_already_exists" => ErrorCode::IndexAlreadyExists,
194186
"index_not_found" => ErrorCode::IndexNotFound,
195187
"invalid_index_uid" => ErrorCode::InvalidIndexUid,
196-
"index_not_accessible" => ErrorCode::IndexNotAccessible,
197188
"invalid_state" => ErrorCode::InvalidState,
198-
"missing_primary_key" => ErrorCode::MissingPrimaryKey,
199-
"primary_key_already_present" => ErrorCode::PrimaryKeyAlreadyPresent,
189+
"primary_key_inference_failed" => ErrorCode::PrimaryKeyInferenceFailed,
190+
"index_primary_key_already_exists" => ErrorCode::IndexPrimaryKeyAlreadyPresent,
191+
"invalid_ranking_rule" => ErrorCode::InvalidRankingRule,
192+
"invalid_store_file" => ErrorCode::InvalidStoreFile,
200193
"max_field_limit_exceeded" => ErrorCode::MaxFieldsLimitExceeded,
201194
"missing_document_id" => ErrorCode::MissingDocumentId,
195+
"invalid_document_id" => ErrorCode::InvalidDocumentId,
202196
"invalid_filter" => ErrorCode::InvalidFilter,
197+
"invalid_sort" => ErrorCode::InvalidSort,
198+
"bad_parameter" => ErrorCode::BadParameter,
203199
"bad_request" => ErrorCode::BadRequest,
200+
"database_size_limit_reached" => ErrorCode::DatabaseSizeLimitReached,
204201
"document_not_found" => ErrorCode::DocumentNotFound,
205202
"internal" => ErrorCode::InternalError,
206-
"invalid_token" => ErrorCode::InvalidToken,
207-
"maintenance" => ErrorCode::Maintenance,
203+
"invalid_geo_field" => ErrorCode::InvalidGeoField,
204+
"invalid_api_key" => ErrorCode::InvalidApiKey,
208205
"missing_authorization_header" => ErrorCode::MissingAuthorizationHeader,
209-
"not_found" => ErrorCode::NotFound,
206+
"task_not_found" => ErrorCode::TaskNotFound,
207+
"dump_not_found" => ErrorCode::DumpNotFound,
208+
"no_space_left_on_device" => ErrorCode::NoSpaceLeftOnDevice,
210209
"payload_too_large" => ErrorCode::PayloadTooLarge,
211210
"unretrievable_document" => ErrorCode::UnretrievableDocument,
211+
"search_error" => ErrorCode::SearchError,
212212
"unsupported_media_type" => ErrorCode::UnsupportedMediaType,
213-
"dump_already_in_progress" => ErrorCode::DumpAlreadyInProgress,
213+
"dump_already_processing" => ErrorCode::DumpAlreadyProcessing,
214214
"dump_process_failed" => ErrorCode::DumpProcessFailed,
215+
"missing_content_type" => ErrorCode::MissingContentType,
216+
"malformed_payload" => ErrorCode::MalformedPayload,
217+
"invalid_content_type" => ErrorCode::InvalidContentType,
218+
"missing_payload" => ErrorCode::MissingPayload,
215219
inner => ErrorCode::Unknown(UnknownErrorCode(inner.to_string())),
216220
}
217221
}
@@ -230,7 +234,7 @@ impl std::fmt::Display for Error {
230234
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
231235
match self {
232236
Error::MeiliSearchError {
233-
message,
237+
error_message,
234238
error_code,
235239
error_type,
236240
error_link,
@@ -239,7 +243,7 @@ impl std::fmt::Display for Error {
239243
"Meilisearch {}: {}: {}. {}",
240244
error_type.as_str(),
241245
error_code,
242-
message,
246+
error_message,
243247
error_link,
244248
),
245249
Error::UnreachableServer => write!(fmt, "The MeiliSearch server can't be reached."),
@@ -255,37 +259,37 @@ impl std::error::Error for Error {}
255259
impl From<&serde_json::Value> for Error {
256260
fn from(json: &serde_json::Value) -> Error {
257261

258-
let message = json
262+
let error_message = json
259263
.get("message")
260264
.and_then(|v| v.as_str())
261265
.map(|s| s.to_string())
262266
.unwrap_or_else(|| json.to_string());
263267

264268
let error_link = json
265-
.get("errorLink")
269+
.get("link")
266270
.and_then(|v| v.as_str())
267271
.map(|s| s.to_string())
268272
.unwrap_or_else(String::new);
269273

270274
let error_type = json
271-
.get("errorType")
275+
.get("type")
272276
.and_then(|v| v.as_str())
273277
.and_then(|s| ErrorType::parse(s))
274278
.unwrap_or(ErrorType::Internal);
275279

276-
// If the response doesn't contain an errorType field, the error type
280+
// If the response doesn't contain a type field, the error type
277281
// is assumed to be an internal error.
278282

279283
let error_code = json
280-
.get("errorCode")
284+
.get("code")
281285
.and_then(|v| v.as_str())
282286
.map(|s| ErrorCode::parse(s))
283287
.unwrap_or_else(|| {
284-
ErrorCode::Unknown(UnknownErrorCode(String::from("missing errorCode")))
288+
ErrorCode::Unknown(UnknownErrorCode(String::from("missing error code")))
285289
});
286290

287291
Error::MeiliSearchError {
288-
message,
292+
error_message,
289293
error_code,
290294
error_type,
291295
error_link,

src/indexes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl Index {
111111
match self.delete().await {
112112
Ok (_) => Ok(true),
113113
Err (Error::MeiliSearchError {
114-
message: _,
114+
error_message: _,
115115
error_code: ErrorCode::IndexNotFound,
116116
error_type: _,
117117
error_link: _,

src/request.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ fn parse_response<Output: DeserializeOwned>(
143143
return Ok(output);
144144
}
145145
Err(e) => {
146-
error!("Request succeed but failed to parse response");
146+
error!("Request succeeded but failed to parse response");
147147
return Err(Error::ParseError(e));
148148
}
149149
};

0 commit comments

Comments
 (0)