diff --git a/src/EventListener/ExceptionConversionListener.php b/src/EventListener/ExceptionConversionListener.php new file mode 100644 index 0000000..09a8f61 --- /dev/null +++ b/src/EventListener/ExceptionConversionListener.php @@ -0,0 +1,33 @@ +prettyPrint = (bool) $prettyPrint; + $this->debug = (bool) $debug; + } + + public function onKernelException(GetResponseForExceptionEvent $event) + { + $event->setResponse(VndErrorResponse::fromException($event->getException(), $this->prettyPrint, $this->debug)); + } + + public static function getSubscribedEvents() + { + return array( + KernelEvents::EXCEPTION => array('onKernelException', 100), + ); + } +} diff --git a/tests/Integration/HttpKernelTest.php b/tests/Integration/HttpKernelTest.php index 8421c01..3cdd68c 100644 --- a/tests/Integration/HttpKernelTest.php +++ b/tests/Integration/HttpKernelTest.php @@ -2,8 +2,8 @@ namespace Jsor\Stack\Hal\Integration; +use Jsor\Stack\Hal\EventListener\ExceptionConversionListener; use Jsor\Stack\Hal\EventListener\ResponseConversionListener; -use Jsor\Stack\Hal\ExceptionConverter; use Jsor\Stack\Hal\RequestFormatValidator; use Nocarrier\Hal; use Symfony\Component\EventDispatcher\EventDispatcher; @@ -30,7 +30,6 @@ public function it_intercepts_not_acceptable_format() $httpKernel = new HttpKernel($dispatcher, $resolver); $app = new RequestFormatValidator($httpKernel); - $app = new ExceptionConverter($app); $request = Request::create('/'); $request->attributes->set('_format', 'html'); @@ -65,7 +64,6 @@ public function it_converts_response_to_json() $dispatcher->addSubscriber(new ResponseConversionListener()); $app = new RequestFormatValidator($httpKernel); - $app = new ExceptionConverter($app); $request = Request::create('/'); $request->attributes->set('_format', 'json'); @@ -108,8 +106,9 @@ public function it_converts_exception_to_json() $dispatcher = new EventDispatcher(); $httpKernel = new HttpKernel($dispatcher, $resolver); + $dispatcher->addSubscriber(new ExceptionConversionListener()); + $app = new RequestFormatValidator($httpKernel); - $app = new ExceptionConverter($app); $request = Request::create('/exception'); $request->attributes->set('_format', 'json'); diff --git a/tests/Integration/KernelTest.php b/tests/Integration/KernelTest.php index fe09ed4..2d8fbf7 100644 --- a/tests/Integration/KernelTest.php +++ b/tests/Integration/KernelTest.php @@ -2,8 +2,8 @@ namespace Jsor\Stack\Hal\Integration; +use Jsor\Stack\Hal\EventListener\ExceptionConversionListener; use Jsor\Stack\Hal\EventListener\ResponseConversionListener; -use Jsor\Stack\Hal\ExceptionConverter; use Jsor\Stack\Hal\RequestFormatValidator; use Nocarrier\Hal; use Symfony\Component\EventDispatcher\EventDispatcher; @@ -20,7 +20,6 @@ public function it_intercepts_not_acceptable_format() $kernel = new KernelForTest('test', true); $app = new RequestFormatValidator($kernel); - $app = new ExceptionConverter($app); $request = Request::create('/'); $request->attributes->set('_format', 'html'); @@ -59,7 +58,6 @@ public function it_converts_response_to_json() $dispatcher->addSubscriber(new ResponseConversionListener()); $app = new RequestFormatValidator($kernel); - $app = new ExceptionConverter($app); $request = Request::create('/'); $request->attributes->set('_format', 'json'); @@ -106,8 +104,9 @@ public function it_converts_exception_to_json() $kernel->boot(); $kernel->getContainer()->set('http_kernel', $httpKernel); + $dispatcher->addSubscriber(new ExceptionConversionListener()); + $app = new RequestFormatValidator($kernel); - $app = new ExceptionConverter($app); $request = Request::create('/exception'); $request->attributes->set('_format', 'json');