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

Open in your IDE?
  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 Symfony\Component\Debug\Exception\FlattenException as LegacyFlattenException;
  13. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  17. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  18. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  19. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  20. use Symfony\Component\HttpKernel\HttpKernelInterface;
  21. use Symfony\Component\HttpKernel\KernelEvents;
  22. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  23. /**
  24.  * @author Fabien Potencier <fabien@symfony.com>
  25.  */
  26. class ErrorListener implements EventSubscriberInterface
  27. {
  28.     protected $controller;
  29.     protected $logger;
  30.     protected $debug;
  31.     public function __construct($controllerLoggerInterface $logger nullbool $debug false)
  32.     {
  33.         $this->controller $controller;
  34.         $this->logger $logger;
  35.         $this->debug $debug;
  36.     }
  37.     public function logKernelException(ExceptionEvent $event)
  38.     {
  39.         $e FlattenException::createFromThrowable($event->getThrowable());
  40.         $this->logException($event->getThrowable(), sprintf('Uncaught PHP Exception %s: "%s" at %s line %s'$e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()));
  41.     }
  42.     public function onKernelException(ExceptionEvent $event)
  43.     {
  44.         if (null === $this->controller) {
  45.             return;
  46.         }
  47.         $exception $event->getThrowable();
  48.         $request $this->duplicateRequest($exception$event->getRequest());
  49.         try {
  50.             $response $event->getKernel()->handle($requestHttpKernelInterface::SUB_REQUESTfalse);
  51.         } catch (\Exception $e) {
  52.             $f FlattenException::createFromThrowable($e);
  53.             $this->logException($esprintf('Exception thrown when handling an exception (%s: %s at %s line %s)'$f->getClass(), $f->getMessage(), $e->getFile(), $e->getLine()));
  54.             $prev $e;
  55.             do {
  56.                 if ($exception === $wrapper $prev) {
  57.                     throw $e;
  58.                 }
  59.             } while ($prev $wrapper->getPrevious());
  60.             $prev = new \ReflectionProperty($wrapper instanceof \Exception \Exception::class : \Error::class, 'previous');
  61.             $prev->setAccessible(true);
  62.             $prev->setValue($wrapper$exception);
  63.             throw $e;
  64.         }
  65.         $event->setResponse($response);
  66.         if ($this->debug) {
  67.             $event->getRequest()->attributes->set('_remove_csp_headers'true);
  68.         }
  69.     }
  70.     public function removeCspHeader(ResponseEvent $event): void
  71.     {
  72.         if ($this->debug && $event->getRequest()->attributes->get('_remove_csp_headers'false)) {
  73.             $event->getResponse()->headers->remove('Content-Security-Policy');
  74.         }
  75.     }
  76.     public function onControllerArguments(ControllerArgumentsEvent $event)
  77.     {
  78.         $e $event->getRequest()->attributes->get('exception');
  79.         if (!$e instanceof \Throwable || false === $k array_search($e$event->getArguments(), true)) {
  80.             return;
  81.         }
  82.         $r = new \ReflectionFunction(\Closure::fromCallable($event->getController()));
  83.         $r $r->getParameters()[$k] ?? null;
  84.         if ($r && (!($r $r->getType()) instanceof \ReflectionNamedType || \in_array($r->getName(), [FlattenException::class, LegacyFlattenException::class], true))) {
  85.             $arguments $event->getArguments();
  86.             $arguments[$k] = FlattenException::createFromThrowable($e);
  87.             $event->setArguments($arguments);
  88.         }
  89.     }
  90.     public static function getSubscribedEvents(): array
  91.     {
  92.         return [
  93.             KernelEvents::CONTROLLER_ARGUMENTS => 'onControllerArguments',
  94.             KernelEvents::EXCEPTION => [
  95.                 ['logKernelException'0],
  96.                 ['onKernelException', -128],
  97.             ],
  98.             KernelEvents::RESPONSE => ['removeCspHeader', -128],
  99.         ];
  100.     }
  101.     /**
  102.      * Logs an exception.
  103.      */
  104.     protected function logException(\Throwable $exceptionstring $message): void
  105.     {
  106.         if (null !== $this->logger) {
  107.             if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
  108.                 $this->logger->critical($message, ['exception' => $exception]);
  109.             } else {
  110.                 $this->logger->error($message, ['exception' => $exception]);
  111.             }
  112.         }
  113.     }
  114.     /**
  115.      * Clones the request for the exception.
  116.      */
  117.     protected function duplicateRequest(\Throwable $exceptionRequest $request): Request
  118.     {
  119.         $attributes = [
  120.             '_controller' => $this->controller,
  121.             'exception' => $exception,
  122.             'logger' => $this->logger instanceof DebugLoggerInterface $this->logger null,
  123.         ];
  124.         $request $request->duplicate(nullnull$attributes);
  125.         $request->setMethod('GET');
  126.         return $request;
  127.     }
  128. }