From 30868d2194a38689b473883beb69333457bb6d5f Mon Sep 17 00:00:00 2001 From: David Grudl Date: Thu, 26 Jan 2017 15:12:54 +0100 Subject: [PATCH] added RejectRequestException, a descendant of BadRequestException thrown by framework [WIP] --- src/Application/Application.php | 6 +++--- src/Application/MicroPresenter.php | 2 +- src/Application/UI/Component.php | 11 ++++++----- src/Application/UI/Form.php | 2 +- src/Application/UI/Presenter.php | 13 +++++++------ src/Application/exceptions.php | 30 ++++++++++++++++++++++++++++++ 6 files changed, 48 insertions(+), 16 deletions(-) diff --git a/src/Application/Application.php b/src/Application/Application.php index c792e1d5b..87f0b7a98 100644 --- a/src/Application/Application.php +++ b/src/Application/Application.php @@ -106,7 +106,7 @@ public function createInitialRequest(): Request { $request = $this->router->match($this->httpRequest); if (!$request) { - throw new BadRequestException('No route for HTTP request.'); + throw new RejectRequestException('No route for HTTP request.', RejectRequestException::NO_ROUTE); } return $request; } @@ -123,13 +123,13 @@ public function processRequest(Request $request): void $this->onRequest($this, $request); if (!$request->isMethod($request::FORWARD) && !strcasecmp($request->getPresenterName(), (string) $this->errorPresenter)) { - throw new BadRequestException('Invalid request. Presenter is not achievable.'); + throw new RejectRequestException('Invalid request. Presenter is not achievable.', RejectRequestException::WRONG_PRESENTER); } try { $this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName()); } catch (InvalidPresenterException $e) { - throw count($this->requests) > 1 ? $e : new BadRequestException($e->getMessage(), 0, $e); + throw count($this->requests) > 1 ? $e : new RejectRequestException($e->getMessage(), RejectRequestException::WRONG_PRESENTER, $e); } $this->onPresenter($this, $this->presenter); $response = $this->presenter->run(clone $request); diff --git a/src/Application/MicroPresenter.php b/src/Application/MicroPresenter.php index 80dd13e45..17223bee2 100644 --- a/src/Application/MicroPresenter.php +++ b/src/Application/MicroPresenter.php @@ -83,7 +83,7 @@ public function run(Application\Request $request): Application\IResponse try { $params = Application\UI\ComponentReflection::combineArgs($reflection, $params); } catch (Nette\InvalidArgumentException $e) { - $this->error($e->getMessage()); + throw new Application\RejectRequestException($e->getMessage(), Application\RejectRequestException::WRONG_ARGUMENT); } $response = $callback(...array_values($params)); diff --git a/src/Application/UI/Component.php b/src/Application/UI/Component.php index 5c3aa0153..15241078c 100644 --- a/src/Application/UI/Component.php +++ b/src/Application/UI/Component.php @@ -10,6 +10,7 @@ namespace Nette\Application\UI; use Nette; +use Nette\Application\RejectRequestException; /** @@ -85,7 +86,7 @@ protected function tryCall(string $method, array $params): bool try { $args = $rc->combineArgs($rm, $params); } catch (Nette\InvalidArgumentException $e) { - throw new Nette\Application\BadRequestException($e->getMessage()); + throw new RejectRequestException($e->getMessage(), RejectRequestException::WRONG_ARGUMENT); } $rm->invokeArgs($this, $args); return TRUE; @@ -125,13 +126,13 @@ public function loadState(array $params): void if (isset($params[$name])) { // NULLs are ignored $type = gettype($meta['def']); if (!$reflection->convertType($params[$name], $type)) { - throw new Nette\Application\BadRequestException(sprintf( + throw new RejectRequestException(sprintf( "Value passed to persistent parameter '%s' in %s must be %s, %s given.", $name, $this instanceof Presenter ? 'presenter ' . $this->getName() : "component '{$this->getUniqueId()}'", $type === 'NULL' ? 'scalar' : $type, is_object($params[$name]) ? get_class($params[$name]) : gettype($params[$name]) - )); + ), RejectRequestException::WRONG_ARGUMENT); } $this->$name = $params[$name]; } else { @@ -243,13 +244,13 @@ public static function getPersistentParams(): array /** * Calls signal handler method. - * @throws BadSignalException if there is not handler method + * @throws RejectRequestException if there is not handler method */ public function signalReceived(string $signal): void { if (!$this->tryCall($this->formatSignalMethod($signal), $this->params)) { $class = get_class($this); - throw new BadSignalException("There is no handler for signal '$signal' in class $class."); + throw new RejectRequestException("There is no handler for signal '$signal' in class $class.", RejectRequestException::WRONG_SIGNAL); } } diff --git a/src/Application/UI/Form.php b/src/Application/UI/Form.php index 56fe36360..9ec9becea 100644 --- a/src/Application/UI/Form.php +++ b/src/Application/UI/Form.php @@ -135,7 +135,7 @@ public function signalReceived(string $signal): void } } else { $class = get_class($this); - throw new BadSignalException("Missing handler for signal '$signal' in $class."); + throw new Nette\Application\RejectRequestException("Missing handler for signal '$signal' in $class.", Nette\Application\RejectRequestException::WRONG_SIGNAL); } } diff --git a/src/Application/UI/Presenter.php b/src/Application/UI/Presenter.php index 4f0f93651..07f7f7e98 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -13,6 +13,7 @@ use Nette\Application; use Nette\Application\Responses; use Nette\Application\Helpers; +use Nette\Application\RejectRequestException; use Nette\Http; @@ -297,7 +298,7 @@ public function checkRequirements($element): void /** - * @throws BadSignalException + * @throws RejectRequestException */ public function processSignal(): void { @@ -307,10 +308,10 @@ public function processSignal(): void $component = $this->signalReceiver === '' ? $this : $this->getComponent($this->signalReceiver, FALSE); if ($component === NULL) { - throw new BadSignalException("The signal receiver component '$this->signalReceiver' is not found."); + throw new RejectRequestException("The signal receiver component '$this->signalReceiver' is not found.", RejectRequestException::WRONG_SIGNAL); } elseif (!$component instanceof ISignalReceiver) { - throw new BadSignalException("The signal receiver component '$this->signalReceiver' is not ISignalReceiver implementor."); + throw new RejectRequestException("The signal receiver component '$this->signalReceiver' is not ISignalReceiver implementor.", RejectRequestException::WRONG_SIGNAL); } $component->signalReceived($this->signal); @@ -1140,7 +1141,7 @@ protected function saveGlobalState(): void /** * Initializes $this->globalParams, $this->signal & $this->signalReceiver, $this->action, $this->view. Called by run(). - * @throws Nette\Application\BadRequestException if action name is not valid + * @throws RejectRequestException if action name is not valid */ private function initGlobalParameters(): void { @@ -1171,7 +1172,7 @@ private function initGlobalParameters(): void // init & validate $this->action & $this->view $action = $selfParams[self::ACTION_KEY] ?? self::DEFAULT_ACTION; if (!is_string($action) || !Nette\Utils\Strings::match($action, '#^[a-zA-Z0-9][a-zA-Z0-9_\x7f-\xff]*\z#')) { - $this->error('Action name is not valid.'); + throw new RejectRequestException('Action name is not valid.', RejectRequestException::WRONG_ARGUMENT); } $this->changeAction($action); @@ -1180,7 +1181,7 @@ private function initGlobalParameters(): void if (isset($selfParams[self::SIGNAL_KEY])) { $param = $selfParams[self::SIGNAL_KEY]; if (!is_string($param)) { - $this->error('Signal name is not string.'); + throw new RejectRequestException('Signal name is not string.', RejectRequestException::WRONG_ARGUMENT); } $pos = strrpos($param, '-'); if ($pos) { diff --git a/src/Application/exceptions.php b/src/Application/exceptions.php index 872912966..66c981216 100644 --- a/src/Application/exceptions.php +++ b/src/Application/exceptions.php @@ -69,3 +69,33 @@ class ForbiddenRequestException extends BadRequestException protected $code = Http\IResponse::S403_FORBIDDEN; } + + +/** + * The exception that indicates request rejected by framework. + */ +class RejectRequestException extends UI\BadSignalException +{ + const NO_ROUTE = 1; + const WRONG_PRESENTER = 2; + const WRONG_SIGNAL = 3; + const WRONG_ARGUMENT = 4; + + /** @var int */ + private $reason; + + public function __construct($message, $reason, \Exception $previous = NULL) + { + parent::__construct($message, Http\IResponse::S404_NOT_FOUND, $previous); + } + + + /** + * @return int + */ + public function getReason() + { + return $this->reason; + } + +}