<?php
namespace App\EventListener;
use App\V4\Exception\PolException;
use Exception;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class ExceptionResponseRequestListener implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
KernelEvents::EXCEPTION => 'onKernelException',
];
}
/**
* @param ExceptionEvent $event
*
* @return void
*
* @throws Exception
*/
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
if (!$exception instanceof PolException) {
return;
}
$response = new JsonResponse($exception->getContentResponse(), $exception->getStatusCode());
$event->setResponse($response);
}
}