src/EventSubscriber/ExceptionSubscriber.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\Exception\InvalidArgumentException;
  4. use App\External\Gitlab\RequestException;
  5. use App\Service\TimeEntryManagement\Exception\CloseTimeEntryException;
  6. use App\Service\TimeEntryManagement\Exception\ConflictTimeEntryException;
  7. use App\Service\TimeEntryManagement\Exception\LongTimeLengthException;
  8. use App\Service\TimeEntryManagement\Exception\ShortTimeLengthException;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  11. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  12. use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
  13. use Symfony\Component\HttpKernel\KernelEvents;
  14. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  15. use Symfony\Contracts\Translation\TranslatorInterface;
  16. class ExceptionSubscriber implements EventSubscriberInterface
  17. {
  18.     public const ALLOWED_EXCEPTIONS = [
  19.         AccessDeniedHttpException::class,
  20.         ExceptionInterface::class,
  21.         InvalidArgumentException::class,
  22.         RequestException::class,
  23.         LongTimeLengthException::class,
  24.         ShortTimeLengthException::class,
  25.         CloseTimeEntryException::class,
  26.         ConflictTimeEntryException::class,
  27.         ConflictHttpException::class,
  28.     ];
  29.     public function __construct(private TranslatorInterface $translator)
  30.     {
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             KernelEvents::EXCEPTION => ['onException'],
  36.         ];
  37.     }
  38.     /**
  39.      * @return void
  40.      */
  41.     public function onException(ExceptionEvent $event)
  42.     {
  43.         $throwable \get_class($event->getThrowable());
  44.         if (!\in_array($throwableself::ALLOWED_EXCEPTIONS)) {
  45.             return;
  46.         }
  47.         $message $this->translator->trans($event->getThrowable()->getMessage());
  48.         $event->setThrowable(new $throwable(message$messageprevious$event->getThrowable()));
  49.     }
  50. }