diff --git a/include/dpp/restresults.h b/include/dpp/restresults.h index ec52ca1b71..3dd0ebb69c 100644 --- a/include/dpp/restresults.h +++ b/include/dpp/restresults.h @@ -220,7 +220,7 @@ struct DPP_EXPORT error_detail { /** * @brief Object field index */ - int index = 0; + DPP_DEPRECATED("index is unused and will be removed in a future version") int index = 0; }; /** diff --git a/src/dpp/cluster/confirmation.cpp b/src/dpp/cluster/confirmation.cpp index c6084ff136..0589705752 100644 --- a/src/dpp/cluster/confirmation.cpp +++ b/src/dpp/cluster/confirmation.cpp @@ -74,19 +74,16 @@ bool confirmation_callback_t::is_error() const { namespace { -std::vector find_errors_in_array(const std::string& obj, int index, const std::string& current_field, const json &j); - -std::vector find_errors_in_object(const std::string& obj, int index, const std::string& current_field, const json &j) { +std::vector find_errors_in_object(const std::string& obj, const std::string& current_field, const json &j) { std::vector ret; if (auto errors = j.find("_errors"); errors != j.end()) { - for (auto errordetails = errors->begin(); errordetails != errors->end(); ++errordetails) { + for (const json& errordetails : *errors) { error_detail detail; - detail.code = (*errordetails)["code"].get(); - detail.reason = (*errordetails)["message"].get(); + detail.code = errordetails["code"].get(); + detail.reason = errordetails["message"].get(); detail.field = current_field; detail.object = obj; - detail.index = index; ret.emplace_back(detail); } } else { @@ -97,16 +94,19 @@ std::vector find_errors_in_object(const std::string& obj, int inde if (obj.empty()) { field = current_field; } else if (isdigit(*current_field.c_str())) { - field = obj + '[' + current_field + ']'; + /* An element of an array, e.g. an element of a slash command vector for global_bulk_slash_command_create */ + field = obj; + field += '['; + field += current_field; + field += ']'; } else { - field = obj + '.' + current_field; + /* A field of an object, e.g. message.content too long */ + field = obj; + field += '.'; + field += current_field; } - if (it->is_array()) { - sub_errors = find_errors_in_array(field, index, it.key(), *it); - } else { - sub_errors = find_errors_in_object(field, index, it.key(), *it); - } + sub_errors = find_errors_in_object(field, it.key(), *it); if (!sub_errors.empty()) { ret.reserve(ret.capacity() + sub_errors.size()); @@ -117,30 +117,6 @@ std::vector find_errors_in_object(const std::string& obj, int inde return ret; } -std::vector find_errors_in_array(const std::string& obj, int index, const std::string& current_field, const json &j) { - std::vector ret; - int idx = 0; - - for (auto it = j.begin(); it != j.end(); ++it) { - std::vector sub_errors; - std::string key = '[' + std::to_string(idx) + ']'; - std::string field = obj.empty() ? current_field : obj + '.' + current_field; - - if (it->is_array()) { - sub_errors = find_errors_in_array(field, index, key, *it); - } else { - sub_errors = find_errors_in_object(field, index, key, *it); - } - - if (!sub_errors.empty()) { - ret.reserve(ret.capacity() + sub_errors.size()); - std::move(sub_errors.begin(), sub_errors.end(), std::back_inserter(ret)); - } - ++idx; - } - return ret; -} - } error_info confirmation_callback_t::get_error() const { @@ -152,66 +128,14 @@ error_info confirmation_callback_t::get_error() const { set_string_not_null(&j, "message", e.message); json& errors = j["errors"]; for (auto obj = errors.begin(); obj != errors.end(); ++obj) { + std::vector sub_errors; + std::string field = isdigit(*obj.key().c_str()) ? "[" + obj.key() + "]" : obj.key(); - /* Arrays in the error report are numerically indexed with a number in a string. Ugh. */ - if (isdigit(*(obj.key().c_str()))) { - /* An array of error messages */ - int array_index = std::atoll(obj.key().c_str()); - for (auto index = obj->begin(); index != obj->end(); ++index) { - if (index->find("_errors") != index->end()) { - /* A single object where one or more fields generated an error */ - for (auto errordetails = (*index)["_errors"].begin(); errordetails != (*index)["_errors"].end(); ++errordetails) { - error_detail detail; - detail.code = (*errordetails)["code"].get(); - detail.reason = (*errordetails)["message"].get(); - detail.object.clear(); - detail.field = obj.key(); - detail.index = array_index; - e.errors.emplace_back(detail); - } - } else { - /* An object where one or more fields within it generated an error, e.g. slash command */ - for (auto fields = index->begin(); fields != index->end(); ++fields) { - if (fields->find("_errors") != fields->end()) { - for (auto errordetails = (*fields)["_errors"].begin(); errordetails != (*fields)["_errors"].end(); ++errordetails) { - error_detail detail; - detail.code = (*errordetails)["code"].get(); - detail.reason = (*errordetails)["message"].get(); - detail.field = fields.key(); - detail.object = obj.key(); - detail.index = array_index; - e.errors.emplace_back(detail); - } - } else { - /* An array of objects where one or more generated an error, e.g. slash command bulk registration */ - for (auto fields2 = fields->begin(); fields2 != fields->end(); ++fields2) { - for (auto errordetails = (*fields2)["_errors"].begin(); errordetails != (*fields2)["_errors"].end(); ++errordetails) { - error_detail detail; - detail.code = (*errordetails)["code"].get(); - detail.reason = (*errordetails)["message"].get(); - detail.field = index.key() + "[" + fields.key() + "]." + fields2.key(); - detail.object = obj.key(); - detail.index = array_index; - e.errors.emplace_back(detail); - } - } - } - } - } - } - } else { - std::vector sub_errors; - - if (obj->is_array()) { - sub_errors = find_errors_in_array({}, -1, obj.key(), *obj); - } else { - sub_errors = find_errors_in_object({}, -1, obj.key(), *obj); - } - - if (!sub_errors.empty()) { - e.errors.reserve(e.errors.capacity() + sub_errors.size()); - std::move(sub_errors.begin(), sub_errors.end(), std::back_inserter(e.errors)); - } + sub_errors = find_errors_in_object({}, field, *obj); + + if (!sub_errors.empty()) { + e.errors.reserve(e.errors.capacity() + sub_errors.size()); + std::move(sub_errors.begin(), sub_errors.end(), std::back_inserter(e.errors)); } } @@ -221,15 +145,9 @@ error_info confirmation_callback_t::get_error() const { if (error.object.empty()) { /* A singular field with an error in an unnamed object */ e.human_readable += prefix + "- " + error.field + ": " + error.reason + " (" + error.code + ")"; - } else if (isdigit(*(error.object.c_str()))) { - /* An unnamed array of objects where one or more generated an error, e.g. slash command bulk registration */ - e.human_readable += prefix + "- [" + error.object + "]." + error.field + ": " + error.reason + " (" + error.code + ")"; - } else if (error.index < 0) { + } else { /* An object field that caused an error */ e.human_readable += prefix + "- " + error.object + '.' + error.field + ": " + error.reason + " (" + error.code + ")"; - } else { - /* A named array of objects whre a field in the object has an error */ - e.human_readable += prefix + "- " + error.object + "[" + std::to_string(error.index) + "]." + error.field + ": " + error.reason + " (" + error.code + ")"; } }