Skip to content

Commit

Permalink
Restore ExceptionListener as exception rendering listener, add BC mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mtibben committed Jan 9, 2018
1 parent f7d8fcf commit 9b9ee9e
Show file tree
Hide file tree
Showing 9 changed files with 266 additions and 169 deletions.
Expand Up @@ -77,6 +77,7 @@
<tag name="monolog.logger" channel="request" />
<argument type="service" id="logger" on-invalid="null" />
<argument>%framework.exception_listener.http_log_levels%</argument>
<argument type="service" id="twig.exception_listener" on-invalid="null" />
</service>

</services>
Expand Down
Expand Up @@ -249,6 +249,7 @@ class_exists(SemaphoreStore::class) && SemaphoreStore::isSupported() ? 'semaphor
),
),
),
'http_exception_log_levels' => array(),
);
}
}
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
Expand Up @@ -123,11 +123,12 @@
<argument type="service" id="workflow.registry" />
</service>

<service id="twig.exception_listener" class="Symfony\Component\HttpKernel\EventListener\RenderControllerExceptionListener">
<service id="twig.exception_listener" class="Symfony\Component\HttpKernel\EventListener\ExceptionListener">
<tag name="kernel.event_subscriber" />
<tag name="monolog.logger" channel="request" />
<argument>%twig.exception_listener.controller%</argument>
<argument type="service" id="logger" on-invalid="null" />
<argument>false</argument>
</service>

<service id="twig.controller.exception" class="Symfony\Bundle\TwigBundle\Controller\ExceptionController" public="true">
Expand Down
Expand Up @@ -23,24 +23,22 @@
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

@trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.1 and will be removed in 5.0, use "%s" or "%s" instead.', ExceptionListener::class, RenderControllerExceptionListener::class, LoggingExceptionListener::class), E_USER_DEPRECATED);

/**
* ExceptionListener.
*
* @author Fabien Potencier <fabien@symfony.com>
*
* @deprecated since Symfony 4.1, use RenderControllerExceptionListener or LoggingExceptionListener instead
*/
class ExceptionListener implements EventSubscriberInterface
{
protected $controller;
protected $logger;
private $isBCExceptionLoggingEnabled;

public function __construct($controller, LoggerInterface $logger = null)
public function __construct($controller, LoggerInterface $logger = null/*, bool $isBCExceptionLoggingEnabled = true*/)
{
$this->controller = $controller;
$this->logger = $logger;
$this->isBCExceptionLoggingEnabled = (func_num_args() >= 3) ? (bool) func_get_arg(2) : true;
}

public function onKernelException(GetResponseForExceptionEvent $event)
Expand All @@ -49,14 +47,21 @@ public function onKernelException(GetResponseForExceptionEvent $event)
$request = $event->getRequest();
$eventDispatcher = func_num_args() > 2 ? func_get_arg(2) : null;

$this->logException($exception, sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
if ($this->isBCExceptionLoggingEnabled) {
$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()));
$message = sprintf('Exception thrown when handling an exception (%s: %s at %s line %s)', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine());
if ($this->isBCExceptionLoggingEnabled) {
$this->logException($e, $message);
} elseif (null !== $this->logger) {
$this->logger->critical($message, array('exception' => $e));
}

$wrapper = $e;

Expand Down Expand Up @@ -96,9 +101,13 @@ public static function getSubscribedEvents()
*
* @param \Exception $exception The \Exception instance
* @param string $message The error message to log
*
* @deprecated since Symfony 4.1, to be removed in 5.0. Use LoggingExceptionListener instead to log exceptions.
*/
protected function logException(\Exception $exception, $message)
{
@trigger_error(sprintf('The %s() method is deprecated since Symfony 4.1 and will be removed in 5.0. Use %s instead.', __METHOD__, LoggingExceptionListener::class), E_USER_DEPRECATED);

if (null !== $this->logger) {
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
$this->logger->critical($message, array('exception' => $exception));
Expand Down Expand Up @@ -128,4 +137,14 @@ protected function duplicateRequest(\Exception $exception, Request $request)

return $request;
}

/**
* @deprecated since Symfony 4.1, to be removed in 5.0.
*
* @internal
*/
public function isLoggingExceptions(): bool
{
return $this->isBCExceptionLoggingEnabled;
}
}
Expand Up @@ -23,15 +23,28 @@ class LoggingExceptionListener implements EventSubscriberInterface
{
protected $logger;
protected $httpStatusCodeLogLevel;
private $isDisabled;

public function __construct(LoggerInterface $logger = null, array $httpStatusCodeLogLevel = array())
public function __construct(LoggerInterface $logger = null, array $httpStatusCodeLogLevel = array()/*, ExceptionListener $exceptionListener = null */)
{
$this->logger = $logger ?: new NullLogger();
$this->httpStatusCodeLogLevel = $httpStatusCodeLogLevel;

if (func_num_args() >= 3) {
$exceptionListener = func_get_arg(2);
$this->isDisabled = ($exceptionListener instanceof ExceptionListener) ? $exceptionListener->isLoggingExceptions() : false;
if ($this->isDisabled) {
$this->logger->debug('Logging disabled for backwards compatibility, ExceptionListener is already logging exceptions.');
}
}
}

public function logKernelException(GetResponseForExceptionEvent $event)
{
if ($this->isDisabled) {
return;
}

$exception = $event->getException();
$level = $this->getExceptionLogLevel($exception);
$message = sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine());
Expand Down

This file was deleted.

0 comments on commit 9b9ee9e

Please sign in to comment.