[Server] Use Utils function as workaround for "bad exception" on Windows

sol2-server-rewrite
David Cernat 7 years ago
parent cfb5835e17
commit 456bcee68a

@ -93,10 +93,10 @@ std::deque<std::string> CommandController::cmdParser(const std::string &message)
/*auto const command = '/' >> *~x3::char_(sep) >> sep;
if (!x3::phrase_parse(message.cbegin(), message.cend(), arguments, command, ret))
throw runtime_error("failed to parse message: "+ message);*/
Utils::throwError("failed to parse message: "+ message);*/
if (!x3::parse(message.cbegin(), message.cend(), arguments, ret))
throw runtime_error("failed to parse message: " + message);
Utils::throwError("failed to parse message: " + message);
return ret;
}

@ -100,7 +100,7 @@ EventController::EventController(LuaState *luaCtrl)
});
if (!found)
throw runtime_error("Event " + to_string(i) + " is not registered. Dear developer, please fix me :D");
Utils::throwError("Event " + to_string(i) + " is not registered");
#endif
events[i]; // create core event
}

@ -406,7 +406,7 @@ void checkDependencies(const vector<ServerModuleInfo> &modules, const ServerModu
sstr << depNameRequest << ": version \"" << depVersionFound << "\" is not applicable for \""
<< smi.name << "\" expected \"" << depVersionRequest + "\"";
if (fatal)
throw runtime_error(sstr.str());
Utils::throwError(sstr.str());
else
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "%s", sstr.str().c_str());
}
@ -416,7 +416,7 @@ void checkDependencies(const vector<ServerModuleInfo> &modules, const ServerModu
stringstream sstr;
sstr << depNameRequest + " \"" << depVersionRequest << "\" not found.";
if (fatal)
throw runtime_error(sstr.str());
Utils::throwError(sstr.str());
else
LOG_MESSAGE_SIMPLE(Log::LOG_WARN, "%s", sstr.str().c_str());
}
@ -468,7 +468,7 @@ vector<vector<ServerModuleInfo>::iterator> loadOrderSolver(vector<ServerModuleIn
stringstream sstr;
sstr << "Found circular dependency: \"" << (*it)->name << "\" and \"" << (*found)->name
<< "\" depend on each other" << endl;
throw runtime_error(sstr.str());
Utils::throwError(sstr.str());
}
}
@ -497,7 +497,7 @@ void LuaState::loadModules(const std::string &moduleDir, std::vector<std::string
auto readConfig = [this](path homePath){
const auto mainScript = "main.lua";
if (!is_directory(homePath / "modules"))
throw runtime_error(homePath.string() + ": No such directory.");
Utils::throwError(homePath.string() + ": No such directory.");
for (const auto &moduleDir : directory_iterator(homePath / "modules"))
{
if (is_directory(moduleDir.status()) && exists(moduleDir.path() / mainScript))
@ -557,7 +557,7 @@ void LuaState::loadModules(const std::string &moduleDir, std::vector<std::string
}
}
if (!found)
throw runtime_error("Module: \"" + mssp + "\" not found");
Utils::throwError("Module: \"" + mssp + "\" not found");
}
}
else

@ -6,6 +6,8 @@
using namespace std;
static std::string lastErrorMessage = "unknown error";
const vector<string> Utils::split(const string &str, int delimiter)
{
string buffer;
@ -52,3 +54,20 @@ ESM::Cell Utils::getCellFromDescription(std::string cellDescription)
return cell;
}
const std::string Utils::getLastError()
{
return lastErrorMessage;
}
void Utils::throwError(const std::string errorMessage)
{
#ifdef _WIN32
// Throwing exceptions makes them show up as "bad exception" on Windows with
// our stacktracer, so record their intended error messages separately here
// when possible
lastErrorMessage = errorMessage;
#endif
throw runtime_error(errorMessage);
}

@ -40,6 +40,10 @@ namespace Utils
ESM::Cell getCellFromDescription(std::string cellDescription);
const std::string getLastError();
void throwError(std::string errorMessage);
template<size_t N>
constexpr unsigned int hash(const char(&str)[N], size_t I = N)
{

@ -151,7 +151,13 @@ int main(int argc, char *argv[])
}
catch (const exception &e)
{
LOG_MESSAGE_SIMPLE(Log::LOG_FATAL, " Woops, something wrong! Exception:\n\t%s", e.what());
LOG_MESSAGE_SIMPLE(Log::LOG_FATAL, "A fatal error has occurred!");
#ifndef _WIN32
LOG_APPEND(Log::LOG_FATAL, "\t%s", e.what());
#else
LOG_APPEND(Log::LOG_FATAL, "\t%s", Utils::getLastError().c_str());
#endif
}
stacktrace();
@ -232,21 +238,21 @@ int main(int argc, char *argv[])
case RakNet::RAKNET_STARTED:
break;
case RakNet::RAKNET_ALREADY_STARTED:
throw runtime_error("Already started");
Utils::throwError("Already started");
case RakNet::INVALID_SOCKET_DESCRIPTORS:
throw runtime_error("Incorrect port or address");
Utils::throwError("Incorrect port or address");
case RakNet::INVALID_MAX_CONNECTIONS:
throw runtime_error("Max players cannot be negative or 0");
Utils::throwError("Max players cannot be negative or 0");
case RakNet::SOCKET_FAILED_TO_BIND:
case RakNet::SOCKET_PORT_ALREADY_IN_USE:
case RakNet::PORT_CANNOT_BE_ZERO:
throw runtime_error("Failed to bind port");
Utils::throwError("Failed to bind port");
case RakNet::SOCKET_FAILED_TEST_SEND:
case RakNet::SOCKET_FAMILY_NOT_SUPPORTED:
case RakNet::FAILED_TO_CREATE_NETWORK_THREAD:
case RakNet::COULD_NOT_GENERATE_GUID:
case RakNet::STARTUP_OTHER_FAILURE:
throw runtime_error("Cannot start server");
Utils::throwError("Cannot start server");
}
peer->SetMaximumIncomingConnections((unsigned short) (players));

Loading…
Cancel
Save