Selaa lähdekoodia

Attempt to fix & improve error detection:

- use std::fstream instead of boost::iostreams for properly working flush
- removed some catch-all blocks
- reduce catch scope of some try/catch blocks to clearly indicate intent
Ivan Savenko 2 vuotta sitten
vanhempi
sitoutus
21e794268b

+ 0 - 20
Global.h

@@ -253,26 +253,6 @@ using TLockGuardRec = std::lock_guard<std::recursive_mutex>;
 
 VCMI_LIB_NAMESPACE_BEGIN
 
-void inline handleException()
-{
-	try
-	{
-		throw;
-	}
-	catch(const std::exception & ex)
-	{
-		logGlobal->error(ex.what());
-	}
-	catch(const std::string & ex)
-	{
-		logGlobal->error(ex);
-	}
-	catch(...)
-	{
-		logGlobal->error("Sorry, caught unknown exception type. No more info available.");
-	}
-}
-
 namespace vstd
 {
 	// combine hashes. Present in boost but not in std

+ 5 - 2
client/CServerHandler.cpp

@@ -865,8 +865,11 @@ void CServerHandler::threadHandleConnection()
 		}
 		else
 		{
-			logNetwork->error("Lost connection to server, ending listening thread!");
-			logNetwork->error(e.what());
+			if (e.code() == boost::asio::error::eof)
+				logNetwork->error("Lost connection to server, ending listening thread! Connection has been closed");
+			else
+				logNetwork->error("Lost connection to server, ending listening thread! Reason: %s", e.what());
+
 			if(client)
 			{
 				state = EClientState::DISCONNECTING;

+ 4 - 11
lib/VCMI_Lib.cpp

@@ -48,17 +48,10 @@ DLL_LINKAGE void preinitDLL(CConsoleHandler * Console, bool onlyEssential, bool
 {
 	console = Console;
 	VLC = new LibClasses();
-	try
-	{
-		VLC->loadFilesystem(extractArchives);
-		settings.init();
-		VLC->loadModFilesystem(onlyEssential);
-	}
-	catch(...)
-	{
-		handleException();
-		throw;
-	}
+	VLC->loadFilesystem(extractArchives);
+	settings.init();
+	VLC->loadModFilesystem(onlyEssential);
+
 }
 
 DLL_LINKAGE void loadDLLClasses(bool onlyEssential)

+ 0 - 1
lib/gameState/CGameState.cpp

@@ -569,7 +569,6 @@ void CGameState::initNewGame(const IMapService * mapService, bool allowSavingRan
 			catch(...)
 			{
 				logGlobal->error("Saving random map failed with exception");
-				handleException();
 			}
 		}
 

+ 1 - 1
lib/logging/CLogger.h

@@ -220,7 +220,7 @@ public:
 	void write(const LogRecord & record) override;
 
 private:
-	FileStream file;
+	boost::filesystem::fstream file;
 	CLogFormatter formatter;
 	mutable std::mutex mx;
 };

+ 2 - 9
lib/mapObjectConstructors/AObjectTypeHandler.cpp

@@ -77,15 +77,8 @@ void AObjectTypeHandler::init(const JsonNode & input)
 		tmpl->id = Obj(type);
 		tmpl->subid = subtype;
 		tmpl->stringID = entry.first; // FIXME: create "fullID" - type.object.template?
-		try
-		{
-			tmpl->readJson(entry.second);
-			templates.push_back(std::shared_ptr<const ObjectTemplate>(tmpl));
-		}
-		catch (const std::exception & e)
-		{
-			logGlobal->warn("Failed to load terrains for object %s: %s", entry.first, e.what());
-		}
+		tmpl->readJson(entry.second);
+		templates.push_back(std::shared_ptr<const ObjectTemplate>(tmpl));
 	}
 
 	for(const JsonNode & node : input["sounds"]["ambient"].Vector())

+ 0 - 4
server/CGameHandler.cpp

@@ -109,10 +109,6 @@ public:
             (void)e;
 			return false;
 		}
-		catch(...)
-		{
-			throw;
-		}
 	}
 };
 

+ 24 - 43
server/CVCMIServer.cpp

@@ -409,51 +409,36 @@ void CVCMIServer::threadHandleClient(std::shared_ptr<CConnection> c)
 	setThreadName("CVCMIServer::handleConnection");
 	c->enterLobbyConnectionMode();
 
