Skip to content

Commit

Permalink
added RejectRequestException, a descendant of BadRequestException thr…
Browse files Browse the repository at this point in the history
…own by framework [WIP]
  • Loading branch information
dg committed Jan 27, 2017
1 parent bc77af0 commit 30868d2
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Application/MicroPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
11 changes: 6 additions & 5 deletions src/Application/UI/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Nette\Application\UI;

use Nette;
use Nette\Application\RejectRequestException;


/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/Application/UI/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
13 changes: 7 additions & 6 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Nette\Application;
use Nette\Application\Responses;
use Nette\Application\Helpers;
use Nette\Application\RejectRequestException;
use Nette\Http;


Expand Down Expand Up @@ -297,7 +298,7 @@ public function checkRequirements($element): void


/**
* @throws BadSignalException
* @throws RejectRequestException
*/
public function processSignal(): void
{
Expand All @@ -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);
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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);

Expand All @@ -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) {
Expand Down
30 changes: 30 additions & 0 deletions src/Application/exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

0 comments on commit 30868d2

Please sign in to comment.