Skip to content

Commit e008bbd

Browse files
committed
added RejectedRequestException, a descendant of BadRequestException thrown by framework [WIP]
1 parent 5740cde commit e008bbd

File tree

6 files changed

+53
-20
lines changed

6 files changed

+53
-20
lines changed

src/Application/Application.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public function createInitialRequest()
113113
{
114114
$request = $this->router->match($this->httpRequest);
115115
if (!$request instanceof Request) {
116-
throw new BadRequestException('No route for HTTP request.');
116+
throw new RejectedRequestException('No route for HTTP request.', RejectedRequestException::FAILED_ROUTING);
117117
}
118118
return $request;
119119
}
@@ -133,13 +133,13 @@ public function processRequest(Request $request)
133133
$this->onRequest($this, $request);
134134

135135
if (!$request->isMethod($request::FORWARD) && !strcasecmp($request->getPresenterName(), $this->errorPresenter)) {
136-
throw new BadRequestException('Invalid request. Presenter is not achievable.');
136+
throw new RejectedRequestException('Invalid request. Presenter is not achievable.', RejectedRequestException::FAILED_PRESENTER);
137137
}
138138

139139
try {
140140
$this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName());
141141
} catch (InvalidPresenterException $e) {
142-
throw count($this->requests) > 1 ? $e : new BadRequestException($e->getMessage(), 0, $e);
142+
throw count($this->requests) > 1 ? $e : new RejectedRequestException($e->getMessage(), RejectedRequestException::FAILED_PRESENTER, $e);
143143
}
144144
$this->onPresenter($this, $this->presenter);
145145
$response = $this->presenter->run(clone $request);

src/Application/UI/Component.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Nette\Application\UI;
99

1010
use Nette;
11+
use Nette\Application\RejectedRequestException;
1112

1213

1314
/**
@@ -131,13 +132,13 @@ public function loadState(array $params)
131132
if (isset($params[$name])) { // NULLs are ignored
132133
$type = gettype($meta['def']);
133134
if (!$reflection->convertType($params[$name], $type)) {
134-
throw new Nette\Application\BadRequestException(sprintf(
135+
throw new RejectedRequestException(sprintf(
135136
"Value passed to persistent parameter '%s' in %s must be %s, %s given.",
136137
$name,
137138
$this instanceof Presenter ? 'presenter ' . $this->getName() : "component '{$this->getUniqueId()}'",
138139
$type === 'NULL' ? 'scalar' : $type,
139140
is_object($params[$name]) ? get_class($params[$name]) : gettype($params[$name])
140-
));
141+
), RejectedRequestException::FAILED_ARGUMENT);
141142
}
142143
$this->$name = $params[$name];
143144
} else {
@@ -262,13 +263,13 @@ public static function getPersistentParams()
262263
* Calls signal handler method.
263264
* @param string
264265
* @return void
265-
* @throws BadSignalException if there is not handler method
266+
* @throws RejectedRequestException if there is not handler method
266267
*/
267268
public function signalReceived($signal)
268269
{
269270
if (!$this->tryCall($this->formatSignalMethod($signal), $this->params)) {
270271
$class = get_class($this);
271-
throw new BadSignalException("There is no handler for signal '$signal' in class $class.");
272+
throw new RejectedRequestException("There is no handler for signal '$signal' in class $class.", RejectedRequestException::FAILED_SIGNAL);
272273
}
273274
}
274275

src/Application/UI/ComponentReflection.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Nette\Application\UI;
99

1010
use Nette;
11-
use Nette\Application\BadRequestException;
11+
use Nette\Application\RejectedRequestException;
1212
use Nette\Reflection\ClassType;
1313

1414

@@ -128,13 +128,13 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, $args)
128128
if (isset($args[$name])) {
129129
$res[$i] = $args[$name];
130130
if (!self::convertType($res[$i], $type, $isClass)) {
131-
throw new BadRequestException(sprintf(
131+
throw new RejectedRequestException(sprintf(
132132
'Argument $%s passed to %s() must be %s, %s given.',
133133
$name,
134134
($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName(),
135135
$type === 'NULL' ? 'scalar' : $type,
136136
is_object($args[$name]) ? get_class($args[$name]) : gettype($args[$name])
137-
));
137+
), $isClass ? RejectedRequestException::FAILED_OBJECT_ARGUMENT : RejectedRequestException::FAILED_ARGUMENT);
138138
}
139139
} elseif ($param->isDefaultValueAvailable()) {
140140
$res[$i] = $param->getDefaultValue();
@@ -143,11 +143,11 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, $args)
143143
} elseif ($type === 'array') {
144144
$res[$i] = [];
145145
} else {
146-
throw new BadRequestException(sprintf(
146+
throw new RejectedRequestException(sprintf(
147147
'Missing parameter $%s required by %s()',
148148
$name,
149149
($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName()
150-
));
150+
), $isClass ? RejectedRequestException::FAILED_OBJECT_ARGUMENT : RejectedRequestException::FAILED_ARGUMENT);
151151
}
152152
}
153153
return $res;

