Skip to content

Commit d186075

Browse files
committed
ComponentReflection::combineArgs() throws Nette\InvalidArgumentException and is converted to BadRequestException on higher level
Also reverts "ComponentReflection::combineArgs() throws InvalidArgumentException instead BadRequestException when incompatible type is object" 212ec43.
1 parent 5c60e91 commit d186075

5 files changed

+67
-61
lines changed

src/Application/MicroPresenter.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,11 @@ public function run(Application\Request $request): Application\IResponse
8080
}
8181
}
8282
$params['presenter'] = $this;
83-
$params = Application\UI\ComponentReflection::combineArgs($reflection, $params);
83+
try {
84+
$params = Application\UI\ComponentReflection::combineArgs($reflection, $params);
85+
} catch (Nette\InvalidArgumentException $e) {
86+
$this->error($e->getMessage());
87+
}
8488

8589
$response = $callback(...array_values($params));
8690

src/Application/UI/Component.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ protected function tryCall(string $method, array $params): bool
8282
$rm = $rc->getMethod($method);
8383
if ($rm->isPublic() && !$rm->isAbstract() && !$rm->isStatic()) {
8484
$this->checkRequirements($rm);
85-
$rm->invokeArgs($this, $rc->combineArgs($rm, $params));
85+
try {
86+
$args = $rc->combineArgs($rm, $params);
87+
} catch (Nette\InvalidArgumentException $e) {
88+
throw new Nette\Application\BadRequestException($e->getMessage());
89+
}
90+
$rm->invokeArgs($this, $args);
8691
return TRUE;
8792
}
8893
}

src/Application/UI/ComponentReflection.php

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

1212
use Nette;
13-
use Nette\Application\BadRequestException;
1413

1514

