vendor/symfony/symfony/src/Symfony/Component/HttpKernel/EventListener/ExceptionListener.php line 42

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