You are here

class ExceptionListener in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/symfony/http-kernel/EventListener/ExceptionListener.php \Symfony\Component\HttpKernel\EventListener\ExceptionListener

ExceptionListener.

@author Fabien Potencier <fabien@symfony.com>

Hierarchy

Expanded class hierarchy of ExceptionListener

1 file declares its use of ExceptionListener
ExceptionListenerTest.php in vendor/symfony/http-kernel/Tests/EventListener/ExceptionListenerTest.php

File

vendor/symfony/http-kernel/EventListener/ExceptionListener.php, line 29

Namespace

Symfony\Component\HttpKernel\EventListener
View source
class ExceptionListener implements EventSubscriberInterface {
  protected $controller;
  protected $logger;
  public function __construct($controller, LoggerInterface $logger = null) {
    $this->controller = $controller;
    $this->logger = $logger;
  }
  public function onKernelException(GetResponseForExceptionEvent $event) {
    $exception = $event
      ->getException();
    $request = $event
      ->getRequest();
    $this
      ->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception
      ->getMessage(), $exception
      ->getFile(), $exception
      ->getLine()));
    $request = $this
      ->duplicateRequest($exception, $request);
    try {
      $response = $event
        ->getKernel()
        ->handle($request, HttpKernelInterface::SUB_REQUEST, false);
    } catch (\Exception $e) {
      $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()), false);
      $wrapper = $e;
      while ($prev = $wrapper
        ->getPrevious()) {
        if ($exception === ($wrapper = $prev)) {
          throw $e;
        }
      }
      $prev = new \ReflectionProperty('Exception', 'previous');
      $prev
        ->setAccessible(true);
      $prev
        ->setValue($wrapper, $exception);
      throw $e;
    }
    $event
      ->setResponse($response);
  }
  public static function getSubscribedEvents() {
    return array(
      KernelEvents::EXCEPTION => array(
        'onKernelException',
        -128,
      ),
    );
  }

  /**
   * Logs an exception.
   *
   * @param \Exception $exception The \Exception instance
   * @param string     $message   The error message to log
   */
  protected function logException(\Exception $exception, $message) {
    if (null !== $this->logger) {
      if (!$exception instanceof HttpExceptionInterface || $exception
        ->getStatusCode() >= 500) {
        $this->logger
          ->critical($message, array(
          'exception' => $exception,
        ));
      }
      else {
        $this->logger
          ->error($message, array(
          'exception' => $exception,
        ));
      }
    }
  }

  /**
   * Clones the request for the exception.
   *
   * @param \Exception $exception The thrown exception.
   * @param Request    $request   The original request.
   *
   * @return Request $request The cloned request.
   */
  protected function duplicateRequest(\Exception $exception, Request $request) {
    $attributes = array(
      '_controller' => $this->controller,
      'exception' => FlattenException::create($exception),
      'logger' => $this->logger instanceof DebugLoggerInterface ? $this->logger : null,
      // keep for BC -- as $format can be an argument of the controller callable
      // see src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
      // @deprecated since version 2.4, to be removed in 3.0
      'format' => $request
        ->getRequestFormat(),
    );
    $request = $request
      ->duplicate(null, null, $attributes);
    $request
      ->setMethod('GET');
    return $request;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ExceptionListener::$controller protected property
ExceptionListener::$logger protected property
ExceptionListener::duplicateRequest protected function Clones the request for the exception.
ExceptionListener::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to. Overrides EventSubscriberInterface::getSubscribedEvents
ExceptionListener::logException protected function Logs an exception.
ExceptionListener::onKernelException public function
ExceptionListener::__construct public function