src/EventSubscriber/ExceptionSubscriber.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Exception\UserBannedException;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  7. use Symfony\Component\HttpKernel\KernelEvents;
  8. class ExceptionSubscriber implements EventSubscriberInterface
  9. {
  10.     public static function getSubscribedEvents()
  11.     {
  12.         return [
  13.             KernelEvents::EXCEPTION => 'onKernelException',
  14.         ];
  15.     }
  16.     public function onKernelException(ExceptionEvent $event)
  17.     {
  18.         $exception $event->getThrowable();
  19.         if ($exception instanceof UserBannedException) {
  20.             $response = new JsonResponse([
  21.                 'success' => false,
  22.                 'message' => $exception->getMessage()
  23.             ], 403);
  24.             $event->setResponse($response);
  25.         }
  26.     }
  27. }