From 8fe56d9e01acca1910cec31c6cfa0927f4a2d749 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20D=C3=BCsterhus?= Date: Tue, 18 Jun 2013 00:28:56 +0200 Subject: [PATCH] Throw InvalidArgumentException instead of NotFoundException It provides better error messages for the user --- file/lib/data/message/MessageAction.class.php | 3 +++ .../system/command/CommandHandler.class.php | 2 +- .../command/NotFoundException.class.php | 21 ++++++++++++++++++- .../command/commands/ColorCommand.class.php | 2 +- .../command/commands/FreeCommand.class.php | 2 +- .../command/commands/MuteCommand.class.php | 2 +- .../command/commands/WhisperCommand.class.php | 2 +- 7 files changed, 28 insertions(+), 6 deletions(-) diff --git a/file/lib/data/message/MessageAction.class.php b/file/lib/data/message/MessageAction.class.php index 128d389..4c6f0e7 100644 --- a/file/lib/data/message/MessageAction.class.php +++ b/file/lib/data/message/MessageAction.class.php @@ -125,6 +125,9 @@ public function validateSend() { catch (\chat\system\command\UserNotFoundException $e) { throw new UserInputException('text', WCF::getLanguage()->getDynamicVariable('chat.error.userNotFound', array('exception' => $e))); } + catch (\InvalidArgumentException $e) { + throw new UserInputException('text', WCF::getLanguage()->getDynamicVariable('chat.error.invalidArgument', array('exception' => $e))); + } } else { $this->parameters['type'] = Message::TYPE_NORMAL; diff --git a/file/lib/system/command/CommandHandler.class.php b/file/lib/system/command/CommandHandler.class.php index c1ad397..c575e62 100644 --- a/file/lib/system/command/CommandHandler.class.php +++ b/file/lib/system/command/CommandHandler.class.php @@ -111,7 +111,7 @@ public function loadCommand() { $class = '\chat\system\command\commands\\'.ucfirst(strtolower($parts[0])).'Command'; if (!class_exists($class)) { - throw new NotFoundException(); + throw new NotFoundException($parts[0]); } return new $class($this); diff --git a/file/lib/system/command/NotFoundException.class.php b/file/lib/system/command/NotFoundException.class.php index 55cd1c2..18b7c5a 100644 --- a/file/lib/system/command/NotFoundException.class.php +++ b/file/lib/system/command/NotFoundException.class.php @@ -10,4 +10,23 @@ * @package be.bastelstu.chat * @subpackage system.chat.command */ -class NotFoundException extends \Exception { } +class NotFoundException extends \Exception { + /** + * given command + * @var string + */ + private $command = ''; + + public function __construct($command) { + $this->command = $command; + } + + /** + * Returns the given command + * + * @return string + */ + public function getCommand() { + return $this->command; + } +} diff --git a/file/lib/system/command/commands/ColorCommand.class.php b/file/lib/system/command/commands/ColorCommand.class.php index 16fada1..f8263a1 100644 --- a/file/lib/system/command/commands/ColorCommand.class.php +++ b/file/lib/system/command/commands/ColorCommand.class.php @@ -55,7 +55,7 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler) foreach ($color as $key => $val) { if (isset(self::$colors[$val])) $color[$key] = self::$colors[$val]; else { - if (!$regex->match($val)) throw new \chat\system\command\NotFoundException(); + if (!$regex->match($val)) throw new \InvalidArgumentException(); $matches = $regex->getMatches(); $val = $matches[1]; diff --git a/file/lib/system/command/commands/FreeCommand.class.php b/file/lib/system/command/commands/FreeCommand.class.php index 833473f..6297d8f 100644 --- a/file/lib/system/command/commands/FreeCommand.class.php +++ b/file/lib/system/command/commands/FreeCommand.class.php @@ -15,7 +15,7 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler) parent::__construct($commandHandler); if (\wcf\util\StringUtil::toLowerCase($this->commandHandler->getParameters()) != 'the fish') { - throw new \chat\system\command\NotFoundException(); + throw new \InvalidArgumentException(); } $this->didInit(); diff --git a/file/lib/system/command/commands/MuteCommand.class.php b/file/lib/system/command/commands/MuteCommand.class.php index f6df4be..00ef2f4 100644 --- a/file/lib/system/command/commands/MuteCommand.class.php +++ b/file/lib/system/command/commands/MuteCommand.class.php @@ -31,7 +31,7 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler) $this->expires = min(max(-0x80000000, $expires), 0x7FFFFFFF); } catch (\wcf\system\exception\SystemException $e) { - throw new \chat\system\command\NotFoundException(); + throw new \InvalidArgumentException(); } $this->user = User::getUserByUsername($username); diff --git a/file/lib/system/command/commands/WhisperCommand.class.php b/file/lib/system/command/commands/WhisperCommand.class.php index 04333c5..38faa18 100644 --- a/file/lib/system/command/commands/WhisperCommand.class.php +++ b/file/lib/system/command/commands/WhisperCommand.class.php @@ -26,7 +26,7 @@ public function __construct(\chat\system\command\CommandHandler $commandHandler) $this->message = \wcf\util\StringUtil::trim($message); } catch (\wcf\system\exception\SystemException $e) { - throw new \chat\system\command\NotFoundException(); + throw new \InvalidArgumentException(); } $this->user = User::getUserByUsername($username);