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\EventDispatcherInterface;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
  18. use Symfony\Component\HttpKernel\Event\ExceptionEvent;
  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 null$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 $eventstring $eventName nullEventDispatcherInterface $eventDispatcher null)
  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 && $eventDispatcher instanceof EventDispatcherInterface) {
  67.             $cspRemovalListener = function ($event) use (&$cspRemovalListener$eventDispatcher) {
  68.                 $event->getResponse()->headers->remove('Content-Security-Policy');
  69.                 $eventDispatcher->removeListener(KernelEvents::RESPONSE$cspRemovalListener);
  70.             };
  71.             $eventDispatcher->addListener(KernelEvents::RESPONSE$cspRemovalListener, -128);
  72.         }
  73.     }
  74.     public function onControllerArguments(ControllerArgumentsEvent $event)
  75.     {
  76.         $e $event->getRequest()->attributes->get('exception');
  77.         if (!$e instanceof \Throwable || false === $k array_search($e$event->getArguments(), true)) {
  78.             return;
  79.         }
  80.         $r = new \ReflectionFunction(\Closure::fromCallable($event->getController()));
  81.         $r $r->getParameters()[$k] ?? null;
  82.         if ($r && (!($r $r->getType()) instanceof \ReflectionNamedType || \in_array($r->getName(), [FlattenException::class, LegacyFlattenException::class], true))) {
  83.             $arguments $event->getArguments();
  84.             $arguments[$k] = FlattenException::createFromThrowable($e);
  85.             $event->setArguments($arguments);
  86.         }
  87.     }
  88.     public static function getSubscribedEvents(): array
  89.     {
  90.         return [
  91.             KernelEvents::CONTROLLER_ARGUMENTS => 'onControllerArguments',
  92.             KernelEvents::EXCEPTION => [
  93.                 ['logKernelException'0],
  94.                 ['onKernelException', -128],
  95.             ],
  96.         ];
  97.     }
  98.     /**
  99.      * Logs an exception.
  100.      */
  101.     protected function logException(\Throwable $exceptionstring $message): void
  102.     {
  103.         if (null !== $this->logger) {
  104.             if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
  105.                 $this->logger->critical($message, ['exception' => $exception]);
  106.             } else {
  107.                 $this->logger->error($message, ['exception' => $exception]);
  108.             }
  109.         }
  110.     }
  111.     /**
  112.      * Clones the request for the exception.
  113.      */
  114.     protected function duplicateRequest(\Throwable $exceptionRequest $request): Request
  115.     {
  116.         $attributes = [
  117.             '_controller' => $this->controller,
  118.             'exception' => $exception,
  119.             'logger' => $this->logger instanceof DebugLoggerInterface $this->logger null,
  120.         ];
  121.         $request $request->duplicate(nullnull$attributes);
  122.         $request->setMethod('GET');
  123.         return $request;
  124.     }
  125. }