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

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