From ce22feee51901262fd3f7c0529bb752987107964 Mon Sep 17 00:00:00 2001 From: kremsy Date: Sat, 19 Apr 2014 23:14:37 +0200 Subject: [PATCH] improved exceptions --- application/core/Chat.php | 6 +-- .../DedicatedServer/Xmlrpc/FaultException.php | 6 +++ .../core/Manialinks/ManialinkManager.php | 33 +++++++---------- application/core/Maps/MapCommands.php | 11 ++---- application/core/Maps/MapList.php | 4 +- application/core/Players/PlayerCommands.php | 2 +- application/core/Players/PlayerList.php | 35 ++++++------------ application/core/Server/ServerCommands.php | 37 ++++++------------- 8 files changed, 52 insertions(+), 82 deletions(-) diff --git a/application/core/Chat.php b/application/core/Chat.php index 1eccd8b0..7ae9a2dd 100644 --- a/application/core/Chat.php +++ b/application/core/Chat.php @@ -4,6 +4,7 @@ namespace ManiaControl; use Maniaplanet\DedicatedServer\Xmlrpc\Exception; use ManiaControl\Players\Player; +use Maniaplanet\DedicatedServer\Xmlrpc\LoginUnknownException; /** * Chat Utility Class @@ -80,10 +81,7 @@ class Chat { } try{ $this->maniaControl->client->chatSendServerMessage($chatMessage, $login); - } catch(Exception $e){ - if($e->getMessage() != "Login unknown."){ - throw $e; - } + } catch(LoginUnknownException $e){ } } return true; diff --git a/application/core/Libs/Maniaplanet/DedicatedServer/Xmlrpc/FaultException.php b/application/core/Libs/Maniaplanet/DedicatedServer/Xmlrpc/FaultException.php index 92a4de2e..f0232673 100644 --- a/application/core/Libs/Maniaplanet/DedicatedServer/Xmlrpc/FaultException.php +++ b/application/core/Libs/Maniaplanet/DedicatedServer/Xmlrpc/FaultException.php @@ -21,6 +21,10 @@ class FaultException extends Exception return new StartIndexOutOfBoundException($faultString, $faultCode); case 'Not in script mode.': return new NotInScriptModeException($faultString, $faultCode); + case 'Change in progress.': + return new ChangeInProgressException($faultString, $faultCode); + case 'The player is not a spectator': + return new PlayerIsNotSpectatorException($faultString, $faultCode); } return new self($faultString, $faultCode); @@ -31,4 +35,6 @@ class LoginUnknownException extends FaultException {} class CouldNotWritePlaylistFileException extends FaultException {} class StartIndexOutOfBoundException extends FaultException {} class NotInScriptModeException extends FaultException {} +class ChangeInProgressException extends FaultException {} +class PlayerIsNotSpectatorException extends FaultException {} ?> diff --git a/application/core/Manialinks/ManialinkManager.php b/application/core/Manialinks/ManialinkManager.php index 88c2ab83..28bd0b0d 100644 --- a/application/core/Manialinks/ManialinkManager.php +++ b/application/core/Manialinks/ManialinkManager.php @@ -11,13 +11,15 @@ use ManiaControl\Callbacks\CallbackManager; use ManiaControl\ManiaControl; use ManiaControl\Players\Player; use Maniaplanet\DedicatedServer\Xmlrpc\Exception; +use Maniaplanet\DedicatedServer\Xmlrpc\LoginUnknownException; +use Maniaplanet\DedicatedServer\Xmlrpc\NotInScriptModeException; /** * Manialink Manager Class * - * @author steeffeen & kremsy + * @author steeffeen & kremsy * @copyright ManiaControl Copyright © 2014 ManiaControl Team - * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener { /* @@ -130,9 +132,9 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener * @return bool */ public function sendManialink($manialinkText, $logins = null, $timeout = 0, $hideOnClick = false) { - $manialinkText = (string) $manialinkText; + $manialinkText = (string)$manialinkText; - if(!$manialinkText) { + if (!$manialinkText) { return true; } @@ -150,7 +152,7 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener } if (is_array($logins)) { $success = true; - foreach ($logins as $login) { + foreach($logins as $login) { $subSuccess = $this->maniaControl->client->sendDisplayManialinkPage($login, $manialinkText, $timeout, $hideOnClick); if (!$subSuccess) { $success = false; @@ -159,11 +161,8 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener return $success; } - } catch(Exception $e) { - if($e->getMessage() == "Login unknown.") { - return false; - } - throw $e; + } catch(LoginUnknownException $e) { + return false; } return true; @@ -178,11 +177,8 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener public function enableAltMenu(Player $player) { try { $success = $this->maniaControl->client->triggerModeScriptEvent('LibXmlRpc_EnableAltMenu', $player->login); - } catch(Exception $e) { - if ($e->getMessage() == 'Not in script mode.') { - return false; - } - throw $e; + } catch(NotInScriptModeException $e) { + return false; } return $success; } @@ -196,11 +192,8 @@ class ManialinkManager implements ManialinkPageAnswerListener, CallbackListener public function disableAltMenu(Player $player) { try { $success = $this->maniaControl->client->triggerModeScriptEvent('LibXmlRpc_DisableAltMenu', $player->login); - } catch(Exception $e) { - if ($e->getMessage() == 'Not in script mode.') { - return false; - } - throw $e; + } catch(NotInScriptModeException $e) { + return false; } return $success; } diff --git a/application/core/Maps/MapCommands.php b/application/core/Maps/MapCommands.php index 607bb51c..8fbc25af 100644 --- a/application/core/Maps/MapCommands.php +++ b/application/core/Maps/MapCommands.php @@ -11,14 +11,14 @@ use ManiaControl\ManiaControl; use ManiaControl\Manialinks\IconManager; use ManiaControl\Manialinks\ManialinkPageAnswerListener; use ManiaControl\Players\Player; -use Maniaplanet\DedicatedServer\Xmlrpc\Exception; +use Maniaplanet\DedicatedServer\Xmlrpc\ChangeInProgressException; /** * Class offering Commands to manage Maps * - * @author steeffeen & kremsy + * @author steeffeen & kremsy * @copyright ManiaControl Copyright © 2014 ManiaControl Team - * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ class MapCommands implements CommandListener, ManialinkPageAnswerListener, CallbackListener { /* @@ -210,10 +210,7 @@ class MapCommands implements CommandListener, ManialinkPageAnswerListener, Callb try { $this->maniaControl->client->restartMap(); - } catch(Exception $e) { - if ($e->getMessage() != 'Change in progress.') { - throw $e; - } + } catch(ChangeInProgressException $e) { } } diff --git a/application/core/Maps/MapList.php b/application/core/Maps/MapList.php index 0ce663f1..1a01fd5e 100644 --- a/application/core/Maps/MapList.php +++ b/application/core/Maps/MapList.php @@ -553,7 +553,9 @@ class MapList implements ManialinkPageAnswerListener, CallbackListener { $index = $this->maniaControl->mapManager->getMapIndex($map); $this->maniaControl->client->jumpToMapIndex($index); } catch(Exception $e) { - // TODO: is it even possible that an exception other than connection errors will be thrown? - remove try-catch? + //TODO temp added 19.04.2014 + $this->maniaControl->errorHandler->triggerDebugNotice("Exception line 557 MapList.php" . $e->getMessage()); + $this->maniaControl->chat->sendError("Error while Switching Map"); } }); diff --git a/application/core/Players/PlayerCommands.php b/application/core/Players/PlayerCommands.php index 23cc3ad8..596cc012 100644 --- a/application/core/Players/PlayerCommands.php +++ b/application/core/Players/PlayerCommands.php @@ -109,7 +109,7 @@ class PlayerCommands implements CommandListener, ManialinkPageAnswerListener, Ca try { $this->maniaControl->client->autoTeamBalance(); } catch(Exception $e) { - $this->maniaControl->errorHandler->triggerDebugNotice("PlayerCommands Debug Line 110: " . $e->getMessage()); + $this->maniaControl->errorHandler->triggerDebugNotice("PlayerCommands Debug Line 112: " . $e->getMessage()); // TODO: only catch 'not in team mode' exception - throw others (like connection error) $this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login); return; diff --git a/application/core/Players/PlayerList.php b/application/core/Players/PlayerList.php index 26137246..2cf1df1d 100644 --- a/application/core/Players/PlayerList.php +++ b/application/core/Players/PlayerList.php @@ -23,14 +23,14 @@ use ManiaControl\Formatter; use ManiaControl\ManiaControl; use ManiaControl\Manialinks\ManialinkManager; use ManiaControl\Manialinks\ManialinkPageAnswerListener; -use Maniaplanet\DedicatedServer\Xmlrpc\Exception; +use Maniaplanet\DedicatedServer\Xmlrpc\PlayerIsNotSpectatorException; /** * PlayerList Widget Class * - * @author steeffeen & kremsy + * @author steeffeen & kremsy * @copyright ManiaControl Copyright © 2014 ManiaControl Team - * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ class PlayerList implements ManialinkPageAnswerListener, CallbackListener, TimerListener { /* @@ -650,10 +650,7 @@ class PlayerList implements ManialinkPageAnswerListener, CallbackListener, Timer try { $this->maniaControl->client->forceSpectator($adminLogin, PlayerActions::SPECTATOR_BUT_KEEP_SELECTABLE); $this->maniaControl->client->forceSpectatorTarget($adminLogin, $targetLogin, 1); - } catch(Exception $e) { - if($e->getMessage() != "The player is not a spectator"){ - throw $e; - } + } catch(PlayerIsNotSpectatorException $e) { } break; case self::ACTION_OPEN_PLAYER_DETAILED: @@ -724,10 +721,7 @@ class PlayerList implements ManialinkPageAnswerListener, CallbackListener, Timer try { $this->maniaControl->client->forceSpectator($target->login, PlayerActions::SPECTATOR_BUT_KEEP_SELECTABLE); $this->maniaControl->client->spectatorReleasePlayerSlot($target->login); - } catch(Exception $e) { - if ($e->getMessage() != 'Login unknown.' && $e->getMessage() != 'The player is not a spectator') { - $this->maniaControl->chat->sendException($e); - } + } catch(PlayerIsNotSpectatorException $e) { } }); break; @@ -748,14 +742,7 @@ class PlayerList implements ManialinkPageAnswerListener, CallbackListener, Timer $votesPlugin->undefineVote('kick'); $message = '$39F You got kicked due a Public vote!$z '; - try { - $this->maniaControl->client->kick($target->login, $message); - } catch(Exception $e) { - $this->maniaControl->errorHandler->triggerDebugNotice("PlayerList Debug Line 751: " . $e->getMessage()); - // TODO: only possible valid exception should be "wrong login" - throw others (like connection error) - $this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $target->login); - return; - } + $this->maniaControl->client->kick($target->login, $message); }); break; } @@ -767,16 +754,18 @@ class PlayerList implements ManialinkPageAnswerListener, CallbackListener, Timer * @param Player $player */ public function updateWidget(Player $player) { - foreach ($this->playersListShown as $login => $shown) { - if (!$shown) continue; - + foreach($this->playersListShown as $login => $shown) { + if (!$shown) { + continue; + } + // Check if shown player still exists $player = $this->maniaControl->playerManager->getPlayer($login); if (!$player) { unset($this->playersListShown[$login]); continue; } - + // Reopen widget if ($shown != self::SHOWN_MAIN_WINDOW) { $this->playersListShown[$login] = false; diff --git a/application/core/Server/ServerCommands.php b/application/core/Server/ServerCommands.php index 74bcb5b3..38ce8f9f 100644 --- a/application/core/Server/ServerCommands.php +++ b/application/core/Server/ServerCommands.php @@ -13,14 +13,14 @@ use ManiaControl\Commands\CommandListener; use ManiaControl\ManiaControl; use ManiaControl\Manialinks\ManialinkPageAnswerListener; use ManiaControl\Players\Player; -use Maniaplanet\DedicatedServer\Xmlrpc\Exception; +use Maniaplanet\DedicatedServer\Xmlrpc\NotInScriptModeException; /** * Class offering various Commands related to the Dedicated Server * - * @author steeffeen & kremsy + * @author steeffeen & kremsy * @copyright ManiaControl Copyright © 2014 ManiaControl Team - * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 + * @license http://www.gnu.org/licenses/ GNU General Public License, Version 3 */ class ServerCommands implements CallbackListener, CommandListener, ManialinkPageAnswerListener, TimerListener { /* @@ -106,11 +106,8 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage //Check if Pause exists in current GameMode try { $scriptInfos = $this->maniaControl->client->getModeScriptInfo(); - } catch(Exception $e) { - if ($e->getMessage() == 'Not in script mode.') { - return; - } - throw $e; + } catch(NotInScriptModeException $e) { + return; } $pauseExists = false; foreach($scriptInfos->commandDescs as $param) { @@ -164,12 +161,8 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage try { $this->maniaControl->client->triggerModeScriptEvent('WarmUp_Extend', '10'); - } catch(Exception $e) { - if ($e->getMessage() == 'Not in script mode.') { - $this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login); - return; - } - throw $e; + } catch(NotInScriptModeException $e) { + return; } $this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> extended the WarmUp by 10 seconds!'); @@ -189,12 +182,8 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage try { $this->maniaControl->client->triggerModeScriptEvent('WarmUp_Stop', ''); - } catch(Exception $e) { - if ($e->getMessage() == 'Not in script mode.') { - $this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login); - return; - } - throw $e; + } catch(NotInScriptModeException $e) { + return; } $this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> stopped the WarmUp!'); @@ -212,12 +201,8 @@ class ServerCommands implements CallbackListener, CommandListener, ManialinkPage } try { $this->maniaControl->client->sendModeScriptCommands(array('Command_ForceWarmUp' => True)); - } catch(Exception $e) { - if ($e->getMessage() == 'Not in script mode.') { - $this->maniaControl->chat->sendError('Error occurred: ' . $e->getMessage(), $player->login); - return; - } - throw $e; + } catch(NotInScriptModeException $e) { + return; } $this->maniaControl->chat->sendInformation('$<' . $player->nickname . '$> paused the Game!');