git repos / saland

commit 49e814a6

Poul Sander · 2019-04-15 17:21
49e814a6d0f647e07523b5a50409089ddbbfdcc6 patch · browse files
parent 44151aef78f63bce1ce6592f9eadeb67cbe0cded

Better error handling for the console commands

Changed files

M src/saland/Game.cpp before
M src/saland/console/Console.cpp before
diff --git a/src/saland/Game.cpp b/src/saland/Game.cpp index 1487202..89e5c98 100644 --- a/src/saland/Game.cpp +++ b/src/saland/Game.cpp
@@ -66,6 +66,16 @@ bool PlaceablesSortLowerY(const std::shared_ptr<Placeable>& i, const std::shared
return i->Y+i->Radius > j->Y+j->Radius;
}
+static int string2int_trows(const std::string& s) {
+ try {
+ return std::stoi(s);
+ } catch (std::exception&){
+ std::stringstream ss;
+ ss << "Failed to convert \"" << s << "\" to an integer";
+ throw std::runtime_error(ss.str());
+ }
+}
+
static bool reset_region = false;
static bool teleport = false;
static int teleportX = 0;
@@ -78,8 +88,8 @@ struct GotoConsoleCommand : public ConsoleCommand {
return "Must be ran like \"goto X Y\"";
}
teleport = true;
- teleportX = std::stoi(args[1]);
- teleportY = std::stoi(args[2]);
+ teleportX = string2int_trows(args[1]);
+ teleportY = string2int_trows(args[2]);
return "Teleport queued!";
}
};
diff --git a/src/saland/console/Console.cpp b/src/saland/console/Console.cpp index 4d444f7..6716bf2 100644 --- a/src/saland/console/Console.cpp +++ b/src/saland/console/Console.cpp
@@ -130,8 +130,12 @@ void Console::ProcessCommand(const std::string& command) {
sago::SagoTextField response;
SetFieldValues(response);
if (commandVector.size() && commands.find(commandVector[0]) != commands.end() ) {
- std::string ret = commands[commandVector[0]]->run(commandVector);
- response.SetText(ret);
+ try {
+ std::string ret = commands[commandVector[0]]->run(commandVector);
+ response.SetText(ret);
+ } catch (std::exception& e) {
+ response.SetText(e.what());
+ }
}
else {
response.SetText(std::string(" \"")+command+"\" not recognized");