瀏覽代碼

[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
alexandresalome 14 年之前
父節點
當前提交
05d9e74293

+ 12 - 0
src/Symfony/Component/Routing/Exception/Generator/InvalidParameterException.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace Symfony\Component\Routing\Exception\Generator;
+
+/**
+ * Exception thrown when a parameter is not valid
+ *
+ * @author Alexandre Salomé <alexandre.salome@gmail.com>
+ */
+class InvalidParameterException extends \Exception
+{
+}

+ 13 - 0
src/Symfony/Component/Routing/Exception/Generator/MissingMandatoryParametersException.php

@@ -0,0 +1,13 @@
+<?php
+
+namespace Symfony\Component\Routing\Exception\Generator;
+
+/**
+ * Exception thrown when a route cannot be generated because of missing
+ * mandatory parameters.
+ *
+ * @author Alexandre Salomé <alexandre.salome@gmail.com>
+ */
+class MissingMandatoryParametersException extends \Exception
+{
+}

+ 12 - 0
src/Symfony/Component/Routing/Exception/Generator/NotExistingRouteException.php

@@ -0,0 +1,12 @@
+<?php
+
+namespace Symfony\Component\Routing\Exception\Generator;
+
+/**
+ * Exception thrown when a route does not exists
+ *
+ * @author Alexandre Salomé <alexandre.salome@gmail.com>
+ */
+class NotExistingRouteException extends \Exception
+{
+}

+ 6 - 3
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]]));
                         }
                     }
 

+ 4 - 4
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()
     {