Skip to content

Commit

Permalink
added RejectedRequestException, a descendant of BadRequestException t…
Browse files Browse the repository at this point in the history
…hrown by framework [WIP]
  • Loading branch information
dg committed Jan 26, 2017
1 parent 961bf3a commit dedb5e5
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function createInitialRequest()
{
$request = $this->router->match($this->httpRequest);
if (!$request instanceof Request) {
throw new BadRequestException('No route for HTTP request.');
throw new RejectedRequestException('No route for HTTP request.', RejectedRequestException::FAILED_ROUTING);
}
return $request;
}
Expand All @@ -133,13 +133,13 @@ public function processRequest(Request $request)
$this->onRequest($this, $request);

if (!$request->isMethod($request::FORWARD) && !strcasecmp($request->getPresenterName(), $this->errorPresenter)) {
throw new BadRequestException('Invalid request. Presenter is not achievable.');
throw new RejectedRequestException('Invalid request. Presenter is not achievable.', RejectedRequestException::FAILED_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 RejectedRequestException($e->getMessage(), RejectedRequestException::FAILED_PRESENTER, $e);
}
$this->onPresenter($this, $this->presenter);
$response = $this->presenter->run(clone $request);
Expand Down
9 changes: 5 additions & 4 deletions src/Application/UI/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Nette\Application\UI;

use Nette;
use Nette\Application\RejectedRequestException;


/**
Expand Down Expand Up @@ -131,13 +132,13 @@ public function loadState(array $params)
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 RejectedRequestException(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])
));
), RejectedRequestException::FAILED_ARGUMENT);
}
$this->$name = $params[$name];
} else {
Expand Down Expand Up @@ -262,13 +263,13 @@ public static function getPersistentParams()
* Calls signal handler method.
* @param string
* @return void
* @throws BadSignalException if there is not handler method
* @throws RejectedRequestException if there is not handler method
*/
public function signalReceived($signal)
{
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 RejectedRequestException("There is no handler for signal '$signal' in class $class.", RejectedRequestException::FAILED_SIGNAL);
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/Application/UI/ComponentReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace Nette\Application\UI;

use Nette;
use Nette\Application\BadRequestException;
use Nette\Application\RejectedRequestException;
use Nette\Reflection\ClassType;


Expand Down Expand Up @@ -128,13 +128,13 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, $args)
if (isset($args[$name])) {
$res[$i] = $args[$name];
if (!self::convertType($res[$i], $type, $isClass)) {
throw new BadRequestException(sprintf(
throw new RejectedRequestException(sprintf(
'Argument $%s passed to %s() must be %s, %s given.',
$name,
($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName(),
$type === 'NULL' ? 'scalar' : $type,
is_object($args[$name]) ? get_class($args[$name]) : gettype($args[$name])
));
), $isClass ? RejectedRequestException::FAILED_OBJECT_ARGUMENT : RejectedRequestException::FAILED_ARGUMENT);
}
} elseif ($param->isDefaultValueAvailable()) {
$res[$i] = $param->getDefaultValue();
Expand All @@ -143,11 +143,11 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, $args)
} elseif ($type === 'array') {
$res[$i] = [];
} else {
throw new BadRequestException(sprintf(
throw new RejectedRequestException(sprintf(
'Missing parameter $%s required by %s()',
$name,
($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName()
));
), $isClass ? RejectedRequestException::FAILED_OBJECT_ARGUMENT : RejectedRequestException::FAILED_ARGUMENT);
}
}
return $res;
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 @@ -143,7 +143,7 @@ public function signalReceived($signal)
}
} else {
$class = get_class($this);
throw new BadSignalException("Missing handler for signal '$signal' in $class.");
throw new Nette\Application\RejectedRequestException("Missing handler for signal '$signal' in $class.", Nette\Application\RejectedRequestException::FAILED_SIGNAL);
}
}

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


Expand Down Expand Up @@ -309,7 +310,7 @@ public function checkRequirements($element)

/**
* @return void
* @throws BadSignalException
* @throws RejectedRequestException
*/
public function processSignal()
{
Expand All @@ -319,10 +320,10 @@ public function processSignal()

$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 RejectedRequestException("The signal receiver component '$this->signalReceiver' is not found.", RejectedRequestException::FAILED_SIGNAL);

} elseif (!$component instanceof ISignalReceiver) {
throw new BadSignalException("The signal receiver component '$this->signalReceiver' is not ISignalReceiver implementor.");
throw new RejectedRequestException("The signal receiver component '$this->signalReceiver' is not ISignalReceiver implementor.", RejectedRequestException::FAILED_SIGNAL);
}

$component->signalReceived($this->signal);
Expand Down Expand Up @@ -393,7 +394,7 @@ public function changeAction($action)
$this->view = $action;

} else {
$this->error('Action name is not alphanumeric string.');
throw new RejectedRequestException('Action name is not alphanumeric string.', RejectedRequestException::FAILED_ARGUMENT);
}
}

Expand Down Expand Up @@ -444,7 +445,7 @@ public function setLayout($layout)

/**
* @return void
* @throws Nette\Application\BadRequestException if no template found
* @throws RejectedRequestException if no template found
* @throws Nette\Application\AbortException
*/
public function sendTemplate()
Expand Down Expand Up @@ -1215,7 +1216,7 @@ protected function saveGlobalState()
/**
* Initializes $this->globalParams, $this->signal & $this->signalReceiver, $this->action, $this->view. Called by run().
* @return void
* @throws Nette\Application\BadRequestException if action name is not valid
* @throws RejectedRequestException if action name is not valid
*/
private function initGlobalParameters()
{
Expand Down Expand Up @@ -1251,7 +1252,7 @@ private function initGlobalParameters()
if (isset($selfParams[self::SIGNAL_KEY])) {
$param = $selfParams[self::SIGNAL_KEY];
if (!is_string($param)) {
$this->error('Signal name is not string.');
throw new RejectedRequestException('Signal name is not string.', RejectedRequestException::FAILED_ARGUMENT);
}
$pos = strrpos($param, '-');
if ($pos) {
Expand Down
31 changes: 31 additions & 0 deletions src/Application/exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,34 @@ class ForbiddenRequestException extends BadRequestException
protected $code = Http\IResponse::S403_FORBIDDEN;

}


/**
* The exception that indicates request rejected by framework.
*/
class RejectedRequestException extends UI\BadSignalException
{
const FAILED_ROUTING = 1;
const FAILED_PRESENTER = 2;
const FAILED_ARGUMENT = 3;
const FAILED_OBJECT_ARGUMENT = 4;
const FAILED_SIGNAL = 5;

/** @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 dedb5e5

Please sign in to comment.