lib/netgen/content-browser/bundle/EventListener/ExceptionConversionListener.php line 49

  1. <?php
  2. declare(strict_types=1);
  3. namespace Netgen\Bundle\ContentBrowserBundle\EventListener;
  4. use Netgen\ContentBrowser\Exceptions\InvalidArgumentException;
  5. use Netgen\ContentBrowser\Exceptions\NotFoundException;
  6. use Netgen\ContentBrowser\Exceptions\OutOfBoundsException;
  7. use Netgen\ContentBrowser\Utils\BackwardsCompatibility\ExceptionEventThrowableTrait;
  8. use Netgen\ContentBrowser\Utils\BackwardsCompatibility\MainRequestEventTrait;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  11. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  12. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  13. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  14. use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  17. use function is_a;
  18. final class ExceptionConversionListener implements EventSubscriberInterface
  19. {
  20.     use ExceptionEventThrowableTrait;
  21.     use MainRequestEventTrait;
  22.     /**
  23.      * @var array<class-string<\Throwable>, class-string<\Symfony\Component\HttpKernel\Exception\HttpException>>
  24.      */
  25.     private array $exceptionMap = [
  26.         NotFoundException::class => NotFoundHttpException::class,
  27.         OutOfBoundsException::class => UnprocessableEntityHttpException::class,
  28.         InvalidArgumentException::class => BadRequestHttpException::class,
  29.         // Various other useful exceptions
  30.         AccessDeniedException::class => AccessDeniedHttpException::class,
  31.     ];
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [KernelEvents::EXCEPTION => ['onException'10]];
  35.     }
  36.     /**
  37.      * Converts exceptions to Symfony HTTP exceptions.
  38.      *
  39.      * @param \Symfony\Component\HttpKernel\Event\ExceptionEvent $event
  40.      */
  41.     public function onException($event): void
  42.     {
  43.         if (!$this->isMainRequest($event)) {
  44.             return;
  45.         }
  46.         $attributes $event->getRequest()->attributes;
  47.         if ($attributes->get(SetIsApiRequestListener::API_FLAG_NAME) !== true) {
  48.             return;
  49.         }
  50.         $exception $this->getThrowable($event);
  51.         if ($exception instanceof HttpExceptionInterface) {
  52.             return;
  53.         }
  54.         $exceptionClass null;
  55.         foreach ($this->exceptionMap as $sourceException => $targetException) {
  56.             if (is_a($exception$sourceExceptiontrue)) {
  57.                 $exceptionClass $targetException;
  58.                 break;
  59.             }
  60.         }
  61.         if ($exceptionClass !== null) {
  62.             $convertedException = new $exceptionClass(
  63.                 $exception->getMessage(),
  64.                 $exception,
  65.                 $exception->getCode(),
  66.             );
  67.             $this->setThrowable($event$convertedException);
  68.         }
  69.     }
  70. }