Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added RejectedRequestException #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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);
}
}

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 @@ -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);
}
}
}
13 changes: 7 additions & 6 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Nette;
use Nette\Application;
use Nette\Application\Helpers;
use Nette\Application\RejectRequestException;
use Nette\Application\Responses;
use Nette\Http;

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


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

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