Using new command error system

This commit is contained in:
WolverinDEV
2020-01-25 23:42:37 +01:00
parent 10092cfab0
commit bb2e7699dc
18 changed files with 1223 additions and 1190 deletions
+22 -29
View File
@@ -578,18 +578,24 @@ bool ConnectedClient::notifyClientNeededPermissions() {
return true;
}
bool ConnectedClient::notifyError(const CommandResult& result, const std::string& retCode) {
bool ConnectedClient::notifyError(const command_result& result, const std::string& retCode) {
Command cmd("error");
cmd["id"] = result.error.errorId;
cmd["msg"] = result.error.message;
if(result.is_detailed()) {
auto detailed = result.details();
cmd["id"] = (int) detailed->error_id;
cmd["msg"] = findError(detailed->error_id).message;
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(retCode.length() > 0)
cmd["return_code"] = retCode;
for(const auto& extra : result.extraProperties)
cmd[extra.first] = extra.second;
this->sendCommand(cmd);
return true;
}
@@ -792,60 +798,46 @@ bool ConnectedClient::handleCommandFull(Command& cmd, bool disconnectOnFail) {
logTrace(this->getServerId() == 0 ? LOG_QUERY : this->getServerId(), "{}[Command][Client -> Server] Processing command: {}", CLIENT_STR_LOG_PREFIX, cmd.build(false));
#endif
CommandResult result;
command_result result;
try {
result = this->handleCommand(cmd);
} catch(invalid_argument& ex){
debugMessage(this->getServerId(), "{}[Command] Execution throws invalid_argument exception ({}).", CLIENT_STR_LOG_PREFIX, ex.what());
logWarning(this->getServerId(), "{}[Command] Failed to handle command. Received invalid argument exception: {}", CLIENT_STR_LOG_PREFIX, ex.what());
if(disconnectOnFail) {
this->disconnect("Invalid argument (" + string(ex.what()) + ")");
return false;
} else {
result = {findError("parameter_convert"), "Invalid argument (" + string(ex.what()) + ")"};
result = command_result{error::parameter_convert};
}
} catch (exception& ex) {
logWarning(this->getServerId(), "{}[Command] Failed to handle command. Received exception with message: {}", CLIENT_STR_LOG_PREFIX, ex.what());
if(disconnectOnFail) {
this->disconnect("Error while command handling (" + string(ex.what()) + ")!");
return false;
} else {
result = {findError("vs_critical"), "error while command handling (" + string(ex.what()) + ")"};
result = command_result{error::vs_critical};
}
} catch (...) {
this->disconnect("Error while command handling! (unknown)");
return false;
}
bool generateReturnStatus = false;
if(result.type() == PERM_ERROR){
if(result.error_code() != error::ok || this->getType() == ClientType::CLIENT_QUERY){
generateReturnStatus = true;
} else if(cmd["return_code"].size() > 0) {
generateReturnStatus = !cmd["return_code"].string().empty();
}
if(this->getType() == ClientType::CLIENT_QUERY)
generateReturnStatus = true;
if (!result) {
generateReturnStatus = true;
stringstream ss;
ss << "{";
for(auto it = result.extraProperties.begin(); it != result.extraProperties.end();){
ss << it->first << " = " << it->second;
if(++it != result.extraProperties.end())
ss << ", ";
}
ss << "}" << endl;
logTrace(this->getServerId(), "{}[Command] Command {} with return code {} fails and returns error code {:#06x}. Properties: {}", CLIENT_STR_LOG_PREFIX, cmd.command(), cmd["return_code"].size() ? "\"" + cmd["return_code"].first().as<std::string>() + "\"" : "<undefined>", result.error.errorId, ss.str());
}
if(generateReturnStatus)
this->notifyError(result, cmd["return_code"].size() > 0 ? cmd["return_code"].first().as<std::string>() : "");
if(!result && this->state == ConnectionState::INIT_HIGH) {
if(result.error_code() != error::ok && this->state == ConnectionState::INIT_HIGH)
this->closeConnection(system_clock::now()); //Disconnect now
}
for (const auto& handler : postCommandHandler)
handler();
postCommandHandler.clear();
end = system_clock::now();
if(end - start > milliseconds(10)) {
@@ -854,6 +846,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();
return true;
}