src/EventSubscriber/ExceptionSubscriber.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  5. use Symfony\Component\HttpKernel\Exception\HttpException;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. class ExceptionSubscriber implements EventSubscriberInterface
  8. {
  9.     public function onKernelException(ExceptionEvent $event): void
  10.     {
  11.        /* $exception = $event->getThrowable();
  12.         
  13.         if ($exception instanceof HttpException) 
  14.         {
  15.             $data = [
  16.                 'status' => $exception->getStatusCode(),
  17.                 'message' => $exception->getMessage()
  18.             ];
  19.             
  20.             $event->setResponse(new JsonResponse($data));
  21.         } 
  22.         else 
  23.         {
  24.             $data = [
  25.                 'status' => 500, // Le status n'existe pas car ce n'est pas une exception HTTP, donc on met 500 par défaut.
  26.                 'message' => $exception->getMessage()
  27.             ];
  28.             
  29.             $event->setResponse(new JsonResponse($data));
  30.         }*/
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             'kernel.exception' => 'onKernelException',
  36.         ];
  37.     }
  38. }