Skip to content

Commit

Permalink
ExceptionListener implements EventSubscriberInterface
Browse files Browse the repository at this point in the history
  • Loading branch information
hswong3i committed Dec 16, 2014
1 parent ab74f52 commit 0923ee2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
42 changes: 32 additions & 10 deletions src/AuthBucket/OAuth2/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,24 @@

use AuthBucket\OAuth2\Exception\ExceptionInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* ExceptionListener.
*
* @author Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
*/
class ExceptionListener
class ExceptionListener implements EventSubscriberInterface
{
private $logger;

public function __construct(
LoggerInterface $logger = null
) {
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

Expand All @@ -47,16 +48,25 @@ private function handleException(
GetResponseForExceptionEvent $event,
ExceptionInterface $exception
) {
$message = unserialize($exception->getMessage());

if (null !== $this->logger) {
$this->logger->debug(sprintf('OAuth2 exception occured by "%s" at line %s: %s',
$message = sprintf(
'%s: %s (code %s) at %s line %s',
get_class($exception),
$exception->getMessage(),
$exception->getCode(),
$exception->getFile(),
$exception->getLine(),
var_export($message, true)
));
$exception->getLine()
);

if ($exception->getCode() < 500) {
$this->logger->error($message, array('exception' => $exception));
} else {
$this->logger->critical($message, array('exception' => $exception));
}
}

$message = unserialize($exception->getMessage());

if (isset($message['redirect_uri'])) {
$redirectUri = $message['redirect_uri'];
unset($message['redirect_uri']);
Expand All @@ -74,4 +84,16 @@ private function handleException(

$event->setResponse($response);
}

public static function getSubscribedEvents()
{
return array(
/*
* Priority -2 is used to come after those from SecurityServiceProvider (0)
* but before the error handlers added with Silex\EventListener\LogListener (-4)
* and Silex\Application::error (defaults to -8)
*/
KernelEvents::EXCEPTION => array('onKernelException', -2),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ServiceProviderInterface;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* OAuth2 service provider as plugin for Silex SecurityServiceProvider.
Expand Down Expand Up @@ -293,6 +292,6 @@ public function connect(Application $app)

public function boot(Application $app)
{
$app['dispatcher']->addListener(KernelEvents::EXCEPTION, array($app['authbucket_oauth2.exception_listener'], 'onKernelException'), -8);
$app['dispatcher']->addSubscriber($app['authbucket_oauth2.exception_listener']);
}
}

0 comments on commit 0923ee2

Please sign in to comment.