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

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