Browse Source

Add a new exception class for routing errors.

When routes cannot be found for reverse routing or parsing a URL
a special error is raised. This error will be used to trigger special
error pages that make it easier to understand which routes are
connected.
mark_story 11 years ago
parent
commit
ac16ff1bbc
2 changed files with 7 additions and 12 deletions
  1. 4 9
      src/Routing/Router.php
  2. 3 3
      tests/TestCase/Routing/RouterTest.php

+ 4 - 9
src/Routing/Router.php

@@ -16,8 +16,8 @@ namespace Cake\Routing;
 
 use Cake\Core\App;
 use Cake\Core\Configure;
-use Cake\Error;
 use Cake\Network\Request;
+use Cake\Routing\Error\MissingRouteException;
 use Cake\Routing\ScopedRouteCollection;
 use Cake\Routing\Route\Route;
 use Cake\Utility\Inflector;
@@ -481,7 +481,7 @@ class Router {
  *
  * @param string $url URL to be parsed
  * @return array Parsed elements from URL
- * @throws \Cake\Error\Exception When a route cannot be handled
+ * @throws \Cake\Routing\Error\MissingRouteException When a route cannot be handled
  */
 	public static function parse($url) {
 		if (!static::$initialized) {
@@ -496,8 +496,7 @@ class Router {
 				return $collection->parse($url);
 			}
 		}
-		// TODO improve this with a custom exception.
-		throw new Error\Exception('No routes match the given URL.');
+		throw new MissingRouteException($url);
 	}
 
 /**
@@ -849,11 +848,7 @@ class Router {
 			}
 		}
 
-		// TODO improve with custom exception
-		throw new Error\Exception(sprintf(
-			'Unable to find a matching route for %s',
-			var_export($url, true)
-		));
+		throw new MissingRouteException(var_export($url, true));
 	}
 
 /**

+ 3 - 3
tests/TestCase/Routing/RouterTest.php

@@ -1074,7 +1074,7 @@ class RouterTest extends TestCase {
 /**
  * Test that using invalid names causes exceptions.
  *
- * @expectedException \Cake\Error\Exception
+ * @expectedException \Cake\Routing\Error\MissingRouteException
  * @return void
  */
 	public function testNamedRouteException() {
@@ -2083,7 +2083,7 @@ class RouterTest extends TestCase {
 /**
  * Test url() works with patterns on :action
  *
- * @expectedException Cake\Error\Exception
+ * @expectedException Cake\Routing\Error\MissingRouteException
  * @return void
  */
 	public function testUrlPatterOnAction() {
@@ -2275,7 +2275,7 @@ class RouterTest extends TestCase {
 /**
  * testRegexRouteMatching method
  *
- * @expectedException Cake\Error\Exception
+ * @expectedException Cake\Routing\Error\MissingRouteException
  * @return void
  */
 	public function testRegexRouteMatchUrl() {