4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\EventListener;
14 use Psr\Log\LoggerInterface;
15 use Symfony\Component\Debug\Exception\FlattenException;
16 use Symfony\Component\HttpFoundation\Request;
17 use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
18 use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
19 use Symfony\Component\HttpKernel\KernelEvents;
20 use Symfony\Component\HttpKernel\HttpKernelInterface;
21 use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
22 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
27 * @author Fabien Potencier <fabien@symfony.com>
29 class ExceptionListener implements EventSubscriberInterface
31 protected $controller;
34 public function __construct($controller, LoggerInterface $logger = null)
36 $this->controller = $controller;
37 $this->logger = $logger;
40 public function onKernelException(GetResponseForExceptionEvent $event)
42 $exception = $event->getException();
43 $request = $event->getRequest();
45 $this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
47 $request = $this->duplicateRequest($exception, $request);
50 $response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, false);
51 } catch (\Exception $e) {
52 $this->logException($e, sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()));
56 while ($prev = $wrapper->getPrevious()) {
57 if ($exception === $wrapper = $prev) {
62 $prev = new \ReflectionProperty('Exception', 'previous');
63 $prev->setAccessible(true);
64 $prev->setValue($wrapper, $exception);
69 $event->setResponse($response);
72 public static function getSubscribedEvents()
75 KernelEvents::EXCEPTION => array('onKernelException', -128),
82 * @param \Exception $exception The \Exception instance
83 * @param string $message The error message to log
85 protected function logException(\Exception $exception, $message)
87 if (null !== $this->logger) {
88 if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
89 $this->logger->critical($message, array('exception' => $exception));
91 $this->logger->error($message, array('exception' => $exception));
97 * Clones the request for the exception.
99 * @param \Exception $exception The thrown exception
100 * @param Request $request The original request
102 * @return Request $request The cloned request
104 protected function duplicateRequest(\Exception $exception, Request $request)
107 '_controller' => $this->controller,
108 'exception' => FlattenException::create($exception),
109 'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
110 // keep for BC -- as $format can be an argument of the controller callable
111 // see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
112 // @deprecated since version 2.4, to be removed in 3.0
113 'format' => $request->getRequestFormat(),
115 $request = $request->duplicate(null, null, $attributes);
116 $request->setMethod('GET');