src/EventSubscriber/ExceptionSubscriber.php line 22

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\KernelEvents;
  6. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. class ExceptionSubscriber implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents()
  12.     {
  13.         // return the subscribed events, their methods and priorities
  14.         return [
  15.             KernelEvents::EXCEPTION =>'onKernelExecption',
  16.         ];
  17.     }
  18.     public function onKernelExecption(ExceptionEvent $event)
  19.     {
  20.         $exception $event->getThrowable();
  21.         $response = new JsonResponse([
  22.             'code'          => Response::HTTP_INTERNAL_SERVER_ERROR,
  23.             'message'       => $exception->getMessage()
  24.         ]);
  25.         $event->setResponse($response);
  26.       
  27.     }
  28.     
  29.     
  30. }