8.3.x Error.php public static Error::decodeException($exception)
8.0.x Error.php public static Error::decodeException($exception)
8.1.x Error.php public static Error::decodeException($exception)
8.2.x Error.php public static Error::decodeException($exception)
8.4.x Error.php public static Error::decodeException($exception)

Decodes an exception and retrieves the correct caller.

Parameters

\Exception|\Throwable $exception: The exception object that was thrown.

Return value

array An error in the format expected by _drupal_log_error().

12 calls to Error::decodeException()
DefaultExceptionHtmlSubscriber::makeSubrequest in core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php
Makes a subrequest to retrieve the default error page.
DefaultExceptionSubscriber::onHtml in core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php
Handles any exception as a generic error page for HTML.
DefaultExceptionSubscriber::onJson in core/lib/Drupal/Core/EventSubscriber/DefaultExceptionSubscriber.php
Handles any exception as a generic error page for JSON.
Error::renderExceptionSafe in core/lib/Drupal/Core/Utility/Error.php
Renders an exception error message without further exceptions.
ExceptionLoggingSubscriber::onError in core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php
Log not-otherwise-specified errors, including HTTP 500.

... See full list

File

core/lib/Drupal/Core/Utility/Error.php, line 37

Class

Error
Drupal error utility class.

Namespace

Drupal\Core\Utility

Code

public static function decodeException($exception) {
  $message = $exception->getMessage();

  $backtrace = $exception->getTrace();
  // Add the line throwing the exception to the backtrace.
  array_unshift($backtrace, array('line' => $exception->getLine(), 'file' => $exception->getFile()));

  // For PDOException errors, we try to return the initial caller,
  // skipping internal functions of the database layer.
  if ($exception instanceof \PDOException || $exception instanceof DatabaseExceptionWrapper) {
    // The first element in the stack is the call, the second element gives us
    // the caller. We skip calls that occurred in one of the classes of the
    // database layer or in one of its global functions.
    $db_functions = array('db_query', 'db_query_range');
    while (!empty($backtrace[1]) && ($caller = $backtrace[1]) && 
      ((isset($caller['class']) && (strpos($caller['class'], 'Query') !== FALSE || strpos($caller['class'], 'Database') !== FALSE || strpos($caller['class'], 'PDO') !== FALSE)) ||
        in_array($caller['function'], $db_functions))) {
      // We remove that call.
      array_shift($backtrace);
    }
    if (isset($exception->query_string, $exception->args)) {
      $message .= ": " . $exception->query_string . "; " . print_r($exception->args, TRUE);
    }
  }

  $caller = static::getLastCaller($backtrace);

  return array(
    '%type' => get_class($exception),
    // The standard PHP exception handler considers that the exception message
    // is plain-text. We mimic this behavior here.
    '@message' => $message,
    '%function' => $caller['function'],
    '%file' => $caller['file'],
    '%line' => $caller['line'],
    'severity_level' => static::ERROR,
    'backtrace' => $backtrace,
    'backtrace_string' => $exception->getTraceAsString(),
  );
}