From da651fb8fc70e28b76b12efb0dc58edbfb820e17 Mon Sep 17 00:00:00 2001 From: Nik Barham Date: Fri, 17 Nov 2017 15:28:35 +0000 Subject: [PATCH] Moved the catching of exceptions to much lower in the stack to allow middleware error handlers (e.g. Whoops) to fire in Preroute middleware --- src/Router.php | 88 ++++++++++++++++++++++++-------------------------- 1 file changed, 43 insertions(+), 45 deletions(-) diff --git a/src/Router.php b/src/Router.php index 446a3d9..dcb8910 100644 --- a/src/Router.php +++ b/src/Router.php @@ -122,7 +122,13 @@ public function run(Request $request) $starttime = $request->server->get('REQUEST_TIME_FLOAT'); $this->log("Router: ->run() called. Starting clock at REQUEST_TIME+%.2fms", microtime(true) - $starttime); - $response = $this->process($request); + try { + $response = $this->process($request); + } catch (\Throwable $e) { + $this->log("Router: Exception"); + $response = $this->handleException($e, $request); + } + $this->log("Router: Preparing to send response"); $response->prepare($request); $response->send(); @@ -138,51 +144,43 @@ public function run(Request $request) */ public function process(Request $request) : Response { - try { - // Try and run the next middleware - $next = next($this->prerouteMiddleware); - if ($next instanceof Middleware) { - $this->log("Router: Calling Middleware: %s", get_class($next)); - $response = $next->process($request, $this); - $this->log("Router: Leaving Middleware: %s", get_class($next)); - return $response; - } elseif (is_string($next)) { - $this->log("Router: Calling Middleware: %s", $next); - $response = $this->getMiddleware($next)->process($request, $this); - $this->log("Router: Leaving Middleware: %s", $next); - return $response; - } else { - // Null byte poisoning protection - list($uri) = explode('?', str_replace(chr(0), '', $request->server->get('REQUEST_URI'))); - $dispatch = $this->dispatcher->dispatch($request->server->get('REQUEST_METHOD'), $uri); - switch ($dispatch[0]) { - case Dispatcher::NOT_FOUND: - $this->log("Router: Route not matched"); - throw new Exception\NotFound('Router: Route not matched'); - - case Dispatcher::METHOD_NOT_ALLOWED: - $this->log("Router: Method not Allowed"); - throw new Exception\MethodNotAllowed( - $dispatch[1], - 'Router: Method not Allowed: ' . $request->getMethod() - ); - - case Dispatcher::FOUND: - try { - $dispatcher = unserialize($dispatch[1]); - $this->log( - "Router: Route matched: %s@%s", - $dispatcher->controllerClass, - $dispatcher->controllerMethod - ); - return $dispatcher->startProcessing($this, $request, $dispatch[2]); - } catch (\Throwable $e) { - return $this->handleException($e, $request); - } - } + // Try and run the next middleware + $next = next($this->prerouteMiddleware); + if ($next instanceof Middleware) { + $this->log("Router: Calling Middleware: %s", get_class($next)); + $response = $next->process($request, $this); + $this->log("Router: Leaving Middleware: %s", get_class($next)); + return $response; + } elseif (is_string($next)) { + $this->log("Router: Calling Middleware: %s", $next); + $response = $this->getMiddleware($next)->process($request, $this); + $this->log("Router: Leaving Middleware: %s", $next); + return $response; + } else { + // Null byte poisoning protection + list($uri) = explode('?', str_replace(chr(0), '', $request->server->get('REQUEST_URI'))); + $dispatch = $this->dispatcher->dispatch($request->server->get('REQUEST_METHOD'), $uri); + switch ($dispatch[0]) { + case Dispatcher::NOT_FOUND: + $this->log("Router: Route not matched"); + throw new Exception\NotFound('Router: Route not matched'); + + case Dispatcher::METHOD_NOT_ALLOWED: + $this->log("Router: Method not Allowed"); + throw new Exception\MethodNotAllowed( + $dispatch[1], + 'Router: Method not Allowed: ' . $request->getMethod() + ); + + case Dispatcher::FOUND: + $dispatcher = unserialize($dispatch[1]); + $this->log( + "Router: Route matched: %s@%s", + $dispatcher->controllerClass, + $dispatcher->controllerMethod + ); + return $dispatcher->startProcessing($this, $request, $dispatch[2]); } - } catch (\Throwable $e) { - return $this->handleException($e, $request); } }