src/Listener/ApiExceptionListener.php line 12

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Exception\ApiException;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  6. class ApiExceptionListener
  7. {
  8.     public function onKernelException(ExceptionEvent $event)
  9.     {
  10.         $exception $event->getThrowable();
  11.         if (!$exception instanceof ApiException) {
  12.             return;
  13.         }
  14.         $responseData $exception->getResponse();
  15.         $response = new Response();
  16.         $response->setContent(json_encode($responseData));
  17.         $response->setStatusCode($exception->getCode());
  18.         $event->setResponse($response);
  19.     }
  20. }