<?php
namespace ApiBundle\Exceptions;
use OAuth2\OAuth2ServerException;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
/**
* Class OAuth2ServerExceptionListener
* @package ApiBundle\Services
*/
class OAuth2ServerExceptionListener
{
private $container;
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* @param GetResponseForExceptionEvent $event
*/
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
if ($exception instanceof OAuth2ServerException) {
$translator = $this->container->get('translator');
$data = [
'message' => $translator->trans($exception->getMessage(), [], 'api_translations'),
];
$response = new JsonResponse($data, $exception->getHttpCode());
$event->setResponse($response);
}
}
}