src/Application/UI/Form.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function signalReceived($signal)
143143
}
144144
} else {
145145
$class = get_class($this);
146-
throw new BadSignalException("Missing handler for signal '$signal' in $class.");
146+
throw new Nette\Application\RejectedRequestException("Missing handler for signal '$signal' in $class.", Nette\Application\RejectedRequestException::FAILED_SIGNAL);
147147
}
148148
}
149149

src/Application/UI/Presenter.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Nette\Application;
1212
use Nette\Application\Responses;
1313
use Nette\Application\Helpers;
14+
use Nette\Application\RejectedRequestException;
1415
use Nette\Http;
1516

1617

@@ -309,7 +310,7 @@ public function checkRequirements($element)
309310

310311
/**
311312
* @return void
312-
* @throws BadSignalException
313+
* @throws RejectedRequestException
313314
*/
314315
public function processSignal()
315316
{
@@ -319,10 +320,10 @@ public function processSignal()
319320

320321
$component = $this->signalReceiver === '' ? $this : $this->getComponent($this->signalReceiver, FALSE);
321322
if ($component === NULL) {
322-
throw new BadSignalException("The signal receiver component '$this->signalReceiver' is not found.");
323+
throw new RejectedRequestException("The signal receiver component '$this->signalReceiver' is not found.", RejectedRequestException::FAILED_SIGNAL);
323324

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

328329
$component->signalReceived($this->signal);
@@ -393,7 +394,7 @@ public function changeAction($action)
393394
$this->view = $action;
394395

395396
} else {
396-
$this->error('Action name is not alphanumeric string.');
397+
throw new RejectedRequestException('Action name is not alphanumeric string.', RejectedRequestException::FAILED_ARGUMENT);
397398
}
398399
}
399400

@@ -444,7 +445,7 @@ public function setLayout($layout)
444445

445446
/**
446447
* @return void
447-
* @throws Nette\Application\BadRequestException if no template found
448+
* @throws RejectedRequestException if no template found
448449
* @throws Nette\Application\AbortException
449450
*/
450451
public function sendTemplate()
@@ -1215,7 +1216,7 @@ protected function saveGlobalState()
12151216
/**
12161217
* Initializes $this->globalParams, $this->signal & $this->signalReceiver, $this->action, $this->view. Called by run().
12171218
* @return void
1218-
* @throws Nette\Application\BadRequestException if action name is not valid
1219+
* @throws RejectedRequestException if action name is not valid
12191220
*/
12201221
private function initGlobalParameters()
12211222
{
@@ -1251,7 +1252,7 @@ private function initGlobalParameters()
12511252
if (isset($selfParams[self::SIGNAL_KEY])) {
12521253
$param = $selfParams[self::SIGNAL_KEY];
12531254
if (!is_string($param)) {
1254-
$this->error('Signal name is not string.');
1255+
throw new RejectedRequestException('Signal name is not string.', RejectedRequestException::FAILED_ARGUMENT);
12551256
}
12561257
$pos = strrpos($param, '-');
12571258
if ($pos) {

src/Application/exceptions.php

+31
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,34 @@ class ForbiddenRequestException extends BadRequestException
7070
protected $code = IResponse::S403_FORBIDDEN;
7171

7272
}
73+
74+
75+
/**
76+
* The exception that indicates request rejected by framework.
77+
*/
78+
class RejectedRequestException extends UI\BadSignalException
79+
{
80+
const FAILED_ROUTING = 1;
81+
const FAILED_PRESENTER = 2;
82+
const FAILED_ARGUMENT = 3;
83+
const FAILED_OBJECT_ARGUMENT = 4;
84+
const FAILED_SIGNAL = 5;
85+
86+
/** @var int */
87+
private $reason;
88+
89+
public function __construct($message, $reason, \Exception $previous = NULL)
90+
{
91+
parent::__construct($message, IResponse::S404_NOT_FOUND, $previous);
92+
}
93+
94+
95+
/**
96+
* @return int
97+
*/
98+
public function getReason()
99+
{
100+
return $this->reason;
101+
}
102+
103+
}

0 commit comments

Comments
 (0)