class FinalExceptionSubscriber
Same name in other branches
- 9 core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php \Drupal\Core\EventSubscriber\FinalExceptionSubscriber
- 10 core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php \Drupal\Core\EventSubscriber\FinalExceptionSubscriber
- 11.x core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php \Drupal\Core\EventSubscriber\FinalExceptionSubscriber
Last-chance handler for exceptions: the final exception subscriber.
This handler will catch any exceptions not caught elsewhere and report them as an error page.
Each format has its own way of handling exceptions:
- html: exception.default_html, exception.custom_page_html and exception.fast_404_html
- json: exception.default_json
And when the serialization module is installed, all serialization formats are handled by a single exception subscriber:: serialization.exception.default.
This exception subscriber runs after all the above (it has a lower priority), which makes it the last-chance exception handler. It always sends a plain text response. If it's a displayable error and the error level is configured to be verbose, then a helpful backtrace is also printed.
Hierarchy
- class \Drupal\Core\EventSubscriber\FinalExceptionSubscriber implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses \Drupal\Core\StringTranslation\StringTranslationTrait
Expanded class hierarchy of FinalExceptionSubscriber
1 file declares its use of FinalExceptionSubscriber
- FinalExceptionSubscriberTest.php in core/
tests/ Drupal/ Tests/ Core/ EventSubscriber/ FinalExceptionSubscriberTest.php
1 string reference to 'FinalExceptionSubscriber'
- core.services.yml in core/
core.services.yml - core/core.services.yml
1 service uses FinalExceptionSubscriber
File
-
core/
lib/ Drupal/ Core/ EventSubscriber/ FinalExceptionSubscriber.php, line 34
Namespace
Drupal\Core\EventSubscriberView source
class FinalExceptionSubscriber implements EventSubscriberInterface {
use StringTranslationTrait;
/**
* @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 FinalExceptionSubscriber.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
$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 exceptions for this subscriber.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event
* The event to process.
*/
public function onException(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, and unconditionally in update.php.
$message = '';
if ($this->isErrorDisplayable($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';
}
$error = $this->simplifyFileInError($error);
unset($error['backtrace']);
if (!$this->isErrorLevelVerbose()) {
// Without verbose logging, use a simple message.
// We use \Drupal\Component\Render\FormattableMarkup directly here,
// rather than use t() since we are in the middle of error handling, and
// we don't want t() to cause further errors.
$message = new FormattableMarkup('%type: @message in %function (line %line of %file).', $error);
}
else {
// With verbose logging, we will also include a backtrace.
$backtrace_exception = $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.
$error['@backtrace'] = Error::formatBacktrace($backtrace);
$message = new FormattableMarkup('%type: @message in %function (line %line of %file). <pre class="backtrace">@backtrace</pre>', $error);
}
}
$content_type = $event->getRequest()
->getRequestFormat() == 'html' ? 'text/html' : 'text/plain';
$content = $this->t('The website encountered an unexpected error. Please try again later.');
$content .= $message ? '</br></br>' . $message : '';
$response = new Response($content, 500, [
'Content-Type' => $content_type,
]);
if ($exception instanceof HttpExceptionInterface) {
$response->setStatusCode($exception->getStatusCode());
$response->headers
->add($exception->getHeaders());
}
else {
$response->setStatusCode(Response::HTTP_INTERNAL_SERVER_ERROR, '500 Service unavailable (with message)');
}
$event->setResponse($response);
}
/**
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
// Run as the final (very late) KernelEvents::EXCEPTION subscriber.
$events[KernelEvents::EXCEPTION][] = [
'onException',
-256,
];
return $events;
}
/**
* Checks whether the error level is verbose or not.
*
* @return bool
*/
protected function isErrorLevelVerbose() {
return $this->getErrorLevel() === ERROR_REPORTING_DISPLAY_VERBOSE;
}
/**
* Wrapper for error_displayable().
*
* @param $error
* Optional error to examine for ERROR_REPORTING_DISPLAY_SOME.
*
* @return bool
*
* @see \error_displayable
*/
protected function isErrorDisplayable($error) {
return error_displayable($error);
}
/**
* Attempts to reduce error verbosity in the error message's file path.
*
* Attempts to reduce verbosity by removing DRUPAL_ROOT from the file path in
* the message. This does not happen for (false) security.
*
* @param $error
* Optional error to examine for ERROR_REPORTING_DISPLAY_SOME.
*
* @return
* The updated $error.
*/
protected function simplifyFileInError($error) {
// 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);
}
return $error;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overrides |
---|---|---|---|---|
FinalExceptionSubscriber::$configFactory | protected | property | The config factory. | |
FinalExceptionSubscriber::$errorLevel | protected | property | One of the error level constants defined in bootstrap.inc. | |
FinalExceptionSubscriber::getErrorLevel | protected | function | Gets the configured error level. | |
FinalExceptionSubscriber::getSubscribedEvents | public static | function | ||
FinalExceptionSubscriber::isErrorDisplayable | protected | function | Wrapper for error_displayable(). | 1 |
FinalExceptionSubscriber::isErrorLevelVerbose | protected | function | Checks whether the error level is verbose or not. | 1 |
FinalExceptionSubscriber::onException | public | function | Handles exceptions for this subscriber. | |
FinalExceptionSubscriber::simplifyFileInError | protected | function | Attempts to reduce error verbosity in the error message's file path. | 1 |
FinalExceptionSubscriber::__construct | public | function | Constructs a new FinalExceptionSubscriber. | |
StringTranslationTrait::$stringTranslation | protected | property | The string translation service. | |
StringTranslationTrait::formatPlural | protected | function | Formats a string containing a count of items. | |
StringTranslationTrait::getNumberOfPlurals | protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait::getStringTranslation | protected | function | Gets the string translation service. | |
StringTranslationTrait::setStringTranslation | public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait::t | protected | function | Translates a string to the current language or to a given language. |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.