Skip to content

Commit

Permalink
Add exception conversion listener
Browse files Browse the repository at this point in the history
  • Loading branch information
jsor committed Oct 28, 2014
1 parent e3b6787 commit 19693e7
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
33 changes: 33 additions & 0 deletions src/EventListener/ExceptionConversionListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Jsor\Stack\Hal\EventListener;

use Jsor\Stack\Hal\Response\VndErrorResponse;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class ExceptionConversionListener implements EventSubscriberInterface
{
private $prettyPrint;
private $debug;

public function __construct($prettyPrint = false,
$debug = false)
{
$this->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),
);
}
}
7 changes: 3 additions & 4 deletions tests/Integration/HttpKernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down
7 changes: 3 additions & 4 deletions tests/Integration/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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');
Expand Down

0 comments on commit 19693e7

Please sign in to comment.