summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorwebchick2012-10-11 02:25:09 (GMT)
committerwebchick2012-10-11 02:25:09 (GMT)
commita964b7aebd7083904dfb4878a3e360b19f90ae43 (patch)
tree0f17d269657819d15b86e57bdbad4500b3321c12
parent16e74de41d7b478d379c691c15a3f8af7b5b3f95 (diff)
Issue #1803338 by chx, katbailey, jthorson, Crell: Fixed 403 and 404 pages call error_log() cluttering server error logs.
-rw-r--r--core/lib/Drupal/Core/CoreBundle.php2
-rw-r--r--core/lib/Drupal/Core/EventSubscriber/ExceptionListener.php83
-rw-r--r--core/lib/Drupal/Core/ExceptionController.php3
3 files changed, 85 insertions, 3 deletions
diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php
index ed15dd5..f1e43d6 100644
--- a/core/lib/Drupal/Core/CoreBundle.php
+++ b/core/lib/Drupal/Core/CoreBundle.php
@@ -119,7 +119,7 @@ class CoreBundle extends Bundle
$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')
diff --git a/core/lib/Drupal/Core/EventSubscriber/ExceptionListener.php b/core/lib/Drupal/Core/EventSubscriber/ExceptionListener.php
new file mode 100644
index 0000000..f71b351
--- /dev/null
+++ b/core/lib/Drupal/Core/EventSubscriber/ExceptionListener.php
@@ -0,0 +1,83 @@
+<?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),
+ );
+ }
+}
diff --git a/core/lib/Drupal/Core/ExceptionController.php b/core/lib/Drupal/Core/ExceptionController.php
index 20c9ec2..cb448de 100644
--- a/core/lib/Drupal/Core/ExceptionController.php
+++ b/core/lib/Drupal/Core/ExceptionController.php
@@ -12,9 +12,8 @@ use Symfony\Component\DependencyInjection\ContainerAware;
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.