Skip to content
This repository has been archived by the owner on Jun 9, 2022. It is now read-only.

Commit

Permalink
Moved the catching of exceptions to much lower in the stack to allow …
Browse files Browse the repository at this point in the history
…middleware error handlers (e.g. Whoops) to fire in Preroute middleware
  • Loading branch information
Nik Barham committed Nov 17, 2017
1 parent f8cd0a5 commit da651fb
Showing 1 changed file with 43 additions and 45 deletions.
88 changes: 43 additions & 45 deletions src/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
}

Expand Down

0 comments on commit da651fb

Please sign in to comment.