Skip to content

Commit c9678d0

Browse files
adrienlucasnicolas-grekas
authored andcommitted
Enrich Router's MissingMandatoryParametersException
1 parent b6e48b1 commit c9678d0

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
6.1
55
---
66

7+
* Add `getMissingParameters` and `getRouteName` methods on `MissingMandatoryParametersException`
78
* Allow using UTF-8 parameter names
89

910
5.3

Exception/MissingMandatoryParametersException.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,39 @@
1919
*/
2020
class MissingMandatoryParametersException extends \InvalidArgumentException implements ExceptionInterface
2121
{
22+
private string $routeName = '';
23+
private array $missingParameters = [];
24+
25+
/**
26+
* @param string[] $missingParameters
27+
* @param int $code
28+
*/
29+
public function __construct(string $routeName = '', $missingParameters = null, $code = 0, \Throwable $previous = null)
30+
{
31+
if (\is_array($missingParameters)) {
32+
$this->routeName = $routeName;
33+
$this->missingParameters = $missingParameters;
34+
$message = sprintf('Some mandatory parameters are missing ("%s") to generate a URL for route "%s".', implode('", "', $missingParameters), $routeName);
35+
} else {
36+
trigger_deprecation('symfony/routing', '6.1', 'Construction of "%s" with an exception message is deprecated, provide the route name and an array of missing parameters instead.', __CLASS__);
37+
$message = $routeName;
38+
$previous = $code instanceof \Throwable ? $code : null;
39+
$code = (int) $missingParameters;
40+
}
41+
42+
parent::__construct($message, $code, $previous);
43+
}
44+
45+
/**
46+
* @return string[]
47+
*/
48+
public function getMissingParameters(): array
49+
{
50+
return $this->missingParameters;
51+
}
52+
53+
public function getRouteName(): string
54+
{
55+
return $this->routeName;
56+
}
2257
}

Generator/UrlGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ protected function doGenerate(array $variables, array $defaults, array $requirem
171171

172172
// all params must be given
173173
if ($diff = array_diff_key($variables, $mergedParams)) {
174-
throw new MissingMandatoryParametersException(sprintf('Some mandatory parameters are missing ("%s") to generate a URL for route "%s".', implode('", "', array_keys($diff)), $name));
174+
throw new MissingMandatoryParametersException($name, array_keys($diff));
175175
}
176176

177177
$url = '';

Tests/Generator/UrlGeneratorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,9 +321,33 @@ public function testGenerateWithInvalidLocale()
321321
$generator->generate($name);
322322
}
323323

324+
/**
325+
* @group legacy
326+
*/
327+
public function testLegacyThrowingMissingMandatoryParameters()
328+
{
329+
$this->expectDeprecation('Since symfony/routing 6.1: Construction of "Symfony\Component\Routing\Exception\MissingMandatoryParametersException" with an exception message is deprecated, provide the route name and an array of missing parameters instead.');
330+
331+
$exception = new MissingMandatoryParametersException('expected legacy message');
332+
$this->assertSame('expected legacy message', $exception->getMessage());
333+
}
334+
335+
/**
336+
* @group legacy
337+
*/
338+
public function testLegacyThrowingMissingMandatoryParametersWithAllParameters()
339+
{
340+
$this->expectDeprecation('Since symfony/routing 6.1: Construction of "Symfony\Component\Routing\Exception\MissingMandatoryParametersException" with an exception message is deprecated, provide the route name and an array of missing parameters instead.');
341+
342+
$exception = new MissingMandatoryParametersException('expected legacy message', 256, new \Exception());
343+
$this->assertSame('expected legacy message', $exception->getMessage());
344+
$this->assertInstanceOf(\Exception::class, $exception->getPrevious());
345+
}
346+
324347
public function testGenerateForRouteWithoutMandatoryParameter()
325348
{
326349
$this->expectException(MissingMandatoryParametersException::class);
350+
$this->expectExceptionMessage('Some mandatory parameters are missing ("foo") to generate a URL for route "test".');
327351
$routes = $this->getRoutes('test', new Route('/testing/{foo}'));
328352
$this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
329353
}
@@ -554,6 +578,7 @@ public function testImportantVariable()
554578
public function testImportantVariableWithNoDefault()
555579
{
556580
$this->expectException(MissingMandatoryParametersException::class);
581+
$this->expectExceptionMessage('Some mandatory parameters are missing ("_format") to generate a URL for route "test".');
557582
$routes = $this->getRoutes('test', new Route('/{page}.{!_format}'));
558583
$generator = $this->getGenerator($routes);
559584

0 commit comments

Comments
 (0)