From 169749396b2ad288e5df95c0432fbfdb2fb8dfd9 Mon Sep 17 00:00:00 2001 From: mpostma Date: Mon, 22 Jun 2020 16:09:57 +0200 Subject: [PATCH 1/3] update error types to be more accurate --- meilisearch-error/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/meilisearch-error/src/lib.rs b/meilisearch-error/src/lib.rs index 12cdc5930..20a2c734b 100644 --- a/meilisearch-error/src/lib.rs +++ b/meilisearch-error/src/lib.rs @@ -87,10 +87,11 @@ impl Code { match self { // index related errors // create index is thrown on internal error while creating an index. - CreateIndex => ErrCode::invalid("index_creation_failed", StatusCode::BAD_REQUEST), + CreateIndex => ErrCode::internal("index_creation_failed", StatusCode::BAD_REQUEST), IndexAlreadyExists => ErrCode::invalid("index_already_exists", StatusCode::BAD_REQUEST), // thrown when requesting an unexisting index - 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("index_not_accessible", StatusCode::INTERNAL_SERVER_ERROR), // invalid state error From ac63f1cd7a0c4abd3914b85467339e5d9624435b Mon Sep 17 00:00:00 2001 From: mpostma Date: Wed, 24 Jun 2020 11:33:16 +0200 Subject: [PATCH 2/3] fix typo in error code --- 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 20a2c734b..4967aba3a 100644 --- a/meilisearch-error/src/lib.rs +++ b/meilisearch-error/src/lib.rs @@ -102,7 +102,7 @@ impl Code { PrimaryKeyAlreadyPresent => ErrCode::invalid("primary_key_already_present", StatusCode::BAD_REQUEST), // invalid document - MaxFieldsLimitExceeded => ErrCode::invalid("max_field_limit_exceeded", StatusCode::BAD_REQUEST), + MaxFieldsLimitExceeded => ErrCode::invalid("max_fields_limit_exceeded", StatusCode::BAD_REQUEST), MissingDocumentId => ErrCode::invalid("missing_document_id", StatusCode::BAD_REQUEST), // error related to facets From 99e8d4adae69569d20a9c6b21f1774cf98c526d1 Mon Sep 17 00:00:00 2001 From: mpostma Date: Mon, 6 Jul 2020 09:56:10 +0200 Subject: [PATCH 3/3] fix missing primary key --- meilisearch-core/src/error.rs | 2 +- meilisearch-error/src/lib.rs | 2 +- meilisearch-http/src/routes/document.rs | 4 ++-- meilisearch-http/tests/errors.rs | 16 +++++++++++++++- meilisearch-http/tests/index.rs | 3 +-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/meilisearch-core/src/error.rs b/meilisearch-core/src/error.rs index 0540ca8d0..477063bac 100644 --- a/meilisearch-core/src/error.rs +++ b/meilisearch-core/src/error.rs @@ -41,7 +41,7 @@ impl ErrorCode for Error { FacetError(_) => Code::Facet, FilterParseError(_) => Code::Filter, IndexAlreadyExists => Code::IndexAlreadyExists, - MissingPrimaryKey => Code::InvalidState, + MissingPrimaryKey => Code::MissingPrimaryKey, MissingDocumentId => Code::MissingDocumentId, MaxFieldsLimitExceeded => Code::MaxFieldsLimitExceeded, Schema(s) => s.error_code(), diff --git a/meilisearch-error/src/lib.rs b/meilisearch-error/src/lib.rs index 4967aba3a..850076b7b 100644 --- a/meilisearch-error/src/lib.rs +++ b/meilisearch-error/src/lib.rs @@ -97,7 +97,7 @@ impl Code { // invalid state error InvalidState => ErrCode::internal("invalid_state", StatusCode::INTERNAL_SERVER_ERROR), // thrown when no primary key has been set - MissingPrimaryKey => ErrCode::internal("missing_primary_key", StatusCode::INTERNAL_SERVER_ERROR), + MissingPrimaryKey => ErrCode::invalid("missing_primary_key", StatusCode::BAD_REQUEST), // error thrown when trying to set an already existing primary key PrimaryKeyAlreadyPresent => ErrCode::invalid("primary_key_already_present", StatusCode::BAD_REQUEST), diff --git a/meilisearch-http/src/routes/document.rs b/meilisearch-http/src/routes/document.rs index e7ad3801b..b646d5678 100644 --- a/meilisearch-http/src/routes/document.rs +++ b/meilisearch-http/src/routes/document.rs @@ -156,7 +156,7 @@ async fn update_multiple_documents( let mut schema = index .main .schema(&reader)? - .ok_or(Error::internal("Impossible to retrieve the schema"))?; + .ok_or(meilisearch_core::Error::SchemaMissing)?; if schema.primary_key().is_none() { let id = match ¶ms.primary_key { @@ -164,7 +164,7 @@ async fn update_multiple_documents( None => body .first() .and_then(find_primary_key) - .ok_or(Error::bad_request("Could not infer a primary key"))?, + .ok_or(meilisearch_core::Error::MissingPrimaryKey)? }; schema diff --git a/meilisearch-http/tests/errors.rs b/meilisearch-http/tests/errors.rs index ff0fb2c73..89591f642 100644 --- a/meilisearch-http/tests/errors.rs +++ b/meilisearch-http/tests/errors.rs @@ -92,7 +92,7 @@ async fn max_field_limit_exceeded_error() { } let docs = json!([doc]); assert_error_async!( - "max_field_limit_exceeded", + "max_fields_limit_exceeded", "invalid_request_error", server, server.add_or_replace_multiple_documents_sync(docs).await); @@ -180,3 +180,17 @@ async fn payload_too_large_error() { StatusCode::PAYLOAD_TOO_LARGE, server.create_index(json!(bigvec)).await); } + +#[actix_rt::test] +async fn missing_primary_key_error() { + let mut server = common::Server::with_uid("test"); + server.create_index(json!({"uid": "test"})).await; + let document = json!([{ + "content": "test" + }]); + assert_error!( + "missing_primary_key", + "invalid_request_error", + StatusCode::BAD_REQUEST, + server.add_or_replace_multiple_documents_sync(document).await); +} diff --git a/meilisearch-http/tests/index.rs b/meilisearch-http/tests/index.rs index 965459282..1f7e40725 100644 --- a/meilisearch-http/tests/index.rs +++ b/meilisearch-http/tests/index.rs @@ -658,9 +658,8 @@ async fn check_add_documents_without_primary_key() { let (response, status_code) = server.add_or_replace_multiple_documents_sync(body).await; - let message = response["message"].as_str().unwrap(); assert_eq!(response.as_object().unwrap().len(), 4); - assert_eq!(message, "Could not infer a primary key"); + assert_eq!(response["errorCode"], "missing_primary_key"); assert_eq!(status_code, 400); }