diff --git a/src/Application/Application.php b/src/Application/Application.php index cddd13896..41d3a05a4 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 0a52e537e..ceae3ab74 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 96a1a15b0..f7fb25614 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 { @@ -212,13 +213,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 eb73ed2ec..f48722d73 100644 --- a/src/Application/UI/Form.php +++ b/src/Application/UI/Form.php @@ -136,7 +136,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 048439221..f135f823d 100644 --- a/src/Application/UI/Presenter.php +++ b/src/Application/UI/Presenter.php @@ -12,6 +12,7 @@ use Nette; use Nette\Application; use Nette\Application\Helpers; +use Nette\Application\RejectRequestException; use Nette\Application\Responses; use Nette\Http; @@ -303,7 +304,7 @@ public function checkRequirements($element): void /** - * @throws BadSignalException + * @throws RejectRequestException */ public function processSignal(): void { @@ -313,10 +314,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); @@ -1161,7 +1162,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 { @@ -1192,7 +1193,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); @@ -1201,7 +1202,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 3f0717bca..1114027b7 100644 --- a/src/Application/exceptions.php +++ b/src/Application/exceptions.php @@ -67,3 +67,33 @@ class ForbiddenRequestException extends BadRequestException /** @var int */ protected $code = Http\IResponse::S403_FORBIDDEN; } + + +/** + * The exception that indicates request rejected by framework. + */ +class RejectRequestException extends UI\BadSignalException +{ + public const NO_ROUTE = 1; + public const WRONG_PRESENTER = 2; + public const WRONG_SIGNAL = 3; + public 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; + } +}