src/EventSubscriber/ExceptionSubscriber.php line 27

Open in your IDE?
  1. <?php
  2. /* @author <storageprocedure@gmail.com> */
  3. declare(strict_types=1);
  4. namespace App\EventSubscriber;
  5. use App\Exception\ApplicationLogicException;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\JsonResponse;
  8. use Symfony\Component\HttpFoundation\Response;
  9. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  10. use Symfony\Component\HttpKernel\KernelEvents;
  11. class ExceptionSubscriber implements EventSubscriberInterface
  12. {
  13.     public static function getSubscribedEvents()
  14.     {
  15.         return [
  16.             KernelEvents::EXCEPTION => [
  17.                 ['processApplicationLogicException'10],
  18.                 //                ['logException', 0],
  19.                 //                ['notifyException', -10],
  20.             ],
  21.         ];
  22.     }
  23.     public function processApplicationLogicException(ExceptionEvent $event)
  24.     {
  25.         $exception $event->getThrowable();
  26.         if (!$exception instanceof ApplicationLogicException) {
  27.             return;
  28.         }
  29.         $value $exception->getMessages();
  30.         $value implode('<br/> '$value);
  31.         $response = new JsonResponse($valueResponse::HTTP_BAD_REQUEST);
  32.         $response->setEncodingOptions(JSON_UNESCAPED_UNICODE JSON_UNESCAPED_SLASHES);
  33.         $event->setResponse($response);
  34.     }
  35. }