Some updates

This commit is contained in:
WolverinDEV
2020-04-28 18:27:49 +02:00
parent 633fe10821
commit ff88705f09
15 changed files with 120 additions and 79 deletions
+58 -19
View File
@@ -571,27 +571,66 @@ bool ConnectedClient::notifyClientNeededPermissions() {
return true;
}
inline void write_command_result_error(ts::command_builder_bulk bulk, const command_result& result) {
bulk.put_unchecked("id", (uint32_t) result.error_code());
bulk.put_unchecked("msg", findError(result.error_code()).message);
if(result.is_permission_error())
bulk.put_unchecked("failed_permid", (uint32_t) result.permission_id());
}
inline void write_command_result_detailed(ts::command_builder_bulk bulk, const command_result& result) {
auto details = result.details();
bulk.put_unchecked("id", (uint32_t) details->error_id);
bulk.put_unchecked("msg", findError(details->error_id).message);
for(const auto& extra : details->extra_properties)
bulk.put(extra.first, extra.second);
}
bool ConnectedClient::notifyError(const command_result& result, const std::string& retCode) {
Command cmd("error");
ts::command_builder command{"error"};
if(result.is_detailed()) {
auto detailed = result.details();
cmd["id"] = (int) detailed->error_id;
cmd["msg"] = findError(detailed->error_id).message;
switch(result.type()) {
case command_result_type::error:
write_command_result_error(command.bulk(0), result);
break;
case command_result_type::detailed:
write_command_result_detailed(command.bulk(0), result);
break;
for(const auto& extra : detailed->extra_properties)
cmd[extra.first] = extra.second;
} else {
cmd["id"] = (int) result.error_code();
cmd["msg"] = findError(result.error_code()).message;
if(result.is_permission_error())
cmd["failed_permid"] = result.permission_id();
case command_result_type::bulked: {
auto bulks = result.bulks();
command.reserve_bulks(bulks->size());
for(size_t index{0}; index < bulks->size(); index++) {
auto entry = bulks->at(index);
switch (entry.type()) {
case command_result_type::error:
write_command_result_error(command.bulk(index), entry);
break;
case command_result_type::detailed:
write_command_result_detailed(command.bulk(index), entry);
break;
case command_result_type::bulked:
assert(false);
break;
}
}
if(bulks->empty()) {
logWarning(this->getServerId(), "{} Trying to send empty error bulk.", CLIENT_STR_LOG_PREFIX_(this));
command.put_unchecked(0, "id", (uint32_t) error::ok);
command.put_unchecked(0, "msg", findError(error::ok).message);
}
break;
}
default:
assert(false);
break;
}
if(retCode.length() > 0)
cmd["return_code"] = retCode;
if(!retCode.empty())
command.put_unchecked(0, "return_code", retCode);
this->sendCommand(cmd);
this->sendCommand(command);
return true;
}
@@ -793,7 +832,7 @@ bool ConnectedClient::handleCommandFull(Command& cmd, bool disconnectOnFail) {
this->disconnect("Invalid argument (" + string(ex.what()) + ")");
return false;
} else {
result = command_result{error::parameter_convert};
result = command_result{error::parameter_convert, ex.what()};
}
} catch (exception& ex) {
logWarning(this->getServerId(), "{}[Command] Failed to handle command. Received exception with message: {}", CLIENT_STR_LOG_PREFIX, ex.what());
@@ -809,7 +848,7 @@ bool ConnectedClient::handleCommandFull(Command& cmd, bool disconnectOnFail) {
}
bool generateReturnStatus = false;
if(result.error_code() != error::ok || this->getType() == ClientType::CLIENT_QUERY){
if(result.has_error() || this->getType() == ClientType::CLIENT_QUERY){
generateReturnStatus = true;
} else if(cmd["return_code"].size() > 0) {
generateReturnStatus = !cmd["return_code"].string().empty();
@@ -818,7 +857,7 @@ bool ConnectedClient::handleCommandFull(Command& cmd, bool disconnectOnFail) {
if(generateReturnStatus)
this->notifyError(result, cmd["return_code"].size() > 0 ? cmd["return_code"].first().as<std::string>() : "");
if(result.error_code() != error::ok && this->state == ConnectionState::INIT_HIGH)
if(result.has_error() && this->state == ConnectionState::INIT_HIGH)
this->close_connection(system_clock::now()); //Disconnect now
for (const auto& handler : postCommandHandler)
@@ -832,7 +871,7 @@ bool ConnectedClient::handleCommandFull(Command& cmd, bool disconnectOnFail) {
else
logWarning(this->getServerId(), "Command handling of command {} needs {}ms.", cmd.command(), duration_cast<milliseconds>(end - start).count());
}
result.release_details();
result.release_data();
return true;
}