vendor/symfony/http-kernel/EventListener/ErrorListener.php line 46

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpKernel\EventListener;
  11. use Psr\Log\LoggerInterface;
  12. use Psr\Log\LogLevel;
  13. use Symfony\Component\ErrorHandler\ErrorHandler;
  14. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpKernel\Attribute\WithHttpStatus;
  18. use Symfony\Component\HttpKernel\Attribute\WithLogLevel;
  19. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  20. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  21. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  22. use Symfony\Component\HttpKernel\Exception\HttpException;
  23. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  24. use Symfony\Component\HttpKernel\HttpKernelInterface;
  25. use Symfony\Component\HttpKernel\KernelEvents;
  26. use Symfony\Component\HttpKernel\Log\DebugLoggerConfigurator;
  27. /**
  28.  * @author Fabien Potencier <fabien@symfony.com>
  29.  */
  30. class ErrorListener implements EventSubscriberInterface
  31. {
  32.     protected $controller;
  33.     protected $logger;
  34.     protected $debug;
  35.     /**
  36.      * @var array<class-string, array{log_level: string|null, status_code: int<100,599>|null}>
  37.      */
  38.     protected $exceptionsMapping;
  39.     /**
  40.      * @param array<class-string, array{log_level: string|null, status_code: int<100,599>|null}> $exceptionsMapping
  41.      */
  42.     public function __construct(string|object|array|null $controller, ?LoggerInterface $logger nullbool $debug false, array $exceptionsMapping = [])
  43.     {
  44.         $this->controller $controller;
  45.         $this->logger $logger;
  46.         $this->debug $debug;
  47.         $this->exceptionsMapping $exceptionsMapping;
  48.     }
  49.     /**
  50.      * @return void
  51.      */
  52.     public function logKernelException(ExceptionEvent $event)
  53.     {
  54.         $throwable $event->getThrowable();
  55.         $logLevel $this->resolveLogLevel($throwable);
  56.         foreach ($this->exceptionsMapping as $class => $config) {
  57.             if (!$throwable instanceof $class || !$config['status_code']) {
  58.                 continue;
  59.             }
  60.             if (!$throwable instanceof HttpExceptionInterface || $throwable->getStatusCode() !== $config['status_code']) {
  61.                 $headers $throwable instanceof HttpExceptionInterface $throwable->getHeaders() : [];
  62.                 $throwable = new HttpException($config['status_code'], $throwable->getMessage(), $throwable$headers);
  63.                 $event->setThrowable($throwable);
  64.             }
  65.             break;
  66.         }
  67.         // There's no specific status code defined in the configuration for this exception
  68.         if (!$throwable instanceof HttpExceptionInterface) {
  69.             $class = new \ReflectionClass($throwable);
  70.             do {
  71.                 if ($attributes $class->getAttributes(WithHttpStatus::class, \ReflectionAttribute::IS_INSTANCEOF)) {
  72.                     /** @var WithHttpStatus $instance */
  73.                     $instance $attributes[0]->newInstance();
  74.                     $throwable = new HttpException($instance->statusCode$throwable->getMessage(), $throwable$instance->headers);
  75.                     $event->setThrowable($throwable);
  76.                     break;
  77.                 }
  78.             } while ($class $class->getParentClass());
  79.         }
  80.         $e FlattenException::createFromThrowable($throwable);
  81.         $this->logException($throwablesprintf('Uncaught PHP Exception %s: "%s" at %s line %s'$e->getClass(), $e->getMessage(), basename($e->getFile()), $e->getLine()), $logLevel);
  82.     }
  83.     /**
  84.      * @return void
  85.      */
  86.     public function onKernelException(ExceptionEvent $event)
  87.     {
  88.         if (null === $this->controller) {
  89.             return;
  90.         }
  91.         $throwable $event->getThrowable();
  92.         if ($exceptionHandler set_exception_handler(var_dump(...))) {
  93.             restore_exception_handler();
  94.             if (\is_array($exceptionHandler) && $exceptionHandler[0] instanceof ErrorHandler) {
  95.                 $throwable $exceptionHandler[0]->enhanceError($event->getThrowable());
  96.             }
  97.         }
  98.         $request $this->duplicateRequest($throwable$event->getRequest());
  99.         try {
  100.             $response $event->getKernel()->handle($requestHttpKernelInterface::SUB_REQUESTfalse);
  101.         } catch (\Exception $e) {
  102.             $f FlattenException::createFromThrowable($e);
  103.             $this->logException($esprintf('Exception thrown when handling an exception (%s: %s at %s line %s)'$f->getClass(), $f->getMessage(), basename($e->getFile()), $e->getLine()));
  104.             $prev $e;
  105.             do {
  106.                 if ($throwable === $wrapper $prev) {
  107.                     throw $e;
  108.                 }
  109.             } while ($prev $wrapper->getPrevious());
  110.             $prev = new \ReflectionProperty($wrapper instanceof \Exception \Exception::class : \Error::class, 'previous');
  111.             $prev->setValue($wrapper$throwable);
  112.             throw $e;
  113.         }
  114.         $event->setResponse($response);
  115.         if ($this->debug) {
  116.             $event->getRequest()->attributes->set('_remove_csp_headers'true);
  117.         }
  118.     }
  119.     public function removeCspHeader(ResponseEvent $event): void
  120.     {
  121.         if ($this->debug && $event->getRequest()->attributes->get('_remove_csp_headers'false)) {
  122.             $event->getResponse()->headers->remove('Content-Security-Policy');
  123.         }
  124.     }
  125.     /**
  126.      * @return void
  127.      */
  128.     public function onControllerArguments(ControllerArgumentsEvent $event)
  129.     {
  130.         $e $event->getRequest()->attributes->get('exception');
  131.         if (!$e instanceof \Throwable || false === $k array_search($e$event->getArguments(), true)) {
  132.             return;
  133.         }
  134.         $r = new \ReflectionFunction($event->getController()(...));
  135.         $r $r->getParameters()[$k] ?? null;
  136.         if ($r && (!($r $r->getType()) instanceof \ReflectionNamedType || FlattenException::class === $r->getName())) {
  137.             $arguments $event->getArguments();
  138.             $arguments[$k] = FlattenException::createFromThrowable($e);
  139.             $event->setArguments($arguments);
  140.         }
  141.     }
  142.     public static function getSubscribedEvents(): array
  143.     {
  144.         return [
  145.             KernelEvents::CONTROLLER_ARGUMENTS => 'onControllerArguments',
  146.             KernelEvents::EXCEPTION => [
  147.                 ['logKernelException'0],
  148.                 ['onKernelException', -128],
  149.             ],
  150.             KernelEvents::RESPONSE => ['removeCspHeader', -128],
  151.         ];
  152.     }
  153.     /**
  154.      * Logs an exception.
  155.      */
  156.     protected function logException(\Throwable $exceptionstring $message, ?string $logLevel null): void
  157.     {
  158.         if (null === $this->logger) {
  159.             return;
  160.         }
  161.         $logLevel ??= $this->resolveLogLevel($exception);
  162.         $this->logger->log($logLevel$message, ['exception' => $exception]);
  163.     }
  164.     /**
  165.      * Resolves the level to be used when logging the exception.
  166.      */
  167.     private function resolveLogLevel(\Throwable $throwable): string
  168.     {
  169.         foreach ($this->exceptionsMapping as $class => $config) {
  170.             if ($throwable instanceof $class && $config['log_level']) {
  171.                 return $config['log_level'];
  172.             }
  173.         }
  174.         $class = new \ReflectionClass($throwable);
  175.         do {
  176.             if ($attributes $class->getAttributes(WithLogLevel::class)) {
  177.                 /** @var WithLogLevel $instance */
  178.                 $instance $attributes[0]->newInstance();
  179.                 return $instance->level;
  180.             }
  181.         } while ($class $class->getParentClass());
  182.         if (!$throwable instanceof HttpExceptionInterface || $throwable->getStatusCode() >= 500) {
  183.             return LogLevel::CRITICAL;
  184.         }
  185.         return LogLevel::ERROR;
  186.     }
  187.     /**
  188.      * Clones the request for the exception.
  189.      */
  190.     protected function duplicateRequest(\Throwable $exceptionRequest $request): Request
  191.     {
  192.         $attributes = [
  193.             '_controller' => $this->controller,
  194.             'exception' => $exception,
  195.             'logger' => DebugLoggerConfigurator::getDebugLogger($this->logger),
  196.         ];
  197.         $request $request->duplicate(nullnull$attributes);
  198.         $request->setMethod('GET');
  199.         return $request;
  200.     }
  201. }