Re-add converting HttpExceptions to responses in Handler
This commit is contained in:
parent
1a88e0273d
commit
84044d5057
|
|
@ -4,12 +4,13 @@
|
||||||
* pjdietz\WellRESTed\Handler
|
* pjdietz\WellRESTed\Handler
|
||||||
*
|
*
|
||||||
* @author PJ Dietz <pj@pjdietz.com>
|
* @author PJ Dietz <pj@pjdietz.com>
|
||||||
* @copyright Copyright 2014 by PJ Dietz
|
* @copyright Copyright 2015 by PJ Dietz
|
||||||
* @license MIT
|
* @license MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
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;
|
||||||
|
|
@ -45,7 +46,12 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
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
|
||||||
|
|
@ -43,6 +44,18 @@ 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');
|
||||||
|
|
@ -65,3 +78,11 @@ class OptionsHandler extends Handler
|
||||||
return ["GET","POST"];
|
return ["GET","POST"];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ExceptionHandler extends Handler
|
||||||
|
{
|
||||||
|
protected function get()
|
||||||
|
{
|
||||||
|
throw new NotFoundException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue