Add error handlers to Router

Move catching HttpExceptions and translating into responses from Handler to Router
This commit is contained in:
PJ Dietz 2015-01-01 12:41:53 -05:00
parent 1f6e1f3e9c
commit 9eec436ad4
5 changed files with 171 additions and 46 deletions

View File

@ -10,7 +10,6 @@
namespace pjdietz\WellRESTed; namespace pjdietz\WellRESTed;
use pjdietz\WellRESTed\Exceptions\HttpExceptions\HttpException;
use pjdietz\WellRESTed\Interfaces\HandlerInterface; use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Interfaces\RequestInterface; use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Interfaces\ResponseInterface; use pjdietz\WellRESTed\Interfaces\ResponseInterface;
@ -46,12 +45,7 @@ abstract class Handler implements HandlerInterface
$this->request = $request; $this->request = $request;
$this->args = $args; $this->args = $args;
$this->response = new Response(); $this->response = new Response();
try { $this->buildResponse();
$this->buildResponse();
} catch (HttpException $e) {
$this->response->setStatusCode($e->getCode());
$this->response->setBody($e->getMessage());
}
return $this->response; return $this->response;
} }

View File

@ -83,6 +83,8 @@ class RouteBuilder
* ->variablePattern is passed to setDefaultVariablePattern() * ->variablePattern is passed to setDefaultVariablePattern()
* <br /><br /> * <br /><br />
* ->vars is passed to setTemplateVars() * ->vars is passed to setTemplateVars()
*
* @param object
*/ */
public function readConfiguration($data) public function readConfiguration($data)
{ {

View File

@ -10,6 +10,7 @@
namespace pjdietz\WellRESTed; namespace pjdietz\WellRESTed;
use pjdietz\WellRESTed\Exceptions\HttpExceptions\HttpException;
use pjdietz\WellRESTed\Interfaces\HandlerInterface; use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Interfaces\RequestInterface; use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Interfaces\ResponseInterface; use pjdietz\WellRESTed\Interfaces\ResponseInterface;
@ -23,7 +24,7 @@ class Router implements HandlerInterface
{ {
/** @var array Array of Route objects */ /** @var array Array of Route objects */
private $routes; private $routes;
/** @var array Array of Route objects for error handling. */ /** @var array Hash array of status code => qualified HandlerInterface names for error handling. */
private $errorHandlers; private $errorHandlers;
/** Create a new Router. */ /** Create a new Router. */
@ -44,9 +45,27 @@ class Router implements HandlerInterface
{ {
foreach ($this->routes as $route) { foreach ($this->routes as $route) {
/** @var HandlerInterface $route */ /** @var HandlerInterface $route */
$responce = $route->getResponse($request, $args); try {
if ($responce) { $response = $route->getResponse($request, $args);
return $responce; } catch (HttpException $e) {
$response = new Response();
$response->setStatusCode($e->getCode());
$response->setBody($e->getMessage());
}
if ($response) {
// Check if the router has an error handler for this status code.
$status = $response->getStatusCode();
if (array_key_exists($status, $this->errorHandlers)) {
/** @var HandlerInterface $errorHandler */
$errorHandler = new $this->errorHandlers[$status]();
// Pass the response triggering this along to the error handler.
$errorArgs = array("response" => $response);
if ($args) {
$errorArgs = array_merge($args, $errorArgs);
}
return $errorHandler->getResponse($request, $errorArgs);
}
return $response;
} }
} }
return null; return null;
@ -79,23 +98,23 @@ class Router implements HandlerInterface
/** /**
* Add a custom error handler. * Add a custom error handler.
* *
* @param integer $error The error code. * @param integer $statusCode The error status code.
* @param HandlerInterface $errorHandler The handler for the error. * @param string $errorHandler Fully qualified name to an autoloadable handler class.
*/ */
public function addErrorHandler($error, $errorHandler) public function setErrorHandler($statusCode, $errorHandler)
{ {
$this->errorHandlers[$error] = $errorHandler; $this->errorHandlers[$statusCode] = $errorHandler;
} }
/** /**
* Add custom error handlers. * Add custom error handlers.
* *
* @param array $errorHandlers An array mapping an integer error code to something implementing an HandlerInterface. * @param array $errorHandlers Array mapping integer error codes to qualified handler names.
*/ */
public function addErrorHandlers(array $errorHandlers) public function setErrorHandlers(array $errorHandlers)
{ {
foreach ($errorHandlers as $error => $errorHandler) { foreach ($errorHandlers as $statusCode => $errorHandler) {
$this->addErrorHandler($error, $errorHandler); $this->setErrorHandler($statusCode, $errorHandler);
} }
} }
@ -103,6 +122,7 @@ class Router implements HandlerInterface
* Dispatch the singleton Request through the router and output the response. * Dispatch the singleton Request through the router and output the response.
* *
* Respond with a 404 Not Found if no route provides a response. * Respond with a 404 Not Found if no route provides a response.
* @param array|null $args
*/ */
public function respond($args = null) public function respond($args = null)
{ {
@ -111,16 +131,11 @@ class Router implements HandlerInterface
if (!$response) { if (!$response) {
$response = $this->getNoRouteResponse($request); $response = $this->getNoRouteResponse($request);
} }
$status = $response->getStatusCode();
if (array_key_exists($status, $this->errorHandlers)) {
$errorHandler = new $this->errorHandlers[$status]();
$response = $errorHandler->getResponse($request, $args);
}
$response->respond(); $response->respond();
} }
/** /**
* Prepare a resonse indicating a 404 Not Found error * Prepare a response indicating a 404 Not Found error
* *
* @param RequestInterface $request * @param RequestInterface $request
* @return ResponseInterface * @return ResponseInterface

View File

@ -2,7 +2,6 @@
namespace pjdietz\WellRESTed\Test; namespace pjdietz\WellRESTed\Test;
use pjdietz\WellRESTed\Exceptions\HttpExceptions\NotFoundException;
use pjdietz\WellRESTed\Handler; use pjdietz\WellRESTed\Handler;
class HandlerTest extends \PHPUnit_Framework_TestCase class HandlerTest extends \PHPUnit_Framework_TestCase
@ -44,18 +43,6 @@ class HandlerTest extends \PHPUnit_Framework_TestCase
]; ];
} }
public function testTranslateHttpExceptionToResponse()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getMethod')
->will($this->returnValue("GET"));
$handler = new ExceptionHandler();
$resp = $handler->getResponse($mockRequest);
$this->assertEquals(404, $resp->getStatusCode());
}
public function testReadAllowedMethods() public function testReadAllowedMethods()
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
@ -71,14 +58,6 @@ class HandlerTest extends \PHPUnit_Framework_TestCase
} }
class ExceptionHandler extends Handler
{
protected function get()
{
throw new NotFoundException();
}
}
class OptionsHandler extends Handler class OptionsHandler extends Handler
{ {
protected function getAllowedMethods() protected function getAllowedMethods()

View File

@ -2,8 +2,10 @@
namespace pjdietz\WellRESTed\Test; namespace pjdietz\WellRESTed\Test;
use pjdietz\WellRESTed\Exceptions\HttpExceptions\ForbiddenException;
use pjdietz\WellRESTed\Interfaces\HandlerInterface; use pjdietz\WellRESTed\Interfaces\HandlerInterface;
use pjdietz\WellRESTed\Interfaces\RequestInterface; use pjdietz\WellRESTed\Interfaces\RequestInterface;
use pjdietz\WellRESTed\Interfaces\ResponseInterface;
use pjdietz\WellRESTed\Response; use pjdietz\WellRESTed\Response;
use pjdietz\WellRESTed\Router; use pjdietz\WellRESTed\Router;
use pjdietz\WellRESTed\Routes\StaticRoute; use pjdietz\WellRESTed\Routes\StaticRoute;
@ -46,6 +48,75 @@ class RouterTest extends \PHPUnit_Framework_TestCase
$this->assertEquals(200, $resp->getStatusCode()); $this->assertEquals(200, $resp->getStatusCode());
} }
public function testRespondWithDefaultErrorForException()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue("/"));
$router = new Router();
$router->addRoute(new StaticRoute("/", __NAMESPACE__ . '\\ForbiddenExceptionHandler'));
$resp = $router->getResponse($mockRequest);
$this->assertEquals(403, $resp->getStatusCode());
}
public function testRespondWithErrorHandlerForException()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue("/"));
$router = new Router();
$router->addRoute(new StaticRoute("/", __NAMESPACE__ . '\\ForbiddenExceptionHandler'));
$router->setErrorHandler(403, __NAMESPACE__ . '\\ForbiddenErrorHandler');
$resp = $router->getResponse($mockRequest);
$this->assertEquals("YOU SHALL NOT PASS!", $resp->getBody());
}
public function testRespondWithErrorHandlerForStatusCode()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue("/"));
$router = new Router();
$router->addRoute(new StaticRoute("/", __NAMESPACE__ . '\\ForbiddenHandler'));
$router->setErrorHandler(403, __NAMESPACE__ . '\\ForbiddenErrorHandler');
$resp = $router->getResponse($mockRequest);
$this->assertEquals("YOU SHALL NOT PASS!", $resp->getBody());
}
public function testRespondWithErrorHandlerUsingOriginalResponse()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue("/"));
$router = new Router();
$router->addRoute(new StaticRoute("/", __NAMESPACE__ . '\\MessageHandler'));
$router->setErrorHandlers([404 => __NAMESPACE__ . '\\MessageErrorHandler']);
$resp = $router->getResponse($mockRequest);
$this->assertEquals("<h1>Not Found</h1>", $resp->getBody());
}
public function testRespondWithErrorHandlerUsingInjection()
{
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
$mockRequest->expects($this->any())
->method('getPath')
->will($this->returnValue("/"));
$router = new Router();
$router->addRoute(new StaticRoute("/", __NAMESPACE__ . '\\ForbiddenHandler'));
$router->setErrorHandlers([403 => __NAMESPACE__ . '\\InjectionErrorHandler']);
$resp = $router->getResponse($mockRequest, ["message" => "Pass through"]);
$this->assertEquals("Pass through", $resp->getBody());
}
public function testReturnNullWhenNoRouteMatches() public function testReturnNullWhenNoRouteMatches()
{ {
$mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface'); $mockRequest = $this->getMock('\pjdietz\WellRESTed\Interfaces\RequestInterface');
@ -203,6 +274,70 @@ class NotFoundHandler implements HandlerInterface
} }
} }
class ForbiddenHandler implements HandlerInterface
{
public function getResponse(RequestInterface $request, array $args = null)
{
$response = new Response(403);
$response->setBody("Forbidden");
return $response;
}
}
class ForbiddenExceptionHandler implements HandlerInterface
{
public function getResponse(RequestInterface $request, array $args = null)
{
throw new ForbiddenException();
}
}
class ForbiddenErrorHandler implements HandlerInterface
{
public function getResponse(RequestInterface $request, array $args = null)
{
$response = new Response(403);
$response->setBody("YOU SHALL NOT PASS!");
return $response;
}
}
class MessageHandler implements HandlerInterface
{
public function getResponse(RequestInterface $request, array $args = null)
{
$response = new Response(404);
$response->setBody("Not Found");
return $response;
}
}
class MessageErrorHandler implements HandlerInterface
{
public function getResponse(RequestInterface $request, array $args = null)
{
if (isset($args["response"])) {
/** @var ResponseInterface $response */
$response = $args["response"];
$message = "<h1>" . $response->getBody() . "</h1>";
$response->setBody($message);
return $response;
}
return null;
}
}
class InjectionErrorHandler implements HandlerInterface
{
public function getResponse(RequestInterface $request, array $args = null)
{
$response = new Response(403);
$response->setBody($args["message"]);
return $response;
}
}
class InjectionHandler implements HandlerInterface class InjectionHandler implements HandlerInterface
{ {
public function getResponse(RequestInterface $request, array $args = null) public function getResponse(RequestInterface $request, array $args = null)