Skip to content

[Routing] Enrich MissingMandatoryParametersException #45075

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
6.1
---

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

5.3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,39 @@
*/
class MissingMandatoryParametersException extends \InvalidArgumentException implements ExceptionInterface
{
private string $routeName = '';
private array $missingParameters = [];

/**
* @param string[] $missingParameters
* @param int $code
*/
public function __construct(string $routeName = '', $missingParameters = null, $code = 0, \Throwable $previous = null)
{
if (\is_array($missingParameters)) {
$this->routeName = $routeName;
$this->missingParameters = $missingParameters;
$message = sprintf('Some mandatory parameters are missing ("%s") to generate a URL for route "%s".', implode('", "', $missingParameters), $routeName);
} else {
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__);
$message = $routeName;
$previous = $code instanceof \Throwable ? $code : null;
$code = (int) $missingParameters;
}

parent::__construct($message, $code, $previous);
}

/**
* @return string[]
*/
public function getMissingParameters(): array
{
return $this->missingParameters;
}

public function getRouteName(): string
{
return $this->routeName;
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/Routing/Generator/UrlGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected function doGenerate(array $variables, array $defaults, array $requirem

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

$url = '';
Expand Down
25 changes: 25 additions & 0 deletions src/Symfony/Component/Routing/Tests/Generator/UrlGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,33 @@ public function testGenerateWithInvalidLocale()
$generator->generate($name);
}

/**
* @group legacy
*/
public function testLegacyThrowingMissingMandatoryParameters()
{
$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.');

$exception = new MissingMandatoryParametersException('expected legacy message');
$this->assertSame('expected legacy message', $exception->getMessage());
}

/**
* @group legacy
*/
public function testLegacyThrowingMissingMandatoryParametersWithAllParameters()
{
$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.');

$exception = new MissingMandatoryParametersException('expected legacy message', 256, new \Exception());
$this->assertSame('expected legacy message', $exception->getMessage());
$this->assertInstanceOf(\Exception::class, $exception->getPrevious());
}

public function testGenerateForRouteWithoutMandatoryParameter()
{
$this->expectException(MissingMandatoryParametersException::class);
$this->expectExceptionMessage('Some mandatory parameters are missing ("foo") to generate a URL for route "test".');
$routes = $this->getRoutes('test', new Route('/testing/{foo}'));
$this->getGenerator($routes)->generate('test', [], UrlGeneratorInterface::ABSOLUTE_URL);
}
Expand Down Expand Up @@ -554,6 +578,7 @@ public function testImportantVariable()
public function testImportantVariableWithNoDefault()
{
$this->expectException(MissingMandatoryParametersException::class);
$this->expectExceptionMessage('Some mandatory parameters are missing ("_format") to generate a URL for route "test".');
$routes = $this->getRoutes('test', new Route('/{page}.{!_format}'));
$generator = $this->getGenerator($routes);

Expand Down