vendor/fntv/api-client-bundle/EventListener/ApiExceptionListener.php line 18

Open in your IDE?
  1. <?php
  2. namespace Fntv\ApiClientBundle\EventListener;
  3. use Symfony\Component\HttpFoundation\JsonResponse;
  4. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  5. use Fntv\ApiClientBundle\Exception\ApiException;
  6. /**
  7.  * Catch les ApiException pour retouner une JsonResponse normalisée
  8.  */
  9. class ApiExceptionListener
  10. {
  11.     /**
  12.      * @param ExceptionEvent $event
  13.      * @return void
  14.      */
  15.     public function onKernelException(ExceptionEvent $event): void
  16.     {
  17.         $exception $event->getThrowable();
  18.         if (!$exception instanceof ApiException) {
  19.             return;
  20.         }
  21.         $content = [
  22.             'error' => $exception->getMessage(),
  23.             'details' => $exception->getDetails(),
  24.         ];
  25.         $code $exception->getCode() ?? Response::HTTP_INTERNAL_SERVER_ERROR;
  26.         $event->setResponse(new JsonResponse($content$code));
  27.     }
  28. }