Skip to content

Commit 8f80e9f

Browse files
committed
Added exceptions
1 parent 5c35996 commit 8f80e9f

10 files changed

+155
-18
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace PhpRouter\Exception;
3+
4+
use PhpRouter\RouteException;
5+
6+
/**
7+
* Class RouteCallbackNotFoundException
8+
* @package PhpRouter\Exception
9+
*/
10+
class RouteCallbackNotFoundException extends RouteException
11+
{
12+
/**
13+
* @var int
14+
*/
15+
protected $code = RouteException::CALLBACK_NOT_FOUND;
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace PhpRouter\Exception;
3+
4+
use PhpRouter\RouteException;
5+
6+
/**
7+
* Class RouteInvalidDefinitionException
8+
* @package PhpRouter\Exception
9+
*/
10+
class RouteInvalidDefinitionException extends RouteException
11+
{
12+
/**
13+
* @var int
14+
*/
15+
protected $code = RouteException::INVALID_DEFINITION;
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace PhpRouter\Exception;
3+
4+
use PhpRouter\RouteException;
5+
6+
/**
7+
* Class RouteInvalidTypeException
8+
* @package PhpRouter\Exception
9+
*/
10+
class RouteInvalidTypeException extends RouteException
11+
{
12+
/**
13+
* @var int
14+
*/
15+
protected $code = RouteException::WRONG_TYPE;
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace PhpRouter\Exception;
3+
4+
use PhpRouter\RouteException;
5+
6+
/**
7+
* Class RouteNotFoundException
8+
* @package PhpRouter\Exception
9+
*/
10+
class RouteNotFoundException extends RouteException
11+
{
12+
/**
13+
* @var int
14+
*/
15+
protected $code = RouteException::ROUTE_NOT_FOUND;
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
namespace PhpRouter\Exception;
3+
4+
use PhpRouter\RouteException;
5+
6+
/**
7+
* Class RouteWrongCallbackException
8+
* @package PhpRouter\Exception
9+
*/
10+
class RouteWrongCallbackException extends RouteException
11+
{
12+
/**
13+
* @var int
14+
*/
15+
protected $code = RouteException::WRONG_CALLBACK_FORMAT;
16+
}

src/Route.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<?php
22
namespace PhpRouter;
33

4-
use Exception;
4+
use PhpRouter\Exception\RouteCallbackNotFoundException;
5+
use PhpRouter\Exception\RouteInvalidDefinitionException;
6+
use PhpRouter\Exception\RouteInvalidTypeException;
7+
use PhpRouter\Exception\RouteWrongCallbackException;
58
use ReflectionClass;
69

710
/**
@@ -15,7 +18,7 @@ class Route
1518
private $patterns = [
1619
'route' => '/^
1720
(?<method>[\|\w]+)\h+
18-
(?<path>([@\/\w]+))
21+
(?<url>([@\/\w]+))
1922
(?:\.(?<extension>\w+))?
2023
(?:\h+\[(?<type>\w+)\])?$/x',
2124
'callback' => '/^
@@ -71,7 +74,8 @@ class Route
7174
* @param array|callable $rules
7275
* @param callable|null $callback
7376
* @param array $callbackArgs
74-
* @throws Exception
77+
* @throws RouteInvalidDefinitionException
78+
* @throws RouteInvalidTypeException
7579
*/
7680
public function __construct($route, $rules, $callback = null, $callbackArgs = [])
7781
{
@@ -91,17 +95,17 @@ public function __construct($route, $rules, $callback = null, $callbackArgs = []
9195
* Parse route and save results in object properties
9296
*
9397
* @param string $route
94-
* @throws \Exception
95-
* @return void
98+
* @throws RouteInvalidDefinitionException
99+
* @throws RouteInvalidTypeException
96100
*/
97101
private function parseRoute($route)
98102
{
99103
if (!preg_match($this->patterns['route'], $route, $result)) {
100-
throw new \Exception('Wrong route format!');
104+
throw new RouteInvalidDefinitionException();
101105
}
102106

103107
$this->methods = explode('|', $result['method']);
104-
$this->url = $result['path'];
108+
$this->url = $result['url'];
105109

106110
if (!empty($result['extension'])) {
107111
$this->extension = $result['extension'];
@@ -110,7 +114,7 @@ private function parseRoute($route)
110114

111115
if (!empty($result['type'])) {
112116
if (!in_array($result['type'], $this->types)) {
113-
throw new \Exception(sprintf('Wrong type format! Allowed [%s]', implode(', ', $this->types)));
117+
throw new RouteInvalidTypeException();
114118
}
115119

116120
$this->type = $result['type'];
@@ -189,7 +193,7 @@ public function parseParams($requestUrl)
189193
* Execute specified Route - anonymous function or pointed class->method
190194
*
191195
* @return mixed
192-
* @throws Exception
196+
* @throws RouteWrongCallbackException
193197
*/
194198
public function dispatch()
195199
{
@@ -198,7 +202,7 @@ public function dispatch()
198202
} else if (preg_match($this->patterns['callback'], $this->callback, $result)) {
199203
return $this->call($result['class'], $result['method'], $result['type'], [$this->namedParams]);
200204
}
201-
throw new Exception('Wrong callback');
205+
throw new RouteWrongCallbackException();
202206
}
203207

204208
/**
@@ -209,12 +213,12 @@ public function dispatch()
209213
* @param string $type
210214
* @param array $params
211215
* @return mixed
212-
* @throws Exception
216+
* @throws RouteCallbackNotFoundException
213217
*/
214218
private function call($class, $method, $type, array $params = [])
215219
{
216220
if (!class_exists($class) || !method_exists($class, $method)) {
217-
throw new Exception('No class or method found!');
221+
throw new RouteCallbackNotFoundException();
218222
}
219223

220224
if ('->' == $type) {

src/RouteException.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
namespace PhpRouter;
3+
4+
use Exception;
5+
6+
/**
7+
* Class RouteException
8+
* @package PhpRouter
9+
*/
10+
class RouteException extends Exception
11+
{
12+
const ROUTE_NOT_FOUND = 404;
13+
const INVALID_DEFINITION = 1000;
14+
const WRONG_TYPE = 1001;
15+
const WRONG_CALLBACK_FORMAT = 1002;
16+
const CALLBACK_NOT_FOUND = 1003;
17+
18+
/**
19+
* @var array List of errors and base error descriptions
20+
*/
21+
private static $errors = [
22+
self::ROUTE_NOT_FOUND => 'Route not found',
23+
self::INVALID_DEFINITION => 'Route definition is not correct',
24+
self::WRONG_TYPE => 'Defined request type is not correct',
25+
self::WRONG_CALLBACK_FORMAT => 'Declared callback has wrong format',
26+
self::CALLBACK_NOT_FOUND => 'Defined callback does not exists',
27+
];
28+
29+
/**
30+
* RouteException constructor.
31+
* @param string $message
32+
* @param int $code
33+
* @param Exception|null $previous
34+
*/
35+
public function __construct($message = '', $code = 0, Exception $previous = null)
36+
{
37+
parent::__construct($this->getMessageByCode(), $this->getCode(), $previous);
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
private function getMessageByCode()
44+
{
45+
return !empty(self::$errors[$this->getCode()]) ? self::$errors[$this->getCode()] : '';
46+
}
47+
}

src/Router.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace PhpRouter;
33

4+
use PhpRouter\Exception\RouteNotFoundException;
5+
46
/**
57
* Class Router
68
* @package PhpRouter
@@ -31,6 +33,7 @@ public function __construct(RouteRequest $routeRequest, RouteCollection $routeCo
3133
* Run Router and dispatch requested Route
3234
*
3335
* @return mixed
36+
* @throws RouteNotFoundException
3437
*/
3538
public function run()
3639
{
@@ -44,7 +47,8 @@ public function run()
4447
$route->parseParams($this->routeRequest->getRequestUrl());
4548
return $route->dispatch();
4649
}
47-
// @todo: 404
50+
51+
throw new RouteNotFoundException();
4852
}
4953

5054
/**

tests/RouteTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public function shouldCallSpecifiedCallback()
7171
*/
7272
public function shouldThrowWrongCallbackException()
7373
{
74-
$this->setExpectedException('Exception');
74+
$this->setExpectedException('PhpRouter\Exception\RouteWrongCallbackException');
7575
(new Route('GET /test.html', 'nothingSpecial'))->dispatch();
7676
}
7777

@@ -80,7 +80,7 @@ public function shouldThrowWrongCallbackException()
8080
*/
8181
public function shouldThrowInvalidRouteException()
8282
{
83-
$this->setExpectedException('Exception');
83+
$this->setExpectedException('PhpRouter\Exception\RouteInvalidDefinitionException');
8484
new Route('/test.html', 'A->index');
8585
}
8686

@@ -89,7 +89,7 @@ public function shouldThrowInvalidRouteException()
8989
*/
9090
public function shouldThrowInvalidTypeException()
9191
{
92-
$this->setExpectedException('Exception');
92+
$this->setExpectedException('PhpRouter\Exception\RouteInvalidTypeException');
9393
new Route('GET /test.html [not_exists]', 'A->index');
9494
}
9595

@@ -98,7 +98,7 @@ public function shouldThrowInvalidTypeException()
9898
*/
9999
public function shouldThrowClassOrMethodNotFoundException()
100100
{
101-
$this->setExpectedException('Exception');
101+
$this->setExpectedException('PhpRouter\Exception\RouteCallbackNotFoundException');
102102
(new Route('GET /test/page.html', 'A->test'))->dispatch();
103103
}
104104
}

tests/RouterTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,12 @@ public function shouldMatchRouteAndDispatch()
4949
*/
5050
public function shouldCallAjaxRouteWithAjaxFalse()
5151
{
52+
$this->setExpectedException('PhpRouter\Exception\RouteNotFoundException');
53+
5254
$request = $this->getRequestMock('GET', '/ajax');
5355
$router = new Router($request, $this->collection);
5456

55-
$this->assertNull($router->run());
57+
$router->run();
5658
}
5759

5860
/**

0 commit comments

Comments
 (0)