remove error response when deleting non existing doc (#1532)

* remove error response when deleting non existing doc

* handle other response when doc not found

* add test and refactor approach

* add response msg when doc not found

* add optional flag ignore_not_found

* change reponse msg
This commit is contained in:
Krunal Gandhi 2024-02-06 10:19:05 +00:00 committed by GitHub
parent 559b2de337
commit 562e152d9b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 2 deletions

View File

@ -1365,6 +1365,11 @@ bool get_fetch_document(const std::shared_ptr<http_req>& req, const std::shared_
bool del_remove_document(const std::shared_ptr<http_req>& req, const std::shared_ptr<http_res>& res) {
std::string doc_id = req->params["id"];
bool ignore_not_found = false;
if((req->params.count("ignore_not_found") != 0) && (req->params["ignore_not_found"] == "true")) {
ignore_not_found = true;
}
CollectionManager & collectionManager = CollectionManager::get_instance();
auto collection = collectionManager.get_collection(req->params["collection"]);
if(collection == nullptr) {
@ -1374,20 +1379,35 @@ bool del_remove_document(const std::shared_ptr<http_req>& req, const std::shared
Option<nlohmann::json> doc_option = collection->get(doc_id);
if(!doc_option.ok()) {
if (!doc_option.ok()) {
if (ignore_not_found && doc_option.code() == 404) {
nlohmann::json resp;
resp["id"] = doc_id;
res->set_200(resp.dump());
return true;
}
res->set(doc_option.code(), doc_option.error());
return false;
}
Option<std::string> deleted_id_op = collection->remove(doc_id);
if(!deleted_id_op.ok()) {
if (!deleted_id_op.ok()) {
if (ignore_not_found && doc_option.code() == 404) {
nlohmann::json resp;
resp["id"] = doc_id;
res->set_200(resp.dump());
return true;
}
res->set(deleted_id_op.code(), deleted_id_op.error());
return false;
}
nlohmann::json doc = doc_option.get();
res->set_200(doc.dump(-1, ' ', false, nlohmann::detail::error_handler_t::ignore));
return true;
}

View File

@ -1491,4 +1491,42 @@ TEST_F(CoreAPIUtilsTest, TestInvalidConversationModels) {
ASSERT_EQ(400, resp->status_code);
ASSERT_EQ("Property `model_name` is not provided or not a string.", nlohmann::json::parse(resp->body)["message"]);
}
TEST_F(CoreAPIUtilsTest, DeleteNonExistingDoc) {
Collection *coll1;
std::vector<field> fields = {field("title", field_types::STRING, false),
field("points", field_types::INT32, false),};
coll1 = collectionManager.get_collection("coll1").get();
if(coll1 == nullptr) {
coll1 = collectionManager.create_collection("coll1", 2, fields, "points").get();
}
for(size_t i=0; i<10; i++) {
nlohmann::json doc;
doc["id"] = std::to_string(i);
doc["title"] = "Title " + std::to_string(i);
doc["points"] = i;
coll1->add(doc.dump());
}
std::shared_ptr<http_req> req = std::make_shared<http_req>();
std::shared_ptr<http_res> res = std::make_shared<http_res>(nullptr);
req->params["collection"] = "coll1";
req->params["id"] = "9";
del_remove_document(req, res);
ASSERT_EQ(200, res->status_code);
req->params["id"] = "10";
del_remove_document(req, res);
ASSERT_EQ(404, res->status_code);
req->params["ignore_not_found"] = "true";
del_remove_document(req, res);
ASSERT_EQ(200, res->status_code);
}