vendor/symfony/http-kernel/EventListener/ExceptionListener.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\ErrorHandler\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. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.4, use "ErrorListener" instead.'ExceptionListener::class), \E_USER_DEPRECATED);
  22. /**
  23.  * @deprecated since Symfony 4.4, use ErrorListener instead
  24.  */
  25. class ExceptionListener implements EventSubscriberInterface
  26. {
  27.     protected $controller;
  28.     protected $logger;
  29.     protected $debug;
  30.     public function __construct($controllerLoggerInterface $logger null$debug false)
  31.     {
  32.         $this->controller $controller;
  33.         $this->logger $logger;
  34.         $this->debug $debug;
  35.     }
  36.     public function logKernelException(GetResponseForExceptionEvent $event)
  37.     {
  38.         $e FlattenException::createFromThrowable($event->getException());
  39.         $this->logException($event->getException(), sprintf('Uncaught PHP Exception %s: "%s" at %s line %s'$e->getClass(), $e->getMessage(), $e->getFile(), $e->getLine()));
  40.     }
  41.     public function onKernelException(GetResponseForExceptionEvent $event)
  42.     {
  43.         if (null === $this->controller) {
  44.             return;
  45.         }
  46.         $exception $event->getException();
  47.         $request $this->duplicateRequest($exception$event->getRequest());
  48.         $eventDispatcher = \func_num_args() > func_get_arg(2) : null;
  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 static function getSubscribedEvents()
  75.     {
  76.         return [
  77.             KernelEvents::EXCEPTION => [
  78.                 ['logKernelException'0],
  79.                 ['onKernelException', -128],
  80.             ],
  81.         ];
  82.     }
  83.     /**
  84.      * Logs an exception.
  85.      *
  86.      * @param \Exception $exception The \Exception instance
  87.      * @param string     $message   The error message to log
  88.      */
  89.     protected function logException(\Exception $exception$message)
  90.     {
  91.         if (null !== $this->logger) {
  92.             if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
  93.                 $this->logger->critical($message, ['exception' => $exception]);
  94.             } else {
  95.                 $this->logger->error($message, ['exception' => $exception]);
  96.             }
  97.         }
  98.     }
  99.     /**
  100.      * Clones the request for the exception.
  101.      *
  102.      * @return Request The cloned request
  103.      */
  104.     protected function duplicateRequest(\Exception $exceptionRequest $request)
  105.     {
  106.         $attributes = [
  107.             '_controller' => $this->controller,
  108.             'exception' => FlattenException::createFromThrowable($exception),
  109.             'logger' => $this->logger instanceof DebugLoggerInterface $this->logger null,
  110.         ];
  111.         $request $request->duplicate(nullnull$attributes);
  112.         $request->setMethod('GET');
  113.         return $request;
  114.     }
  115. }