You are here

public function Js::errorHandler in JS Callback Handler 8.3

Provides custom PHP error handling.

Parameters

$error_level: The level of the error raised.

$message: The error message.

File

src/Js.php, line 314

Class

Js
JS Callback Handler service.

Namespace

Drupal\js

Code

public function errorHandler($error_level, $message) {
  if ($error_level & error_reporting()) {
    require_once \Drupal::root() . '/core/includes/errors.inc';
    $types = drupal_error_levels();
    list($severity_msg, $severity_level) = $types[$error_level];
    $backtrace = debug_backtrace();
    $caller = $this
      ->getLastCaller($backtrace);

    // We treat recoverable errors as fatal.
    $recoverable = $error_level == E_RECOVERABLE_ERROR;

    // As __toString() methods must not throw exceptions (recoverable errors)
    // in PHP, we allow them to trigger a fatal error by emitting a user error
    // using trigger_error().
    $to_string = $error_level == E_USER_ERROR && substr($caller['function'], -strlen('__toString()')) == '__toString()';
    $error = [
      '%type' => isset($types[$error_level]) ? $severity_msg : 'Unknown error',
      '@message' => Markup::create(Xss::filterAdmin($message)),
      '%function' => $caller['function'],
      '%file' => $caller['file'],
      '%line' => $caller['line'],
      'severity_level' => $severity_level,
      'backtrace' => $backtrace,
    ];
    static::logError($error, $recoverable || $to_string);
  }
}