Forgot to catch exceptions in static routes

This commit is contained in:
Phil 2015-01-01 20:38:57 +00:00
parent 451a1c0576
commit b6ec262d0e
1 changed files with 15 additions and 8 deletions

View File

@ -35,6 +35,19 @@ class Router implements HandlerInterface
$this->errorHandlers = array(); $this->errorHandlers = array();
} }
private function tryResponse($handler, $request, $args)
{
$response = null;
try {
$response = $handler->getResponse($request, $args);
} catch (HttpException $e) {
$response = new Response();
$response->setStatusCode($e->getCode());
$response->setBody($e->getMessage());
}
return $response;
}
/** /**
* Return the response built by the handler based on the request * Return the response built by the handler based on the request
* *
@ -47,19 +60,13 @@ class Router implements HandlerInterface
$path = $request->getPath(); $path = $request->getPath();
if (array_key_exists($path, $this->routes)) { if (array_key_exists($path, $this->routes)) {
$handler = new $this->routes[$path](); $handler = new $this->routes[$path]();
$response = $handler->getResponse($request, $args); $response = tryResponse($handler, $request, $args);
} else { } else {
foreach ($this->routes as $path => $route) { foreach ($this->routes as $path => $route) {
// Only take elements that are not $path => $handler mapped. // Only take elements that are not $path => $handler mapped.
if (is_int($path)) { if (is_int($path)) {
/** @var HandlerInterface $route */ /** @var HandlerInterface $route */
try { $response = $this->tryResponse($route, $request, $args);
$response = $route->getResponse($request, $args);
} catch (HttpException $e) {
$response = new Response();
$response->setStatusCode($e->getCode());
$response->setBody($e->getMessage());
}
if ($response) break; if ($response) break;
} }
} }