mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
[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:
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace Pecee\Handler;
|
||||
|
||||
use Pecee\Http\Request;
|
||||
use Pecee\SimpleRouter\RouterEntry;
|
||||
|
||||
abstract class ExceptionHandler {
|
||||
|
||||
abstract public function handleError(Request $request, RouterEntry $router = null, \Exception $error);
|
||||
|
||||
}
|
||||
@@ -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
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user