From db458262320d7bc7d664d425fc7d1916d453b33c Mon Sep 17 00:00:00 2001 From: mpostma Date: Fri, 29 May 2020 16:25:39 +0200 Subject: [PATCH 1/4] take existing_index out of create_index error --- meilisearch-http/src/routes/index.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/meilisearch-http/src/routes/index.rs b/meilisearch-http/src/routes/index.rs index f12179902..25fd3f17f 100644 --- a/meilisearch-http/src/routes/index.rs +++ b/meilisearch-http/src/routes/index.rs @@ -178,7 +178,10 @@ async fn create_index( let created_index = data .db .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 name = body.name.as_ref().unwrap_or(&uid); From 68ad570cfc36de78d6e4c12ad02aef805164c28f Mon Sep 17 00:00:00 2001 From: mpostma Date: Fri, 29 May 2020 16:28:11 +0200 Subject: [PATCH 2/4] replace existing_index with index_already_exists --- meilisearch-error/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meilisearch-error/src/lib.rs b/meilisearch-error/src/lib.rs index 1c8fc4e78..c0dec6a3e 100644 --- a/meilisearch-error/src/lib.rs +++ b/meilisearch-error/src/lib.rs @@ -87,7 +87,7 @@ impl Code { match self { // index related errors 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), OpenIndex => ErrCode::internal("open_index", StatusCode::INTERNAL_SERVER_ERROR), From 3bd5a90976250085f001ef78d674ea149ed33ad9 Mon Sep 17 00:00:00 2001 From: mpostma Date: Fri, 29 May 2020 16:29:46 +0200 Subject: [PATCH 3/4] rename error types --- meilisearch-error/src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/meilisearch-error/src/lib.rs b/meilisearch-error/src/lib.rs index c0dec6a3e..d9537a9bc 100644 --- a/meilisearch-error/src/lib.rs +++ b/meilisearch-error/src/lib.rs @@ -28,8 +28,8 @@ pub trait ErrorCode: std::error::Error { enum ErrorType { InternalError, - InvalidRequest, - Authentication, + InvalidRequestError, + AuthenticationError, } impl fmt::Display for ErrorType { @@ -38,8 +38,8 @@ impl fmt::Display for ErrorType { match self { InternalError => write!(f, "internal_error"), - InvalidRequest => write!(f, "invalid_request"), - Authentication => write!(f, "authentication"), + InvalidRequestError => write!(f, "invalid_request_error"), + AuthenticationError => write!(f, "authentication_error"), } } } @@ -152,7 +152,7 @@ impl ErrCode { ErrCode { status_code, error_name, - error_type: ErrorType::Authentication, + error_type: ErrorType::AuthenticationError, } } @@ -168,7 +168,7 @@ impl ErrCode { ErrCode { status_code, error_name, - error_type: ErrorType::InvalidRequest, + error_type: ErrorType::InvalidRequestError, } } } From e95cec7ea67d3e641413cadc5c1817e474534080 Mon Sep 17 00:00:00 2001 From: mpostma Date: Mon, 1 Jun 2020 11:06:57 +0200 Subject: [PATCH 4/4] add test for error_code --- meilisearch-http/tests/index.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/meilisearch-http/tests/index.rs b/meilisearch-http/tests/index.rs index ed67f2d1d..be8193613 100644 --- a/meilisearch-http/tests/index.rs +++ b/meilisearch-http/tests/index.rs @@ -54,7 +54,7 @@ async fn create_index_with_uid() { "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!(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_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 let (res2_value, status_code) = server.list_indexes().await;