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

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