vendor/contao/core-bundle/src/EventListener/ExceptionConverterListener.php line 60

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\EventListener;
  11. use Contao\CoreBundle\Exception\AccessDeniedException;
  12. use Contao\CoreBundle\Exception\ForwardPageNotFoundException;
  13. use Contao\CoreBundle\Exception\InsecureInstallationException;
  14. use Contao\CoreBundle\Exception\InsufficientAuthenticationException;
  15. use Contao\CoreBundle\Exception\InternalServerErrorException;
  16. use Contao\CoreBundle\Exception\InternalServerErrorHttpException;
  17. use Contao\CoreBundle\Exception\InvalidRequestTokenException;
  18. use Contao\CoreBundle\Exception\NoActivePageFoundException;
  19. use Contao\CoreBundle\Exception\NoLayoutSpecifiedException;
  20. use Contao\CoreBundle\Exception\NoRootPageFoundException;
  21. use Contao\CoreBundle\Exception\PageNotFoundException;
  22. use Contao\CoreBundle\Exception\ServiceUnavailableException as ContaoServiceUnavailableException;
  23. use Lexik\Bundle\MaintenanceBundle\Exception\ServiceUnavailableException;
  24. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  25. use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
  26. use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
  27. use Symfony\Component\HttpKernel\Exception\HttpException;
  28. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  29. use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException;
  30. use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
  31. /**
  32.  * @internal
  33.  */
  34. class ExceptionConverterListener
  35. {
  36.     private const MAPPER = [
  37.         AccessDeniedException::class => 'AccessDeniedHttpException',
  38.         ForwardPageNotFoundException::class => 'InternalServerErrorHttpException',
  39.         InsecureInstallationException::class => 'InternalServerErrorHttpException',
  40.         InsufficientAuthenticationException::class => 'UnauthorizedHttpException',
  41.         InternalServerErrorException::class => 'InternalServerErrorHttpException',
  42.         InvalidRequestTokenException::class => 'BadRequestHttpException',
  43.         NoActivePageFoundException::class => 'NotFoundHttpException',
  44.         NoLayoutSpecifiedException::class => 'InternalServerErrorHttpException',
  45.         NoRootPageFoundException::class => 'NotFoundHttpException',
  46.         PageNotFoundException::class => 'NotFoundHttpException',
  47.         ServiceUnavailableException::class => 'ServiceUnavailableHttpException',
  48.         ContaoServiceUnavailableException::class => 'ServiceUnavailableHttpException',
  49.         \UnusedArgumentsException::class => 'NotFoundHttpException',
  50.     ];
  51.     /**
  52.      * Maps known exceptions to HTTP exceptions.
  53.      */
  54.     public function __invoke(ExceptionEvent $event): void
  55.     {
  56.         $exception $event->getThrowable();
  57.         $class $this->getTargetClass($exception);
  58.         if (null === $class) {
  59.             return;
  60.         }
  61.         if (null !== ($httpException $this->convertToHttpException($exception$class))) {
  62.             $event->setThrowable($httpException);
  63.         }
  64.     }
  65.     private function getTargetClass(\Throwable $exception): ?string
  66.     {
  67.         foreach (self::MAPPER as $source => $target) {
  68.             if ($exception instanceof $source) {
  69.                 return $target;
  70.             }
  71.         }
  72.         return null;
  73.     }
  74.     private function convertToHttpException(\Throwable $exceptionstring $target): ?HttpException
  75.     {
  76.         switch ($target) {
  77.             case 'AccessDeniedHttpException':
  78.                 return new AccessDeniedHttpException($exception->getMessage(), $exception);
  79.             case 'BadRequestHttpException':
  80.                 return new BadRequestHttpException($exception->getMessage(), $exception);
  81.             case 'InternalServerErrorHttpException':
  82.                 return new InternalServerErrorHttpException($exception->getMessage(), $exception);
  83.             case 'NotFoundHttpException':
  84.                 return new NotFoundHttpException($exception->getMessage(), $exception);
  85.             case 'ServiceUnavailableHttpException':
  86.                 return new ServiceUnavailableHttpException(null$exception->getMessage(), $exception);
  87.             case 'UnauthorizedHttpException':
  88.                 return new UnauthorizedHttpException(''$exception->getMessage(), $exception);
  89.         }
  90.         return null;
  91.     }
  92. }