diff --git a/src/Pecee/SimpleRouter/SimpleRouter.php b/src/Pecee/SimpleRouter/SimpleRouter.php index 60cf12e..3f4da47 100644 --- a/src/Pecee/SimpleRouter/SimpleRouter.php +++ b/src/Pecee/SimpleRouter/SimpleRouter.php @@ -425,16 +425,11 @@ class SimpleRouter */ public static function error(Closure $callback): CallbackExceptionHandler { - $routes = static::router()->getRoutes(); - $callbackHandler = new CallbackExceptionHandler($callback); - $group = new RouteGroup(); - $group->addExceptionHandler($callbackHandler); - - array_unshift($routes, $group); - - static::router()->setRoutes($routes); + static::router()->addRoute( + (new RouteGroup())->addExceptionHandler($callbackHandler) + ); return $callbackHandler; } diff --git a/tests/Pecee/SimpleRouter/RouterCallbackExceptionHandlerTest.php b/tests/Pecee/SimpleRouter/RouterCallbackExceptionHandlerTest.php index bf6514f..f86d121 100644 --- a/tests/Pecee/SimpleRouter/RouterCallbackExceptionHandlerTest.php +++ b/tests/Pecee/SimpleRouter/RouterCallbackExceptionHandlerTest.php @@ -19,10 +19,29 @@ class RouterCallbackExceptionHandlerTest extends \PHPUnit\Framework\TestCase throw new ExceptionHandlerException(); }); - TestRouter::debugNoReset('/404-url', 'get'); - TestRouter::router()->reset(); + TestRouter::debug('/404-url'); + } - $this->assertTrue(true); + public function testExceptionHandlerCallback() { + + TestRouter::group(['prefix' => null], function() { + TestRouter::get('/', function() { + return 'Hello world'; + }); + + TestRouter::get('/not-found', 'DummyController@method1'); + TestRouter::error(function(\Pecee\Http\Request $request, \Exception $exception) { + + if($exception instanceof \Pecee\SimpleRouter\Exceptions\NotFoundHttpException && $exception->getCode() === 404) { + return $request->setRewriteCallback(static function() { + return 'success'; + }); + } + }); + }); + + $result = TestRouter::debugOutput('/thisdoes-not/existssss', 'get'); + $this->assertEquals('success', $result); } } \ No newline at end of file