Commit 51a27d10 authored by alexpott's avatar alexpott

Issue #2323759 by Crell, dawehner: Modularize kernel exception handling.

parent 983cdbf0
......@@ -196,6 +196,11 @@ services:
factory_method: get
factory_service: logger.factory
arguments: ['system']
logger.channel.php:
class: Drupal\Core\Logger\LoggerChannel
factory_method: get
factory_service: logger.factory
arguments: ['php']
logger.channel.image:
class: Drupal\Core\Logger\LoggerChannel
factory_method: get
......@@ -736,16 +741,44 @@ services:
tags:
- { name: event_subscriber }
arguments: ['@config.manager', '@config.storage', '@config.storage.snapshot']
exception_controller:
class: Drupal\Core\Controller\ExceptionController
arguments: ['@content_negotiation', '@title_resolver', '@html_page_renderer', '@html_fragment_renderer', '@string_translation', '@url_generator', '@logger.factory']
calls:
- [setContainer, ['@service_container']]
exception_listener:
class: Drupal\Core\EventSubscriber\ExceptionListener
exception.default_json:
class: Drupal\Core\EventSubscriber\ExceptionJsonSubscriber
tags:
- { name: event_subscriber }
exception.default_html:
class: Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber
tags:
- { name: event_subscriber }
arguments: ['@html_fragment_renderer', '@html_page_renderer']
exception.default:
class: Drupal\Core\EventSubscriber\DefaultExceptionSubscriber
tags:
- { name: event_subscriber }
arguments: ['@html_fragment_renderer', '@html_page_renderer', '@config.factory']
exception.logger:
class: Drupal\Core\EventSubscriber\ExceptionLoggingSubscriber
tags:
- { name: event_subscriber }
arguments: ['@logger.factory']
exception.custom_page_json:
class: Drupal\Core\EventSubscriber\ExceptionJsonSubscriber
tags:
- { name: event_subscriber }
arguments: ['@config.factory', '@path.alias_manager', '@http_kernel']
exception.custom_page_html:
class: Drupal\Core\EventSubscriber\CustomPageExceptionHtmlSubscriber
tags:
- { name: event_subscriber }
arguments: ['@config.factory', '@path.alias_manager', '@http_kernel', '@logger.channel.php']
exception.fast_404_html:
class: Drupal\Core\EventSubscriber\Fast404ExceptionHtmlSubscriber
tags:
- { name: event_subscriber }
arguments: ['@config.factory', '@http_kernel']
exception.test_site:
class: Drupal\Core\EventSubscriber\ExceptionTestSiteSubscriber
tags:
- { name: event_subscriber }
arguments: [['@exception_controller', execute]]
route_processor_manager:
class: Drupal\Core\RouteProcessor\RouteProcessorManager
tags:
......
......@@ -573,7 +573,7 @@ function menu_local_tasks($level = 0) {
$data['actions'] = array();
$route_name = \Drupal::routeMatch()->getRouteName();
if (!empty($route_name)) {
if (!\Drupal::request()->attributes->has('exception') && !empty($route_name)) {
$manager = \Drupal::service('plugin.manager.menu.local_task');
$local_tasks = $manager->getTasksBuild($route_name);
foreach ($local_tasks as $level => $items) {
......
......@@ -23,7 +23,7 @@ class ContentNegotiation {
* The normalized type is a short, lowercase version of the format, such as
* 'html', 'json' or 'atom'.
*
* @param Symfony\Component\HttpFoundation\Request $request
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object from which to extract the content type.
*
* @return string
......
......@@ -70,8 +70,8 @@ public function onException(GetResponseForExceptionEvent $event) {
* Cookie provider to send all relevant session data to the user.
*/
public static function getSubscribedEvents() {
$events[KernelEvents::RESPONSE][] = array('onRespond', 0);
$events[KernelEvents::EXCEPTION][] = array('onException', 0);
$events[KernelEvents::RESPONSE][] = ['onRespond', 0];
$events[KernelEvents::EXCEPTION][] = ['onException', 75];
return $events;
}
}
<?php
/**
* @file
* Contains \Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber.
*/
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Path\AliasManagerInterface;
use Drupal\Core\Utility\Error;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
/**
* Exception subscriber for handling core custom error pages.
*/
class CustomPageExceptionHtmlSubscriber extends HttpExceptionSubscriberBase {
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* The page alias manager.
*
* @var \Drupal\Core\Path\AliasManagerInterface
*/
protected $aliasManager;
/**
* The HTTP kernel.
*
* @var \Symfony\Component\HttpKernel\HttpKernelInterface
*/
protected $httpKernel;
/**
* The logger instance.
*
* @var \Psr\Log\LoggerInterface
*/
protected $logger;
/**
* Constructs a new CustomPageExceptionHtmlSubscriber.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
* @param \Drupal\Core\Path\AliasManagerInterface $alias_manager
* The alias manager service.
* @param \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel
* The HTTP Kernel service.
* @param \Psr\Log\LoggerInterface $logger
* The logger service.
*/
public function __construct(ConfigFactoryInterface $config_factory, AliasManagerInterface $alias_manager, HttpKernelInterface $http_kernel, LoggerInterface $logger) {
$this->configFactory = $config_factory;
$this->aliasManager = $alias_manager;
$this->httpKernel = $http_kernel;
$this->logger = $logger;
}
/**
* {@inheritdoc}
*/
protected static function getPriority() {
return -50;
}
/**
* {@inheritDoc}
*/
protected function getHandledFormats() {
return ['html'];
}
/**
* Handles a 403 error for HTML.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process.
*/
public function on403(GetResponseForExceptionEvent $event) {
$path = $this->aliasManager->getPathByAlias($this->configFactory->get('system.site')->get('page.403'));
$this->makeSubrequest($event, $path, Response::HTTP_FORBIDDEN);
}
/**
* Handles a 404 error for HTML.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process.
*/
public function on404(GetResponseForExceptionEvent $event) {
$path = $this->aliasManager->getPathByAlias($this->configFactory->get('system.site')->get('page.404'));
$this->makeSubrequest($event, $path, Response::HTTP_NOT_FOUND);
}
/**
* Makes a subrequest to retrieve a custom error page.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process
* @param string $path
* The path to which to make a subrequest for this error message.
* @param int $status_code
* The status code for the error being handled.
*/
protected function makeSubrequest(GetResponseForExceptionEvent $event, $path, $status_code) {
$request = $event->getRequest();
// @todo Remove dependency on the internal _system_path attribute:
// https://www.drupal.org/node/2293523.
$system_path = $request->attributes->get('_system_path');
if ($path && $path != $system_path) {
// @todo The create() method expects a slash-prefixed path, but we store a
// normal system path in the site_404 variable.
if ($request->getMethod() === 'POST') {
$sub_request = Request::create($request->getBaseUrl() . '/' . $path, 'POST', ['destination' => $system_path, '_exception_statuscode' => $status_code] + $request->request->all(), $request->cookies->all(), [], $request->server->all());
}
else {
$sub_request = Request::create($request->getBaseUrl() . '/' . $path, 'GET', $request->query->all() + ['destination' => $system_path, '_exception_statuscode' => $status_code], $request->cookies->all(), [], $request->server->all());
}
try {
$response = $this->httpKernel->handle($sub_request, HttpKernelInterface::SUB_REQUEST);
$response->setStatusCode($status_code);
$event->setResponse($response);
}
catch (\Exception $e) {
// If an error happened in the subrequest we can't do much else.
// Instead, just log it. The DefaultExceptionHandler will catch the
// original exception and handle it normally.
$error = Error::decodeException($e);
$this->logger->log($error['severity_level'], '%type: !message in %function (line %line of %file).', $error);
}
}
}
}
<?php
/**
* @file
* Contains \Drupal\Core\EventSubscriber\DefaultExceptionHtmlSubscriber.
*/
namespace Drupal\Core\EventSubscriber;
use Drupal\Core\Page\HtmlFragment;
use Drupal\Core\Page\HtmlFragmentRendererInterface;
use Drupal\Core\Page\HtmlPageRendererInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
/**
* Handle most HTTP errors for HTML.
*/
class DefaultExceptionHtmlSubscriber extends HttpExceptionSubscriberBase {
use StringTranslationTrait;
/**
* The HTML fragment renderer.
*
* @var \Drupal\Core\Page\HtmlFragmentRendererInterface
*/
protected $fragmentRenderer;
/**
* The HTML page renderer.
*
* @var \Drupal\Core\Page\HtmlPageRendererInterface
*/
protected $htmlPageRenderer;
/**
* Constructs a new DefaultExceptionHtmlSubscriber.
*
* @param \Drupal\Core\Page\HtmlFragmentRendererInterface $fragment_renderer
* The fragment renderer.
* @param \Drupal\Core\Page\HtmlPageRendererInterface $page_renderer
* The page renderer.
*/
public function __construct(HtmlFragmentRendererInterface $fragment_renderer, HtmlPageRendererInterface $page_renderer) {
$this->fragmentRenderer = $fragment_renderer;
$this->htmlPageRenderer = $page_renderer;
}
/**
* {@inheritdoc}
*/
protected static function getPriority() {
// A very low priority so that custom handlers are almost certain to fire
// before it, even if someone forgets to set a priority.
return -128;
}
/**
* {@inheritDoc}
*/
protected function getHandledFormats() {
return ['html'];
}
/**
* Handles a 403 error for HTML.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process.
*/
public function on403(GetResponseForExceptionEvent $event) {
$response = $this->createResponse($this->t('Access denied'), $this->t('You are not authorized to access this page.'), Response::HTTP_FORBIDDEN);
$response->headers->set('Content-type', 'text/html');
$event->setResponse($response);
}
/**
* Handles a 404 error for HTML.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process.
*/
public function on404(GetResponseForExceptionEvent $event) {
$path = $event->getRequest()->getPathInfo();
$response = $this->createResponse($this->t('Page not found'), $this->t('The requested page "@path" could not be found.', ['@path' => $path]), Response::HTTP_NOT_FOUND);
$response->headers->set('Content-type', 'text/html');
$event->setResponse($response);
}
/**
* Handles a 405 error for HTML.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process.
*/
public function on405(GetResponseForExceptionEvent $event) {
$response = new Response('Method Not Allowed', Response::HTTP_METHOD_NOT_ALLOWED);
$response->headers->set('Content-type', 'text/html');
$event->setResponse($response);
}
/**
* @param $title
* The page title of the response.
* @param $body
* The body of the error page.
* @param $response_code
* The HTTP response code of the response.
* @return Response
* An error Response object ready to return to the browser.
*/
protected function createResponse($title, $body, $response_code) {
$fragment = new HtmlFragment($body);
$fragment->setTitle($title);
$page = $this->fragmentRenderer->render($fragment, $response_code);
return new Response($this->htmlPageRenderer->render($page), $page->getStatusCode());
}
}
<?php
/**
* @file
* Contains \Drupal\Core\EventSubscriber\DefaultExceptionSubscriber.
*/
namespace Drupal\Core\EventSubscriber;
use Drupal\Component\Utility\SafeMarkup;
use Drupal\Component\Utility\String;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\ContentNegotiation;
use Drupal\Core\Page\DefaultHtmlPageRenderer;
use Drupal\Core\Page\HtmlFragment;
use Drupal\Core\Page\HtmlFragmentRendererInterface;
use Drupal\Core\Page\HtmlPageRendererInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Utility\Error;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Last-chance handler for exceptions.
*
* This handler will catch any exceptions not caught elsewhere and report
* them as an error page.
*/
class DefaultExceptionSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* The fragment renderer.
*
* @var \Drupal\Core\Page\HtmlFragmentRendererInterface
*/
protected $fragmentRenderer;
/**
* The page renderer.
*
* @var \Drupal\Core\Page\HtmlPageRendererInterface
*/
protected $htmlPageRenderer;
/**
* @var string
*
* One of the error level constants defined in bootstrap.inc.
*/
protected $errorLevel;
/**
* The config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a new DefaultExceptionHtmlSubscriber.
*
* @param \Drupal\Core\Page\HtmlFragmentRendererInterface $fragment_renderer
* The fragment renderer.
* @param \Drupal\Core\Page\HtmlPageRendererInterface $page_renderer
* The page renderer.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
*/
public function __construct(HtmlFragmentRendererInterface $fragment_renderer, HtmlPageRendererInterface $page_renderer, ConfigFactoryInterface $config_factory) {
$this->fragmentRenderer = $fragment_renderer;
$this->htmlPageRenderer = $page_renderer;
$this->configFactory = $config_factory;
}
/**
* Gets the configured error level.
*
* @return string
*/
protected function getErrorLevel() {
if (!isset($this->errorLevel)) {
$this->errorLevel = $this->configFactory->get('system.logging')->get('error_level');
}
return $this->errorLevel;
}
/**
* Handles any exception as a generic error page for HTML.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process.
*/
protected function onHtml(GetResponseForExceptionEvent $event) {
$exception = $event->getException();
$error = Error::decodeException($exception);
$flatten_exception = FlattenException::create($exception, 500);
// Display the message if the current error reporting level allows this type
// of message to be displayed, and unconditionally in update.php.
if (error_displayable($error)) {
$class = 'error';
// If error type is 'User notice' then treat it as debug information
// instead of an error message.
// @see debug()
if ($error['%type'] == 'User notice') {
$error['%type'] = 'Debug';
$class = 'status';
}
// Attempt to reduce verbosity by removing DRUPAL_ROOT from the file path
// in the message. This does not happen for (false) security.
$root_length = strlen(DRUPAL_ROOT);
if (substr($error['%file'], 0, $root_length) == DRUPAL_ROOT) {
$error['%file'] = substr($error['%file'], $root_length + 1);
}
// Do not translate the string to avoid errors producing more errors.
unset($error['backtrace']);
$message = String::format('%type: !message in %function (line %line of %file).', $error);
// Check if verbose error reporting is on.
if ($this->getErrorLevel() == ERROR_REPORTING_DISPLAY_VERBOSE) {
$backtrace_exception = $flatten_exception;
while ($backtrace_exception->getPrevious()) {
$backtrace_exception = $backtrace_exception->getPrevious();
}
$backtrace = $backtrace_exception->getTrace();
// First trace is the error itself, already contained in the message.
// While the second trace is the error source and also contained in the
// message, the message doesn't contain argument values, so we output it
// once more in the backtrace.
array_shift($backtrace);
// Generate a backtrace containing only scalar argument values.
$message .= '<pre class="backtrace">' . Error::formatFlattenedBacktrace($backtrace) . '</pre>';
}
drupal_set_message(SafeMarkup::set($message), $class, TRUE);
}
$content = $this->t('The website has encountered an error. Please try again later.');
$output = DefaultHtmlPageRenderer::renderPage($content, $this->t('Error'));
$response = new Response($output);
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
}
else {
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR, '500 Service unavailable (with message)');
}
$event->setResponse($response);
}
/**
* Handles any exception as a generic error page for JSON.
*
* @todo This should probably check the error reporting level.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process.
*/
protected function onJson(GetResponseForExceptionEvent $event) {
$exception = $event->getException();
$error = Error::decodeException($exception);
// Display the message if the current error reporting level allows this type
// of message to be displayed,
$message = error_displayable($error) ? 'A fatal error occurred: ' . $exception->getMessage() : '';
// @todo We would prefer to use JsonResponse here, but existing code and
// tests are not prepared for parsing a JSON response when there are quotes
// or other values that would cause escaping issues. Instead, return a
// plain string and mark it as such.
$response = new Response($message, Response::HTTP_INTERNAL_SERVER_ERROR, [
'Content-type' => 'text/plain'
]);
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
}
$event->setResponse($response);
}
/**
* Creates an Html response for the provided criteria.
*
* @param $title
* The page title of the response.
* @param $body
* The body of the error page.
* @param $response_code
* The HTTP response code of the response.
* @return \Symfony\Component\HttpFoundation\Response
* An error Response object ready to return to the browser.
*/
protected function createHtmlResponse($title, $body, $response_code) {
$fragment = new HtmlFragment($body);
$fragment->setTitle($title);
$page = $this->fragmentRenderer->render($fragment, $response_code);
return new Response($this->htmlPageRenderer->render($page), $page->getStatusCode());
}
/**
* Handles errors for this subscriber.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process.
*/
public function onException(GetResponseForExceptionEvent $event) {
$format = $this->getFormat($event->getRequest());
// If it's an unrecognized format, assume HTML.
$method = 'on' . $format;
if (!method_exists($this, $method)) {
$method = 'onHtml';
}
$this->$method($event);
}
/**
* Gets the error-relevant format from the request.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The request object.
*
* @return string
* The format as which to treat the exception.
*/
protected function getFormat(Request $request) {
// @todo We are trying to switch to a more robust content negotiation
// library in https://www.drupal.org/node/1505080 that will make
// $request->getRequestFormat() reliable as a better alternative
// to this code. We therefore use this style for now on the expectation
// that it will get replaced with better code later. This approach makes
// that change easier when we get to it.
$conneg = new ContentNegotiation();
$format = $conneg->getContentType($request);
// These are all JSON errors for our purposes. Any special handling for
// them can/should happen in earlier listeners if desired.
if (in_array($format, ['drupal_modal', 'drupal_dialog', 'drupal_ajax'])) {
$format = 'json';
}
// Make an educated guess that any Accept header type that includes "json"
// can probably handle a generic JSON response for errors. As above, for
// any format this doesn't catch or that wants custom handling should
// register its own exception listener.
foreach ($request->getAcceptableContentTypes() as $mime) {
if (strpos($mime, 'html') === FALSE && strpos($mime, 'json') !== FALSE) {
$format = 'json';
}
}
return $format;
}
/**
* Registers the methods in this class that should be listeners.
*
* @return array
* An array of event listener definitions.
*/
public static function getSubscribedEvents() {
$events[KernelEvents::EXCEPTION][] = ['onException', -256];
return $events;
}
}
<?php
/**
* @file
* Contains \Drupal\Core\EventSubscriber\ExceptionJsonSubscriber.
*/
namespace Drupal\Core\EventSubscriber;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
/**
* Default handling for JSON errors.
*/
class ExceptionJsonSubscriber extends HttpExceptionSubscriberBase {
/**
* {@inheritDoc}
*/
protected function getHandledFormats() {
return ['json'];
}
/**
* {@inheritdoc}
*/
protected static function getPriority() {
// This will fire after the most common HTML handler, since HTML requests
// are still more common than JSON requests.
return -75;
}
/**
* Handles a 403 error for JSON.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process.
*/
public function on403(GetResponseForExceptionEvent $event) {
$response = new JsonResponse(NULL, Response::HTTP_FORBIDDEN);
$event->setResponse($response);
}
/**
* Handles a 404 error for JSON.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process.
*/