Merge pull request #734 from MarinPostma/index-already-exist-code

Index already exist code
This commit is contained in:
Clément Renault 2020-06-01 11:43:29 +02:00 committed by GitHub
commit 2ae05d9fd1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 9 deletions

View File

@ -28,8 +28,8 @@ pub trait ErrorCode: std::error::Error {
enum ErrorType { enum ErrorType {
InternalError, InternalError,
InvalidRequest, InvalidRequestError,
Authentication, AuthenticationError,
} }
impl fmt::Display for ErrorType { impl fmt::Display for ErrorType {
@ -38,8 +38,8 @@ impl fmt::Display for ErrorType {
match self { match self {
InternalError => write!(f, "internal_error"), InternalError => write!(f, "internal_error"),
InvalidRequest => write!(f, "invalid_request"), InvalidRequestError => write!(f, "invalid_request_error"),
Authentication => write!(f, "authentication"), AuthenticationError => write!(f, "authentication_error"),
} }
} }
} }
@ -87,7 +87,7 @@ impl Code {
match self { match self {
// index related errors // index related errors
CreateIndex => ErrCode::invalid("create_index", StatusCode::BAD_REQUEST), CreateIndex => ErrCode::invalid("create_index", StatusCode::BAD_REQUEST),
IndexAlreadyExists => ErrCode::invalid("existing_index", StatusCode::BAD_REQUEST), IndexAlreadyExists => ErrCode::invalid("index_already_exists", StatusCode::BAD_REQUEST),
IndexNotFound => ErrCode::invalid("index_not_found", StatusCode::NOT_FOUND), InvalidIndexUid => ErrCode::invalid("invalid_index_uid", StatusCode::BAD_REQUEST), IndexNotFound => ErrCode::invalid("index_not_found", StatusCode::NOT_FOUND), InvalidIndexUid => ErrCode::invalid("invalid_index_uid", StatusCode::BAD_REQUEST),
OpenIndex => ErrCode::internal("open_index", StatusCode::INTERNAL_SERVER_ERROR), OpenIndex => ErrCode::internal("open_index", StatusCode::INTERNAL_SERVER_ERROR),
@ -152,7 +152,7 @@ impl ErrCode {
ErrCode { ErrCode {
status_code, status_code,
error_name, error_name,
error_type: ErrorType::Authentication, error_type: ErrorType::AuthenticationError,
} }
} }
@ -168,7 +168,7 @@ impl ErrCode {
ErrCode { ErrCode {
status_code, status_code,
error_name, error_name,
error_type: ErrorType::InvalidRequest, error_type: ErrorType::InvalidRequestError,
} }
} }
} }

View File

@ -178,7 +178,10 @@ async fn create_index(
let created_index = data let created_index = data
.db .db
.create_index(&uid) .create_index(&uid)
.map_err(Error::create_index)?; .map_err(|e| match e {
meilisearch_core::Error::IndexAlreadyExists => e.into(),
_ => ResponseError::from(Error::create_index(e))
})?;
let index_response = data.db.main_write::<_, _, ResponseError>(|mut writer| { let index_response = data.db.main_write::<_, _, ResponseError>(|mut writer| {
let name = body.name.as_ref().unwrap_or(&uid); let name = body.name.as_ref().unwrap_or(&uid);

View File

@ -54,7 +54,7 @@ async fn create_index_with_uid() {
"uid": "movies", "uid": "movies",
}); });
let (res1_value, status_code) = server.create_index(body).await; let (res1_value, status_code) = server.create_index(body.clone()).await;
assert_eq!(status_code, 201); assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5); assert_eq!(res1_value.as_object().unwrap().len(), 5);
@ -67,6 +67,13 @@ async fn create_index_with_uid() {
assert!(r1_created_at.len() > 1); assert!(r1_created_at.len() > 1);
assert!(r1_updated_at.len() > 1); assert!(r1_updated_at.len() > 1);
// 1.5 verify that error is thrown when trying to create the same index
let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 400);
assert_eq!(response["errorCode"].as_str().unwrap(), "index_already_exists");
// 2 - Check the list of indexes // 2 - Check the list of indexes
let (res2_value, status_code) = server.list_indexes().await; let (res2_value, status_code) = server.list_indexes().await;