-#ifndef _MSC_VER
-	try
+	while(c->connected)
 	{
-#endif
-		while(c->connected)
+		CPack * pack;
+
+		try
 		{
-			CPack * pack;
-			
-			try
-			{
-				pack = c->retrievePack();
-			}
-			catch(boost::system::system_error & e)
-			{
+			pack = c->retrievePack();
+		}
+		catch(boost::system::system_error & e)
+		{
+			if (e.code() == boost::asio::error::eof)
+				logNetwork->error("Network error receiving a pack. Connection has been closed");
+			else
 				logNetwork->error("Network error receiving a pack. Connection %s dies. What happened: %s", c->toString(), e.what());
-				hangingConnections.insert(c);
-				connections.erase(c);
-				if(connections.empty() || hostClient == c)
-					state = EServerState::SHUTDOWN;
 
-				if(gh && state == EServerState::GAMEPLAY)
-				{
-					gh->handleClientDisconnection(c);
-				}
-				break;
-			}
+			hangingConnections.insert(c);
+			connections.erase(c);
+			if(connections.empty() || hostClient == c)
+				state = EServerState::SHUTDOWN;
 
-			CVCMIServerPackVisitor visitor(*this, this->gh);
-			pack->visit(visitor);
+			if(gh && state == EServerState::GAMEPLAY)
+			{
+				gh->handleClientDisconnection(c);
+			}
+			break;
 		}
-#ifndef _MSC_VER
-	}
-	catch(const std::exception & e)
-	{
-		(void)e;
-		boost::unique_lock<boost::recursive_mutex> queueLock(mx);
-		logNetwork->error("%s dies... \nWhat happened: %s", c->toString(), e.what());
-	}
-	catch(...)
-	{
-		state = EServerState::SHUTDOWN;
-		handleException();
-		throw;
+
+		CVCMIServerPackVisitor visitor(*this, this->gh);
+		pack->visit(visitor);
 	}
-#endif
 
 	boost::unique_lock<boost::recursive_mutex> queueLock(mx);
 
@@ -1022,7 +1007,7 @@ static void handleCommandOptions(int argc, const char * argv[], boost::program_o
 		{
 			po::store(po::parse_command_line(argc, argv, opts), options);
 		}
-		catch(std::exception & e)
+		catch(po::error & e)
 		{
 			std::cerr << "Failure during parsing command-line options:\n" << e.what() << std::endl;
 		}
@@ -1113,10 +1098,6 @@ int main(int argc, const char * argv[])
 			logNetwork->error(e.what());
 			server.state = EServerState::SHUTDOWN;
 		}
-		catch(...)
-		{
-			handleException();
-		}
 	}
 	catch(boost::system::system_error & e)
 	{

+ 5 - 5
server/PlayerMessageProcessor.cpp

@@ -175,7 +175,7 @@ void PlayerMessageProcessor::cheatGiveArmy(PlayerColor player, const CGHeroInsta
 	{
 		amountPerSlot = std::stol(words.at(1));
 	}
-	catch(std::exception&)
+	catch(std::logic_error&)
 	{
 	}
 
@@ -233,7 +233,7 @@ void PlayerMessageProcessor::cheatLevelup(PlayerColor player, const CGHeroInstan
 	{
 		levelsToGain = std::stol(words.at(0));
 	}
-	catch(std::exception&)
+	catch(std::logic_error&)
 	{
 		levelsToGain = 1;
 	}
@@ -252,7 +252,7 @@ void PlayerMessageProcessor::cheatExperience(PlayerColor player, const CGHeroIns
 	{
 		expAmountProcessed = std::stol(words.at(0));
 	}
-	catch(std::exception&)
+	catch(std::logic_error&)
 	{
 		expAmountProcessed = 10000;
 	}
@@ -271,7 +271,7 @@ void PlayerMessageProcessor::cheatMovement(PlayerColor player, const CGHeroInsta
 	{
 		smp.val = std::stol(words.at(0));;
 	}
-	catch(std::exception&)
+	catch(std::logic_error&)
 	{
 		smp.val = 1000000;
 	}
@@ -293,7 +293,7 @@ void PlayerMessageProcessor::cheatResources(PlayerColor player, std::vector<std:
 	{
 		baseResourceAmount = std::stol(words.at(0));;
 	}
-	catch(std::exception&)
+	catch(std::logic_error&)
 	{
 		baseResourceAmount = 100;
 	}