Skip to content

Commit

Permalink
CXXCBC-409: Add handling for 'index does not exist' query error
Browse files Browse the repository at this point in the history
  • Loading branch information
DemetrisChr authored and avsej committed Dec 19, 2023
1 parent ca6969b commit faddc7e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
18 changes: 17 additions & 1 deletion core/operations/document_query.cxx
Expand Up @@ -26,6 +26,7 @@
#include <couchbase/error_codes.hxx>

#include <gsl/assert>
#include <regex>

namespace couchbase::core::operations
{
Expand Down Expand Up @@ -236,7 +237,9 @@ query_request::make_response(error_context::query&& ctx, const encoded_response_
response.ctx.client_context_id);
}
}
response.meta.status = payload.at("status").get_string();
if (const auto* s = payload.find("status"); s != nullptr) {
response.meta.status = payload.at("status").get_string();
}
if (const auto* s = payload.find("signature"); s != nullptr) {
response.meta.signature = couchbase::core::utils::json::generate(*s);
}
Expand Down Expand Up @@ -342,6 +345,19 @@ query_request::make_response(error_context::query&& ctx, const encoded_response_
case 4090: /* IKey: "plan.build_prepared.name_not_in_encoded_plan" */
response.ctx.ec = errc::query::prepared_statement_failure;
break;
case 4300: /* IKey: "plan.new_index_already_exists" */
response.ctx.ec = errc::common::index_exists;
break;
case 5000: /* IKey: "Internal Error" */
if (std::regex_search(response.ctx.first_error_message, std::regex{ ".*[iI]ndex .*already exist.*" })) {
response.ctx.ec = errc::common::index_exists;
} else if (response.ctx.first_error_message.find("Index does not exist") != std::string::npos ||
std::regex_search(response.ctx.first_error_message, std::regex{ ".*[iI]ndex .*[nN]ot [fF]ound.*" })) {
response.ctx.ec = errc::common::index_not_found;
} else if (response.ctx.first_error_message.find("Bucket Not Found") != std::string::npos) {
response.ctx.ec = errc::common::bucket_not_found;
}
break;
case 12009: /* IKey: "datastore.couchbase.DML_error" */
if (response.ctx.first_error_message.find("CAS mismatch") != std::string::npos) {
response.ctx.ec = errc::common::cas_mismatch;
Expand Down
11 changes: 6 additions & 5 deletions core/operations/management/error_utils.cxx
Expand Up @@ -18,6 +18,8 @@
#include "error_utils.hxx"
#include "core/utils/json.hxx"

#include <regex>

namespace couchbase::core::operations::management
{

Expand Down Expand Up @@ -116,13 +118,12 @@ translate_query_error_code(std::uint64_t error, const std::string& message, std:
{
switch (error) {
case 5000: /* IKey: "Internal Error" */
if (message.find(" already exists") != std::string::npos) {
if (std::regex_search(message, std::regex{ ".*[iI]ndex .*already exist.*" })) {
return errc::common::index_exists;
}
if (message.find("not found.") != std::string::npos) {
} else if (message.find("Index does not exist") != std::string::npos ||
std::regex_search(message, std::regex{ ".*[iI]ndex .*[nN]ot [fF]ound.*" })) {
return errc::common::index_not_found;
}
if (message.find("Bucket Not Found") != std::string::npos) {
} else if (message.find("Bucket Not Found") != std::string::npos) {
return errc::common::bucket_not_found;
}
break;
Expand Down
3 changes: 2 additions & 1 deletion core/operations/management/query_index_create.cxx
Expand Up @@ -23,6 +23,7 @@
#include "error_utils.hxx"

#include <fmt/core.h>
#include <regex>

namespace couchbase::core::operations::management
{
Expand Down Expand Up @@ -97,7 +98,7 @@ query_index_create_request::make_response(error_context::http&& ctx, const encod
error.message = entry.at("msg").get_string();
switch (error.code) {
case 5000: /* IKey: "Internal Error" */
if (error.message.find(" already exists") != std::string::npos) {
if (std::regex_search(error.message, std::regex{ ".*[iI]ndex .*already exist.*" })) {
index_already_exists = true;
}
if (error.message.find("Bucket Not Found") != std::string::npos) {
Expand Down
3 changes: 2 additions & 1 deletion core/operations/management/query_index_drop.cxx
Expand Up @@ -22,6 +22,7 @@
#include "error_utils.hxx"

#include <fmt/core.h>
#include <regex>

namespace couchbase::core::operations::management
{
Expand Down Expand Up @@ -80,7 +81,7 @@ query_index_drop_request::make_response(error_context::http&& ctx, const encoded
error.message = entry.at("msg").get_string();
switch (error.code) {
case 5000: /* IKey: "Internal Error" */
if (error.message.find("not found.") != std::string::npos) {
if (std::regex_search(error.message, std::regex{ ".*[iI]ndex .*[nN]ot [fF]ound.*" })) {
index_not_found = true;
}
break;
Expand Down

0 comments on commit faddc7e

Please sign in to comment.