Skip to content
Snippets Groups Projects
Commit a964b7ae authored by Angie Byron's avatar Angie Byron
Browse files

Issue #1803338 by chx, katbailey, jthorson, Crell: Fixed 403 and 404 pages...

Issue #1803338 by chx, katbailey, jthorson, Crell: Fixed 403 and 404 pages call error_log() cluttering server error logs.
parent 16e74de4
No related branches found
No related tags found
No related merge requests found
......@@ -119,7 +119,7 @@ public function build(ContainerBuilder $container) {
$container->register('request_close_subscriber', 'Drupal\Core\EventSubscriber\RequestCloseSubscriber')
->addTag('kernel.event_subscriber');
$container->register('config_global_override_subscriber', '\Drupal\Core\EventSubscriber\ConfigGlobalOverrideSubscriber');
$container->register('exception_listener', 'Symfony\Component\HttpKernel\EventListener\ExceptionListener')
$container->register('exception_listener', 'Drupal\Core\EventSubscriber\ExceptionListener')
->addTag('kernel.event_subscriber')
->addArgument(new Reference('service_container'))
->setFactoryClass('Drupal\Core\ExceptionController')
......
<?php
/*
* @file
* Definition of Drupal\Core\EventSubscriber\ExceptionListener.
*/
namespace Drupal\Core\EventSubscriber;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
/**
* Override of Symfony EventListener class to kill 403 and 404 from server logs.
*
* This is mostly a copy of Symfony's ExceptionListener but it doesn't have a
* $logger property as we are not currently using a logger. The class from
* Symfony will, in the absense of a logger, call error_log() on every http
* exception.
*
* @todo Remove this class once we introduce a logger.
*
* @see http://drupal.org/node/1803338
*/
class ExceptionListener implements EventSubscriberInterface {
private $controller;
public function __construct($controller) {
$this->controller = $controller;
}
public function onKernelException(GetResponseForExceptionEvent $event) {
static $handling;
if ($handling) {
return FALSE;
}
$handling = TRUE;
$exception = $event->getException();
$request = $event->getRequest();
// Do not put a line in the server logs for every HTTP error.
if (!$exception instanceof HttpExceptionInterface || $exception->getStatusCode() >= 500) {
error_log(sprintf('Uncaught PHP Exception %s: "%s" at %s line %s', get_class($exception), $exception->getMessage(), $exception->getFile(), $exception->getLine()));
}
$attributes = array(
'_controller' => $this->controller,
'exception' => FlattenException::create($exception),
'logger' => NULL,
'format' => $request->getRequestFormat(),
);
$request = $request->duplicate(NULL, NULL, $attributes);
$request->setMethod('GET');
try {
$response = $event->getKernel()->handle($request, HttpKernelInterface::SUB_REQUEST, TRUE);
}
catch (\Exception $e) {
$message = sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage());
error_log($message);
// Set handling to false otherwise it won't be able to handle further
// exceptions.
$handling = FALSE;
return;
}
$event->setResponse($response);
$handling = FALSE;
}
public static function getSubscribedEvents() {
return array(
KernelEvents::EXCEPTION => array('onKernelException', -128),
);
}
}
......@@ -12,9 +12,8 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\JsonResponse;
use Drupal\Core\HttpKernel;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\EventListener\ExceptionListener;
use Drupal\Core\EventSubscriber\ExceptionListener;
/**
* This controller handles HTTP errors generated by the routing system.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment