Skip to content

Commit f1f8e71

Browse files
committed
added RejectRequestException, a descendant of BadRequestException thrown by framework [WIP]
1 parent d186075 commit f1f8e71

File tree

6 files changed

+48
-16
lines changed

6 files changed

+48
-16
lines changed

src/Application/Application.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public function createInitialRequest(): Request
106106
{
107107
$request = $this->router->match($this->httpRequest);
108108
if (!$request) {
109-
throw new BadRequestException('No route for HTTP request.');
109+
throw new RejectRequestException('No route for HTTP request.', RejectRequestException::NO_ROUTE);
110110
}
111111
return $request;
112112
}
@@ -123,13 +123,13 @@ public function processRequest(Request $request): void
123123
$this->onRequest($this, $request);
124124

125125
if (!$request->isMethod($request::FORWARD) && !strcasecmp($request->getPresenterName(), (string) $this->errorPresenter)) {
126-
throw new BadRequestException('Invalid request. Presenter is not achievable.');
126+
throw new RejectRequestException('Invalid request. Presenter is not achievable.', RejectRequestException::WRONG_PRESENTER);
127127
}
128128

129129
try {
130130
$this->presenter = $this->presenterFactory->createPresenter($request->getPresenterName());
131131
} catch (InvalidPresenterException $e) {
132-
throw count($this->requests) > 1 ? $e : new BadRequestException($e->getMessage(), 0, $e);
132+
throw count($this->requests) > 1 ? $e : new RejectRequestException($e->getMessage(), RejectRequestException::WRONG_PRESENTER, $e);
133133
}
134134
$this->onPresenter($this, $this->presenter);
135135
$response = $this->presenter->run(clone $request);

src/Application/MicroPresenter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public function run(Application\Request $request): Application\IResponse
8383
try {
8484
$params = Application\UI\ComponentReflection::combineArgs($reflection, $params);
8585
} catch (Nette\InvalidArgumentException $e) {
86-
$this->error($e->getMessage());
86+
throw new Application\RejectRequestException($e->getMessage(), Application\RejectRequestException::WRONG_ARGUMENT);
8787
}
8888

8989
$response = $callback(...array_values($params));

src/Application/UI/Component.php

+6-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Application\UI;
1111

1212
use Nette;
13+
use Nette\Application\RejectRequestException;
1314

1415

1516
/**
@@ -85,7 +86,7 @@ protected function tryCall(string $method, array $params): bool
8586
try {
8687
$args = $rc->combineArgs($rm, $params);
8788
} catch (Nette\InvalidArgumentException $e) {
88-
throw new Nette\Application\BadRequestException($e->getMessage());
89+
throw new RejectRequestException($e->getMessage(), RejectRequestException::WRONG_ARGUMENT);
8990
}
9091
$rm->invokeArgs($this, $args);
9192
return TRUE;
@@ -125,13 +126,13 @@ public function loadState(array $params): void
125126
if (isset($params[$name])) { // NULLs are ignored
126127
$type = gettype($meta['def']);
127128
if (!$reflection->convertType($params[$name], $type)) {
128-
throw new Nette\Application\BadRequestException(sprintf(
129+
throw new RejectRequestException(sprintf(
129130
"Value passed to persistent parameter '%s' in %s must be %s, %s given.",
130131
$name,
131132
$this instanceof Presenter ? 'presenter ' . $this->getName() : "component '{$this->getUniqueId()}'",
132133
$type === 'NULL' ? 'scalar' : $type,
133134
is_object($params[$name]) ? get_class($params[$name]) : gettype($params[$name])
134-
));
135+
), RejectRequestException::WRONG_ARGUMENT);
135136
}
136137
$this->$name = $params[$name];
137138
} else {
@@ -243,13 +244,13 @@ public static function getPersistentParams(): array
243244

244245
/**
245246
* Calls signal handler method.
246-
* @throws BadSignalException if there is not handler method
247+
* @throws RejectRequestException if there is not handler method
247248
*/
248249
public function signalReceived(string $signal): void
249250
{
250251
if (!$this->tryCall($this->formatSignalMethod($signal), $this->params)) {
251252
$class = get_class($this);
252-
throw new BadSignalException("There is no handler for signal '$signal' in class $class.");
253+
throw new RejectRequestException("There is no handler for signal '$signal' in class $class.", RejectRequestException::WRONG_SIGNAL);
253254
}
254255
}
255256

src/Application/UI/Form.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public function signalReceived(string $signal): void
135135
}
136136
} else {
137137
$class = get_class($this);
138-
throw new BadSignalException("Missing handler for signal '$signal' in $class.");
138+
throw new Nette\Application\RejectRequestException("Missing handler for signal '$signal' in $class.", Nette\Application\RejectRequestException::WRONG_SIGNAL);
139139
}
140140
}
141141

src/Application/UI/Presenter.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Nette\Application;
1414
use Nette\Application\Responses;
1515
use Nette\Application\Helpers;
16+
use Nette\Application\RejectRequestException;
1617
use Nette\Http;
1718

1819

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

298299

299300
/**
300-
* @throws BadSignalException
301+
* @throws RejectRequestException
301302
*/
302303
public function processSignal(): void
303304
{
@@ -307,10 +308,10 @@ public function processSignal(): void
307308

308309
$component = $this->signalReceiver === '' ? $this : $this->getComponent($this->signalReceiver, FALSE);
309310
if ($component === NULL) {
310-
throw new BadSignalException("The signal receiver component '$this->signalReceiver' is not found.");
311+
throw new RejectRequestException("The signal receiver component '$this->signalReceiver' is not found.", RejectRequestException::WRONG_SIGNAL);
311312

312313
} elseif (!$component instanceof ISignalReceiver) {
313-
throw new BadSignalException("The signal receiver component '$this->signalReceiver' is not ISignalReceiver implementor.");
314+
throw new RejectRequestException("The signal receiver component '$this->signalReceiver' is not ISignalReceiver implementor.", RejectRequestException::WRONG_SIGNAL);
314315
}
315316

316317
$component->signalReceived($this->signal);
@@ -1140,7 +1141,7 @@ protected function saveGlobalState(): void
11401141

11411142
/**
11421143
* Initializes $this->globalParams, $this->signal & $this->signalReceiver, $this->action, $this->view. Called by run().
1143-
* @throws Nette\Application\BadRequestException if action name is not valid
1144+
* @throws RejectRequestException if action name is not valid
11441145
*/
11451146
private function initGlobalParameters(): void
11461147
{
@@ -1171,7 +1172,7 @@ private function initGlobalParameters(): void
11711172
// init & validate $this->action & $this->view
11721173
$action = $selfParams[self::ACTION_KEY] ?? self::DEFAULT_ACTION;
11731174
if (!is_string($action) || !Nette\Utils\Strings::match($action, '#^[a-zA-Z0-9][a-zA-Z0-9_\x7f-\xff]*\z#')) {
1174-
$this->error('Action name is not valid.');
1175+
throw new RejectRequestException('Action name is not valid.', RejectRequestException::WRONG_ARGUMENT);
11751176
}
11761177
$this->changeAction($action);
11771178

@@ -1180,7 +1181,7 @@ private function initGlobalParameters(): void
11801181
if (isset($selfParams[self::SIGNAL_KEY])) {
11811182
$param = $selfParams[self::SIGNAL_KEY];
11821183
if (!is_string($param)) {
1183-
$this->error('Signal name is not string.');
1184+
throw new RejectRequestException('Signal name is not string.', RejectRequestException::WRONG_ARGUMENT);
11841185
}
11851186
$pos = strrpos($param, '-');
11861187
if ($pos) {

src/Application/exceptions.php

+30
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,33 @@ class ForbiddenRequestException extends BadRequestException
6969
protected $code = Http\IResponse::S403_FORBIDDEN;
7070

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

0 commit comments

Comments
 (0)