1615
/**
@@ -116,7 +115,7 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, $args):
116115
if (isset($args[$name])) {
117116
$res[$i] = $args[$name];
118117
if (!self::convertType($res[$i], $type, $isClass)) {
119-
throw new BadRequestException(sprintf(
118+
throw new Nette\InvalidArgumentException(sprintf(
120119
'Argument $%s passed to %s() must be %s, %s given.',
121120
$name,
122121
($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName(),
@@ -131,7 +130,7 @@ public static function combineArgs(\ReflectionFunctionAbstract $method, $args):
131130
} elseif ($type === 'array') {
132131
$res[$i] = [];
133132
} else {
134-
throw new BadRequestException(sprintf(
133+
throw new Nette\InvalidArgumentException(sprintf(
135134
'Missing parameter $%s required by %s()',
136135
$name,
137136
($method instanceof \ReflectionMethod ? $method->getDeclaringClass()->getName() . '::' : '') . $method->getName()

tests/UI/ComponentReflection.combineArgs.php7.phpt

+27-28
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
declare(strict_types=1);
88

99
use Nette\Application\UI\ComponentReflection as Reflection;
10-
use Nette\Application\BadRequestException;
1110
use Tester\Assert;
1211

1312
require __DIR__ . '/../bootstrap.php';
@@ -54,7 +53,7 @@ test(function () {
5453

5554
Assert::exception(function () use ($method) {
5655
Reflection::combineArgs($method, ['int' => []]);
57-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::params() must be scalar, array given.');
56+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::params() must be scalar, array given.');
5857
});
5958

6059

@@ -66,31 +65,31 @@ test(function () {
6665

6766
Assert::exception(function () use ($method) {
6867
Reflection::combineArgs($method, []);
69-
}, BadRequestException::class, 'Missing parameter $int required by MyPresenter::hints()');
68+
}, Nette\InvalidArgumentException::class, 'Missing parameter $int required by MyPresenter::hints()');
7069

7170
Assert::exception(function () use ($method) {
7271
Reflection::combineArgs($method, ['int' => '']);
73-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hints() must be int, string given.');
72+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hints() must be int, string given.');
7473

7574
Assert::exception(function () use ($method) {
7675
Reflection::combineArgs($method, ['int' => NULL]);
77-
}, BadRequestException::class, 'Missing parameter $int required by MyPresenter::hints()');
76+
}, Nette\InvalidArgumentException::class, 'Missing parameter $int required by MyPresenter::hints()');
7877

7978
Assert::exception(function () use ($method) {
8079
Reflection::combineArgs($method, ['int' => new stdClass]);
81-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hints() must be int, stdClass given.');
80+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hints() must be int, stdClass given.');
8281

8382
Assert::exception(function () use ($method) {
8483
Reflection::combineArgs($method, ['int' => []]);
85-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hints() must be int, array given.');
84+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hints() must be int, array given.');
8685

8786
Assert::exception(function () use ($method) {
8887
Reflection::combineArgs($method, ['int' => '1', 'bool' => '']);
89-
}, BadRequestException::class, 'Argument $bool passed to MyPresenter::hints() must be bool, string given.');
88+
}, Nette\InvalidArgumentException::class, 'Argument $bool passed to MyPresenter::hints() must be bool, string given.');
9089

9190
Assert::exception(function () use ($method) {
9291
Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => '', 'arr' => '']);
93-
}, BadRequestException::class, 'Argument $arr passed to MyPresenter::hints() must be array, string given.');
92+
}, Nette\InvalidArgumentException::class, 'Argument $arr passed to MyPresenter::hints() must be array, string given.');
9493
});
9594

9695

@@ -104,23 +103,23 @@ test(function () {
104103

105104
Assert::exception(function () use ($method) {
106105
Reflection::combineArgs($method, ['int' => '']);
107-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, string given.');
106+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, string given.');
108107

109108
Assert::exception(function () use ($method) {
110109
Reflection::combineArgs($method, ['int' => new stdClass]);
111-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, stdClass given.');
110+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, stdClass given.');
112111

113112
Assert::exception(function () use ($method) {
114113
Reflection::combineArgs($method, ['int' => []]);
115-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, array given.');
114+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hintsNulls() must be int, array given.');
116115

117116
Assert::exception(function () use ($method) {
118117
Reflection::combineArgs($method, ['int' => '1', 'bool' => '']);
119-
}, BadRequestException::class, 'Argument $bool passed to MyPresenter::hintsNulls() must be bool, string given.');
118+
}, Nette\InvalidArgumentException::class, 'Argument $bool passed to MyPresenter::hintsNulls() must be bool, string given.');
120119

121120
Assert::exception(function () use ($method) {
122121
Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => '', 'arr' => '']);
123-
}, BadRequestException::class, 'Argument $arr passed to MyPresenter::hintsNulls() must be array, string given.');
122+
}, Nette\InvalidArgumentException::class, 'Argument $arr passed to MyPresenter::hintsNulls() must be array, string given.');
124123
});
125124

126125

@@ -134,23 +133,23 @@ test(function () {
134133

135134
Assert::exception(function () use ($method) {
136135
Reflection::combineArgs($method, ['int' => '']);
137-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, string given.');
136+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, string given.');
138137

139138
Assert::exception(function () use ($method) {
140139
Reflection::combineArgs($method, ['int' => new stdClass]);
141-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, stdClass given.');
140+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, stdClass given.');
142141

143142
Assert::exception(function () use ($method) {
144143
Reflection::combineArgs($method, ['int' => []]);
145-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, array given.');
144+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::hintsDefaults() must be int, array given.');
146145

147146
Assert::exception(function () use ($method) {
148147
Reflection::combineArgs($method, ['int' => '1', 'bool' => '']);
149-
}, BadRequestException::class, 'Argument $bool passed to MyPresenter::hintsDefaults() must be bool, string given.');
148+
}, Nette\InvalidArgumentException::class, 'Argument $bool passed to MyPresenter::hintsDefaults() must be bool, string given.');
150149

151150
Assert::exception(function () use ($method) {
152151
Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => '', 'arr' => '']);
153-
}, BadRequestException::class, 'Argument $arr passed to MyPresenter::hintsDefaults() must be array, string given.');
152+
}, Nette\InvalidArgumentException::class, 'Argument $arr passed to MyPresenter::hintsDefaults() must be array, string given.');
154153
});
155154

156155

@@ -164,23 +163,23 @@ test(function () {
164163

165164
Assert::exception(function () use ($method) {
166165
Reflection::combineArgs($method, ['int' => '']);
167-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, string given.');
166+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, string given.');
168167

169168
Assert::exception(function () use ($method) {
170169
Reflection::combineArgs($method, ['int' => new stdClass]);
171-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, stdClass given.');
170+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, stdClass given.');
172171

173172
Assert::exception(function () use ($method) {
174173
Reflection::combineArgs($method, ['int' => []]);
175-
}, BadRequestException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, array given.');
174+
}, Nette\InvalidArgumentException::class, 'Argument $int passed to MyPresenter::defaults() must be integer, array given.');
176175

177176
Assert::exception(function () use ($method) {
178177
Reflection::combineArgs($method, ['int' => '1', 'bool' => '']);
179-
}, BadRequestException::class, 'Argument $bool passed to MyPresenter::defaults() must be boolean, string given.');
178+
}, Nette\InvalidArgumentException::class, 'Argument $bool passed to MyPresenter::defaults() must be boolean, string given.');
180179

181180
Assert::exception(function () use ($method) {
182181
Reflection::combineArgs($method, ['int' => '1', 'bool' => '1', 'str' => '', 'arr' => '']);
183-
}, BadRequestException::class, 'Argument $arr passed to MyPresenter::defaults() must be array, string given.');
182+
}, Nette\InvalidArgumentException::class, 'Argument $arr passed to MyPresenter::defaults() must be array, string given.');
184183
});
185184

186185

@@ -191,17 +190,17 @@ test(function () {
191190

192191
Assert::exception(function () use ($method) {
193192
Reflection::combineArgs($method, []);
194-
}, BadRequestException::class, 'Missing parameter $req required by MyPresenter::objects()');
193+
}, Nette\InvalidArgumentException::class, 'Missing parameter $req required by MyPresenter::objects()');
195194

196195
Assert::exception(function () use ($method) {
197196
Reflection::combineArgs($method, ['req' => NULL, 'opt' => NULL]);
198-
}, BadRequestException::class, 'Missing parameter $req required by MyPresenter::objects()');
197+
}, Nette\InvalidArgumentException::class, 'Missing parameter $req required by MyPresenter::objects()');
199198

200199
Assert::exception(function () use ($method) {
201200
Reflection::combineArgs($method, ['req' => $method, 'opt' => NULL]);
202-
}, BadRequestException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, ReflectionMethod given.');
201+
}, Nette\InvalidArgumentException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, ReflectionMethod given.');
203202

204203
Assert::exception(function () use ($method) {
205204
Reflection::combineArgs($method, ['req' => [], 'opt' => NULL]);
206-
}, BadRequestException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, array given.');
205+
}, Nette\InvalidArgumentException::class, 'Argument $req passed to MyPresenter::objects() must be stdClass, array given.');
207206
});

0 commit comments

Comments
 (0)