From 05d9e742932ae714c076659559609ca663aaf882 Mon Sep 17 00:00:00 2001 From: alexandresalome Date: Fri, 13 May 2011 16:57:17 +0200 Subject: [PATCH] [Routing] Add specific exceptions for the UrlGenerator When generating URL, thrown exceptions are InvalidArgumentException and distinction of errors is quite difficult. This modification brings different exceptions for different cases --- .../Generator/InvalidParameterException.php | 12 ++++++++++++ .../MissingMandatoryParametersException.php | 13 +++++++++++++ .../Generator/NotExistingRouteException.php | 12 ++++++++++++ .../Component/Routing/Generator/UrlGenerator.php | 9 ++++++--- .../Routing/Generator/UrlGeneratorTest.php | 8 ++++---- 5 files changed, 47 insertions(+), 7 deletions(-) create mode 100644 src/Symfony/Component/Routing/Exception/Generator/InvalidParameterException.php create mode 100644 src/Symfony/Component/Routing/Exception/Generator/MissingMandatoryParametersException.php create mode 100644 src/Symfony/Component/Routing/Exception/Generator/NotExistingRouteException.php diff --git a/src/Symfony/Component/Routing/Exception/Generator/InvalidParameterException.php b/src/Symfony/Component/Routing/Exception/Generator/InvalidParameterException.php new file mode 100644 index 0000000000..1c198375f0 --- /dev/null +++ b/src/Symfony/Component/Routing/Exception/Generator/InvalidParameterException.php @@ -0,0 +1,12 @@ + + */ +class InvalidParameterException extends \Exception +{ +} diff --git a/src/Symfony/Component/Routing/Exception/Generator/MissingMandatoryParametersException.php b/src/Symfony/Component/Routing/Exception/Generator/MissingMandatoryParametersException.php new file mode 100644 index 0000000000..9e519122f7 --- /dev/null +++ b/src/Symfony/Component/Routing/Exception/Generator/MissingMandatoryParametersException.php @@ -0,0 +1,13 @@ + + */ +class MissingMandatoryParametersException extends \Exception +{ +} diff --git a/src/Symfony/Component/Routing/Exception/Generator/NotExistingRouteException.php b/src/Symfony/Component/Routing/Exception/Generator/NotExistingRouteException.php new file mode 100644 index 0000000000..f5720a2f37 --- /dev/null +++ b/src/Symfony/Component/Routing/Exception/Generator/NotExistingRouteException.php @@ -0,0 +1,12 @@ + + */ +class NotExistingRouteException extends \Exception +{ +} diff --git a/src/Symfony/Component/Routing/Generator/UrlGenerator.php b/src/Symfony/Component/Routing/Generator/UrlGenerator.php index 0dbcc77205..0ca6954d89 100644 --- a/src/Symfony/Component/Routing/Generator/UrlGenerator.php +++ b/src/Symfony/Component/Routing/Generator/UrlGenerator.php @@ -14,6 +14,9 @@ namespace Symfony\Component\Routing\Generator; use Symfony\Component\Routing\Route; use Symfony\Component\Routing\RouteCollection; use Symfony\Component\Routing\RequestContext; +use Symfony\Component\Routing\Exception\Generator\InvalidParameterException; +use Symfony\Component\Routing\Exception\Generator\NotExistingRouteException; +use Symfony\Component\Routing\Exception\Generator\MissingMandatoryParametersException; /** * UrlGenerator generates URL based on a set of routes. @@ -74,7 +77,7 @@ class UrlGenerator implements UrlGeneratorInterface public function generate($name, array $parameters = array(), $absolute = false) { if (null === $route = $this->routes->get($name)) { - throw new \InvalidArgumentException(sprintf('Route "%s" does not exist.', $name)); + throw new NotExistingRouteException(sprintf('Route "%s" does not exist.', $name)); } if (!isset($this->cache[$name])) { @@ -97,7 +100,7 @@ class UrlGenerator implements UrlGeneratorInterface // all params must be given if ($diff = array_diff_key($variables, $tparams)) { - throw new \InvalidArgumentException(sprintf('The "%s" route has some missing mandatory parameters (%s).', $name, implode(', ', $diff))); + throw new MissingMandatoryParametersException(sprintf('The "%s" route has some missing mandatory parameters (%s).', $name, implode(', ', $diff))); } $url = ''; @@ -108,7 +111,7 @@ class UrlGenerator implements UrlGeneratorInterface if (!$isEmpty = in_array($tparams[$token[3]], array(null, '', false), true)) { // check requirement if ($tparams[$token[3]] && !preg_match('#^'.$token[2].'$#', $tparams[$token[3]])) { - throw new \InvalidArgumentException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $tparams[$token[3]])); + throw new InvalidParameterException(sprintf('Parameter "%s" for route "%s" must match "%s" ("%s" given).', $token[3], $name, $token[2], $tparams[$token[3]])); } } diff --git a/tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php b/tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php index 5d52d286f5..d1c24834e2 100644 --- a/tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php +++ b/tests/Symfony/Tests/Component/Routing/Generator/UrlGeneratorTest.php @@ -131,7 +131,7 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Routing\Exception\Generator\NotExistingRouteException */ public function testGenerateWithoutRoutes() { @@ -140,7 +140,7 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Routing\Exception\Generator\MissingMandatoryParametersException */ public function testGenerateForRouteWithoutManditoryParameter() { @@ -149,7 +149,7 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Routing\Exception\Generator\InvalidParameterException */ public function testGenerateForRouteWithInvalidOptionalParameter() { @@ -158,7 +158,7 @@ class UrlGeneratorTest extends \PHPUnit_Framework_TestCase } /** - * @expectedException \InvalidArgumentException + * @expectedException Symfony\Component\Routing\Exception\Generator\InvalidParameterException */ public function testGenerateForRouteWithInvalidManditoryParameter() {