src/EventListener/ExceptionResponseRequestListener.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\V4\Exception\PolException;
  4. use Exception;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class ExceptionResponseRequestListener implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             KernelEvents::EXCEPTION => 'onKernelException',
  15.         ];
  16.     }
  17.     /**
  18.      * @param ExceptionEvent $event
  19.      *
  20.      * @return void
  21.      *
  22.      * @throws Exception
  23.      */
  24.     public function onKernelException(ExceptionEvent $event)
  25.     {
  26.         $exception $event->getThrowable();
  27.         if (!$exception instanceof PolException) {
  28.             return;
  29.         }
  30.         $response = new JsonResponse($exception->getContentResponse(), $exception->getStatusCode());
  31.         $event->setResponse($response);
  32.     }
  33. }