Skip to content

Commit

Permalink
fix: actually just do this. sorry brain
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishura4 committed May 1, 2024
1 parent 6410e63 commit dcd448f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 105 deletions.
2 changes: 1 addition & 1 deletion include/dpp/restresults.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

/**
Expand Down
126 changes: 22 additions & 104 deletions src/dpp/cluster/confirmation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,16 @@ bool confirmation_callback_t::is_error() const {

namespace {

std::vector<error_detail> find_errors_in_array(const std::string& obj, int index, const std::string& current_field, const json &j);

std::vector<error_detail> find_errors_in_object(const std::string& obj, int index, const std::string& current_field, const json &j) {
std::vector<error_detail> find_errors_in_object(const std::string& obj, const std::string& current_field, const json &j) {
std::vector<error_detail> 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<std::string>();
detail.reason = (*errordetails)["message"].get<std::string>();
detail.code = errordetails["code"].get<std::string>();
detail.reason = errordetails["message"].get<std::string>();
detail.field = current_field;
detail.object = obj;
detail.index = index;
ret.emplace_back(detail);
}
} else {
Expand All @@ -97,16 +94,19 @@ std::vector<error_detail> 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());
Expand All @@ -117,30 +117,6 @@ std::vector<error_detail> find_errors_in_object(const std::string& obj, int inde
return ret;
}

std::vector<error_detail> find_errors_in_array(const std::string& obj, int index, const std::string& current_field, const json &j) {
std::vector<error_detail> ret;
int idx = 0;

for (auto it = j.begin(); it != j.end(); ++it) {
std::vector<error_detail> 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 {
Expand All @@ -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<error_detail> sub_errors;
std::string field = isdigit(*obj.key().c_str()) ? "<array>[" + 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<std::string>();
detail.reason = (*errordetails)["message"].get<std::string>();
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<std::string>();
detail.reason = (*errordetails)["message"].get<std::string>();
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<std::string>();
detail.reason = (*errordetails)["message"].get<std::string>();
detail.field = index.key() + "[" + fields.key() + "]." + fields2.key();
detail.object = obj.key();
detail.index = array_index;
e.errors.emplace_back(detail);
}
}
}
}
}
}
} else {
std::vector<error_detail> 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));
}
}

Expand All @@ -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 + "- <array>[" + 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 + ")";
}
}

Expand Down

0 comments on commit dcd448f

Please sign in to comment.