[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
This commit is contained in:
alexandresalome 2011-05-13 16:57:17 +02:00
parent 36d60a4a87
commit 05d9e74293
5 changed files with 47 additions and 7 deletions

View File

@ -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
{
}

View File

@ -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
{
}

View File

@ -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
{
}

View File

@ -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]]));
}
}

View File

@ -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()
{