src/AjaxBundle/EventSubscriber/AjaxExceptionSubscriber.php line 36

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of the SynergyBot project.
  5.  *
  6.  * For the full copyright and license information,
  7.  * please read the LICENSE.md file that was distributed with this source code.
  8.  *
  9.  * The SymfonyBot project - inspiring people to chat!
  10.  *
  11.  * Copyright (c) 2022.
  12.  */
  13. namespace App\AjaxBundle\EventSubscriber;
  14. use App\AjaxBundle\AjaxResponse;
  15. use App\AjaxBundle\Exception\AjaxException;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\HttpFoundation\Response;
  18. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  19. class AjaxExceptionSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * {@inheritdoc}
  23.      */
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             'kernel.exception' => 'onKernelException',
  28.         ];
  29.     }
  30.     public function onKernelException(ExceptionEvent $event): void
  31.     {
  32.         /* @var AjaxException $exception */
  33.         $exception $event->getThrowable();
  34.         if (!$exception instanceof AjaxException) {
  35.             return;
  36.         }
  37.         $response = new AjaxResponse($exception->getDetails(), $exception->getMessage(), Response::HTTP_UNPROCESSABLE_ENTITY);
  38.         $response->headers->set('Content-Type''application/problem+json');
  39.         $event->setResponse($response);
  40.     }
  41. }