You are here

public function Js::exceptionHandler in JS Callback Handler 8.3

Provides custom PHP exception handling.

Uncaught exceptions are those not enclosed in a try/catch block. They are always fatal: the execution of the script will stop as soon as the exception handler exits.

Parameters

$exception: The exception object that was thrown.

File

src/Js.php, line 354

Class

Js
JS Callback Handler service.

Namespace

Drupal\js

Code

public function exceptionHandler($exception) {
  require_once \Drupal::root() . '/core/includes/errors.inc';
  try {
    $this
      ->logError($this
      ->decodeException($exception), TRUE);
  } catch (\Exception $exception2) {

    // Another uncaught exception was thrown while handling the first one.
    // If we are displaying errors, then do so with no possibility of a further
    // uncaught exception being thrown.
    $message = '<h1>Additional uncaught exception thrown while handling exception.</h1>';
    $message .= '<h2>Original</h2><p>' . $this
      ->renderExceptionSafe($exception) . '</p>';
    $message .= '<h2>Additional</h2><p>' . $this
      ->renderExceptionSafe($exception2) . '</p>';
    $caller = $this
      ->getLastCaller(debug_backtrace());
    $error = [
      '%type' => 'Unknown error',
      // The standard PHP error handler considers that the error messages
      // are HTML. Mimic this behavior here.
      '@message' => Markup::create(Xss::filterAdmin($message)),
      '%function' => $caller['function'],
      '%file' => $caller['file'],
      '%line' => $caller['line'],
      'severity_level' => RfcLogLevel::ERROR,
    ];
    $this
      ->logError($error, TRUE);
  }
}