From e706628bb1f02d651f9c99e2a5851df13ffa2eed Mon Sep 17 00:00:00 2001 From: Tamo Date: Thu, 5 Jan 2023 23:21:44 +0100 Subject: [PATCH] fix the error code of the swap index route --- index-scheduler/src/batch.rs | 4 ++-- index-scheduler/src/error.rs | 20 +++++++++++-------- .../first_swap_failed.snap | 2 +- meilisearch-types/src/error.rs | 7 +++++++ meilisearch/tests/tasks/mod.rs | 4 ++-- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/index-scheduler/src/batch.rs b/index-scheduler/src/batch.rs index 78f1e1fbe..423e2f23d 100644 --- a/index-scheduler/src/batch.rs +++ b/index-scheduler/src/batch.rs @@ -882,11 +882,11 @@ impl IndexScheduler { } if !not_found_indexes.is_empty() { if not_found_indexes.len() == 1 { - return Err(Error::IndexNotFound( + return Err(Error::SwapIndexNotFound( not_found_indexes.into_iter().next().unwrap().clone(), )); } else { - return Err(Error::IndexesNotFound( + return Err(Error::SwapIndexesNotFound( not_found_indexes.into_iter().cloned().collect(), )); } diff --git a/index-scheduler/src/error.rs b/index-scheduler/src/error.rs index b7f72210d..0be6e4e32 100644 --- a/index-scheduler/src/error.rs +++ b/index-scheduler/src/error.rs @@ -48,11 +48,6 @@ impl From for Code { pub enum Error { #[error("Index `{0}` not found.")] IndexNotFound(String), - #[error( - "Indexes {} not found.", - .0.iter().map(|s| format!("`{}`", s)).collect::>().join(", ") - )] - IndexesNotFound(Vec), #[error("Index `{0}` already exists.")] IndexAlreadyExists(String), #[error( @@ -64,6 +59,13 @@ pub enum Error { .0.iter().map(|s| format!("`{}`", s)).collect::>().join(", ") )] SwapDuplicateIndexesFound(Vec), + #[error("Index `{0}` not found.")] + SwapIndexNotFound(String), + #[error( + "Indexes {} not found.", + .0.iter().map(|s| format!("`{}`", s)).collect::>().join(", ") + )] + SwapIndexesNotFound(Vec), #[error("Corrupted dump.")] CorruptedDump, #[error( @@ -136,10 +138,11 @@ impl ErrorCode for Error { fn error_code(&self) -> Code { match self { Error::IndexNotFound(_) => Code::IndexNotFound, - Error::IndexesNotFound(_) => Code::IndexNotFound, Error::IndexAlreadyExists(_) => Code::IndexAlreadyExists, - Error::SwapDuplicateIndexesFound(_) => Code::DuplicateIndexFound, - Error::SwapDuplicateIndexFound(_) => Code::DuplicateIndexFound, + Error::SwapDuplicateIndexesFound(_) => Code::InvalidDuplicateIndexesFound, + Error::SwapDuplicateIndexFound(_) => Code::InvalidDuplicateIndexesFound, + Error::SwapIndexNotFound(_) => Code::InvalidSwapIndexes, + Error::SwapIndexesNotFound(_) => Code::InvalidSwapIndexes, Error::InvalidTaskDate { field, .. } => (*field).into(), Error::InvalidTaskUids { .. } => Code::InvalidTaskUids, Error::InvalidTaskStatuses { .. } => Code::InvalidTaskStatuses, @@ -157,6 +160,7 @@ impl ErrorCode for Error { Error::FileStore(e) => e.error_code(), Error::IoError(e) => e.error_code(), Error::Persist(e) => e.error_code(), + // Irrecoverable errors Error::Anyhow(_) => Code::Internal, Error::CorruptedTaskQueue => Code::Internal, diff --git a/index-scheduler/src/snapshots/lib.rs/swap_indexes_errors/first_swap_failed.snap b/index-scheduler/src/snapshots/lib.rs/swap_indexes_errors/first_swap_failed.snap index 3e95c5b25..1a47f87fb 100644 --- a/index-scheduler/src/snapshots/lib.rs/swap_indexes_errors/first_swap_failed.snap +++ b/index-scheduler/src/snapshots/lib.rs/swap_indexes_errors/first_swap_failed.snap @@ -10,7 +10,7 @@ source: index-scheduler/src/lib.rs 1 {uid: 1, status: succeeded, details: { primary_key: Some("id") }, kind: IndexCreation { index_uid: "b", primary_key: Some("id") }} 2 {uid: 2, status: succeeded, details: { primary_key: Some("id") }, kind: IndexCreation { index_uid: "c", primary_key: Some("id") }} 3 {uid: 3, status: succeeded, details: { primary_key: Some("id") }, kind: IndexCreation { index_uid: "d", primary_key: Some("id") }} -4 {uid: 4, status: failed, error: ResponseError { code: 200, message: "Indexes `e`, `f` not found.", error_code: "index_not_found", error_type: "invalid_request", error_link: "https://docs.meilisearch.com/errors#index-not-found" }, details: { swaps: [IndexSwap { indexes: ("a", "b") }, IndexSwap { indexes: ("c", "e") }, IndexSwap { indexes: ("d", "f") }] }, kind: IndexSwap { swaps: [IndexSwap { indexes: ("a", "b") }, IndexSwap { indexes: ("c", "e") }, IndexSwap { indexes: ("d", "f") }] }} +4 {uid: 4, status: failed, error: ResponseError { code: 200, message: "Indexes `e`, `f` not found.", error_code: "invalid_swap_indexes", error_type: "invalid_request", error_link: "https://docs.meilisearch.com/errors#invalid-swap-indexes" }, details: { swaps: [IndexSwap { indexes: ("a", "b") }, IndexSwap { indexes: ("c", "e") }, IndexSwap { indexes: ("d", "f") }] }, kind: IndexSwap { swaps: [IndexSwap { indexes: ("a", "b") }, IndexSwap { indexes: ("c", "e") }, IndexSwap { indexes: ("d", "f") }] }} ---------------------------------------------------------------------- ### Status: enqueued [] diff --git a/meilisearch-types/src/error.rs b/meilisearch-types/src/error.rs index def4f388e..4afb45627 100644 --- a/meilisearch-types/src/error.rs +++ b/meilisearch-types/src/error.rs @@ -146,6 +146,7 @@ pub enum Code { // Invalid swap-indexes InvalidSwapIndexes, + InvalidDuplicateIndexesFound, // Invalid settings update request InvalidSettingsDisplayedAttributes, @@ -380,8 +381,13 @@ impl Code { ErrCode::invalid("duplicate_index_found", StatusCode::BAD_REQUEST) } + // Swap indexes error InvalidSwapIndexes => ErrCode::invalid("invalid_swap_indexes", StatusCode::BAD_REQUEST), + InvalidDuplicateIndexesFound => { + ErrCode::invalid("invalid_swap_duplicate_index_found", StatusCode::BAD_REQUEST) + } + // Invalid settings InvalidSettingsDisplayedAttributes => { ErrCode::invalid("invalid_settings_displayed_attributes", StatusCode::BAD_REQUEST) } @@ -416,6 +422,7 @@ impl Code { ErrCode::invalid("invalid_settings_pagination", StatusCode::BAD_REQUEST) } + // Invalid search InvalidSearchQ => ErrCode::invalid("invalid_search_q", StatusCode::BAD_REQUEST), InvalidSearchOffset => { ErrCode::invalid("invalid_search_offset", StatusCode::BAD_REQUEST) diff --git a/meilisearch/tests/tasks/mod.rs b/meilisearch/tests/tasks/mod.rs index caef60514..4505d7fe5 100644 --- a/meilisearch/tests/tasks/mod.rs +++ b/meilisearch/tests/tasks/mod.rs @@ -862,9 +862,9 @@ async fn test_summarized_index_swap() { }, "error": { "message": "Indexes `cattos`, `doggos` not found.", - "code": "index_not_found", + "code": "invalid_swap_indexes", "type": "invalid_request", - "link": "https://docs.meilisearch.com/errors#index-not-found" + "link": "https://docs.meilisearch.com/errors#invalid-swap-indexes" }, "duration": "[duration]", "enqueuedAt": "[date]",