DefaultExceptionSubscriber::onHtml

protected DefaultExceptionSubscriber::onHtml(GetResponseForExceptionEvent $event)

Handles any exception as a generic error page for HTML.

Parameters

\Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event: The event to process.

File

core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php, line 68

Class

DefaultExceptionSubscriber
Last-chance handler for exceptions.

Namespace

Drupal\Core\EventSubscriber

Code

protected function onHtml(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 (error_displayable($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';
    }

    // 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);
    }

    unset($error['backtrace']);

    if ($this->getErrorLevel() != ERROR_REPORTING_DISPLAY_VERBOSE) {
      // Without verbose logging, use a simple message.

      // We call SafeMarkup::format 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 = SafeMarkup::format('%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 = SafeMarkup::format('%type: @message in %function (line %line of %file). <pre class="backtrace">@backtrace</pre>', $error);
    }
  }

  $content = $this->t('The website encountered an unexpected error. Please try again later.');
  $content .= $message ? '</br></br>' . $message : '';
  $response = new Response($content, 500);

  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);
}
doc_Drupal
2016-10-29 09:01:40
Comments
Leave a Comment

Please login to continue.