vendor/symfony/http-kernel/EventListener/ExceptionListener.php line 50

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;
  13. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
  17. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
  18. use Symfony\Component\HttpKernel\HttpKernelInterface;
  19. use Symfony\Component\HttpKernel\KernelEvents;
  20. use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
  21. /**
  22.  * @author Fabien Potencier <fabien@symfony.com>
  23.  *
  24.  * @final since Symfony 4.3
  25.  */
  26. class ExceptionListener 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(GetResponseForExceptionEvent $event)
  38.     {
  39.         $e FlattenException::create($event->getException());
  40.         $this->logException($event->getException(), sprintf('Uncaught PHP Exception %s: "%s" at %s line %s'$e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()));
  41.     }
  42.     public function onKernelException(GetResponseForExceptionEvent $event)
  43.     {
  44.         if (null === $this->controller) {
  45.             return;
  46.         }
  47.         $exception $event->getException();
  48.         $request $this->duplicateRequest($exception$event->getRequest());
  49.         $eventDispatcher = \func_num_args() > func_get_arg(2) : null;
  50.         try {
  51.             $response $event->getKernel()->handle($requestHttpKernelInterface::SUB_REQUESTfalse);
  52.         } catch (\Exception $e) {
  53.             $f FlattenException::create($e);
  54.             $this->logException($esprintf('Exception thrown when handling an exception (%s: %s at %s line %s)'$f->getClass(), $f->getMessage(), $e->getFile(), $e->getLine()));
  55.             $prev $e;
  56.             do {
  57.                 if ($exception === $wrapper $prev) {
  58.                     throw $e;
  59.                 }
  60.             } while ($prev $wrapper->getPrevious());
  61.             $prev = new \ReflectionProperty($wrapper instanceof \Exception ? \Exception::class : \Error::class, 'previous');
  62.             $prev->setAccessible(true);
  63.             $prev->setValue($wrapper$exception);
  64.             throw $e;
  65.         }
  66.         $event->setResponse($response);
  67.         if ($this->debug && $eventDispatcher instanceof EventDispatcherInterface) {
  68.             $cspRemovalListener = function ($event) use (&$cspRemovalListener$eventDispatcher) {
  69.                 $event->getResponse()->headers->remove('Content-Security-Policy');
  70.                 $eventDispatcher->removeListener(KernelEvents::RESPONSE$cspRemovalListener);
  71.             };
  72.             $eventDispatcher->addListener(KernelEvents::RESPONSE$cspRemovalListener, -128);
  73.         }
  74.     }
  75.     public static function getSubscribedEvents()
  76.     {
  77.         return [
  78.             KernelEvents::EXCEPTION => [
  79.                 ['logKernelException'0],
  80.                 ['onKernelException', -128],
  81.             ],
  82.         ];
  83.     }
  84.     /**
  85.      * Logs an exception.
  86.      *
  87.      * @param \Exception $exception The \Exception instance
  88.      * @param string     $message   The error message to log
  89.      */
  90.     protected function logException(\Exception $exception$message)
  91.     {
  92.         if (null !== $this->logger) {
  93.             if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
  94.                 $this->logger->critical($message, ['exception' => $exception]);
  95.             } else {
  96.                 $this->logger->error($message, ['exception' => $exception]);
  97.             }
  98.         }
  99.     }
  100.     /**
  101.      * Clones the request for the exception.
  102.      *
  103.      * @param \Exception $exception The thrown exception
  104.      * @param Request    $request   The original request
  105.      *
  106.      * @return Request The cloned request
  107.      */
  108.     protected function duplicateRequest(\Exception $exceptionRequest $request)
  109.     {
  110.         $attributes = [
  111.             '_controller' => $this->controller,
  112.             'exception' => FlattenException::create($exception),
  113.             'logger' => $this->logger instanceof DebugLoggerInterface $this->logger null,
  114.         ];
  115.         $request $request->duplicate(nullnull$attributes);
  116.         $request->setMethod('GET');
  117.         return $request;
  118.     }
  119. }