You are here

public static function Error::decodeException in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Utility/Error.php \Drupal\Core\Utility\Error::decodeException()
  2. 9 core/lib/Drupal/Core/Utility/Error.php \Drupal\Core\Utility\Error::decodeException()

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().

9 calls to Error::decodeException()
DefaultExceptionHtmlSubscriber::makeSubrequest in core/lib/Drupal/Core/EventSubscriber/DefaultExceptionHtmlSubscriber.php
Makes a subrequest to retrieve the default error page.
ExceptionLoggingSubscriber::on403 in core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php
Log 403 errors.
ExceptionLoggingSubscriber::onError in core/lib/Drupal/Core/EventSubscriber/ExceptionLoggingSubscriber.php
Log not-otherwise-specified errors, including HTTP 500.
FinalExceptionSubscriber::onException in core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php
Handles exceptions for this subscriber.
MigrateExecutable::handleException in core/modules/migrate/src/MigrateExecutable.php
Takes an Exception object and both saves and displays it.

... See full list

File

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

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, [
    '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) {
    $driver_namespace = Database::getConnectionInfo()['default']['namespace'];
    $backtrace = Log::removeDatabaseEntries($backtrace, $driver_namespace);
    if (isset($exception->query_string, $exception->args)) {
      $message .= ": " . $exception->query_string . "; " . print_r($exception->args, TRUE);
    }
  }
  $caller = static::getLastCaller($backtrace);
  return [
    '%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(),
    'exception' => $exception,
  ];
}