[FEATURE] Added ExceptionHandling functionality + tests.

- Somehow the ExceptionHandling functionality never really get implemented and/or got lost in translation, it's back whup!
- Added some basic tests for Middleware + routing.
This commit is contained in:
Simon Sessingø
2016-04-09 07:05:49 +02:00
parent 2f2c3ca3ca
commit fc4fd0edf1
9 changed files with 183 additions and 23 deletions
+21 -2
View File
@@ -2,6 +2,7 @@
namespace Pecee\SimpleRouter;
use Pecee\Exception\RouterException;
use Pecee\Handler\ExceptionHandler;
use Pecee\Http\Middleware\BaseCsrfVerifier;
use Pecee\Http\Request;
@@ -158,13 +159,18 @@ class RouterBase {
$this->request->loadedRoute = $route;
$route->loadMiddleware($this->request);
$this->request->loadedRoute->renderRoute($this->request);
try {
$this->request->loadedRoute->renderRoute($this->request);
} catch(\Exception $e) {
$this->handleException($e);
}
break;
}
}
if($routeNotAllowed) {
throw new RouterException('Route or method not allowed', 403);
$this->handleException(new RouterException('Route or method not allowed', 403));
}
if(!$this->request->loadedRoute) {
@@ -172,6 +178,19 @@ class RouterBase {
}
}
protected function handleException(\Exception $e) {
if($this->request->loadedRoute !== null && $this->request->loadedRoute->exceptionHandler !== null) {
$handler = new $this->request->loadedRoute->exceptionHandler();
if(!($handler instanceof ExceptionHandler)) {
throw new RouterException('Exception handler must be instanceof ExceptionHandler.');
}
$handler->handleError($this->request, $this->request->loadedRoute, $e);
}
throw $e;
}
/**
* @return string
*/