src/Subscriber/ExceptionSubscriber.php line 20

Open in your IDE?
  1. <?php
  2. namespace App\Subscriber;
  3. use App\Exception\NormalizedExceptionInterface;
  4. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. /**
  10.  * Class ExceptionSubscriber.
  11.  */
  12. class ExceptionSubscriber implements EventSubscriberInterface
  13. {
  14.     const SERVER_ERROR 'server_error';
  15.     const FORBIDDEN 'forbidden';
  16.     public function processException(ExceptionEvent $event)
  17.     {
  18.         /** @var \Exception $exception */
  19.         $exception $event->getThrowable();
  20.         if (!($exception instanceof NormalizedExceptionInterface)) {
  21.             return;
  22.         }
  23.         $result $exception->normalize();
  24.         $httpCode $exception->getHttpCode();
  25.         $event->setResponse(new JsonResponse($result$httpCode));
  26.     }
  27.     /**
  28.      * @return string[][][]|number[][][]
  29.      */
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             KernelEvents::EXCEPTION => [['processException'255]],
  34.         ];
  35.     }
  36. }