You are here

public function Js::logError in JS Callback Handler 8.3

Logs a PHP error or exception and displays the error in fatal cases.

Parameters

$error: An array with the following keys: %type, @ message, %function, %file, %line and severity_level. All the parameters are plain-text, with the exception of @ message, which needs to be a safe HTML string.

$fatal: TRUE if the error is fatal.

3 calls to Js::logError()
Js::errorHandler in src/Js.php
Provides custom PHP error handling.
Js::exceptionHandler in src/Js.php
Provides custom PHP exception handling.
Js::fatalErrorHandler in src/Js.php
Provides custom PHP fatal error handling.

File

src/Js.php, line 590

Class

Js
JS Callback Handler service.

Namespace

Drupal\js

Code

public function logError($error, $fatal = FALSE) {

  // Log the error immediately.
  if (\Drupal::hasService('logger.factory')) {
    try {
      \Drupal::logger('php')
        ->log($error['severity_level'], '%type: @message in %function (line %line of %file).', $error);
    } catch (\Exception $e) {

      // We can't log, for example because the database connection is not
      // available. At least try to log to PHP error log.
      error_log(strtr('Failed to log error: %type: @message in %function (line %line of %file).', $error));
    }
  }

  // Display the error to the user, if it should.
  if ($this
    ->isErrorDisplayable($error)) {
    if (!isset($error['%function'])) {
      drupal_set_message($this
        ->t('%type: @message (line %line of %file).', $error), 'error');
    }
    else {
      drupal_set_message($this
        ->t('%type: @message in %function (line %line of %file).', $error), 'error');
    }
  }

  // If fatal, deliver an internal server error response.
  if ($fatal) {
    $this
      ->getResponse()
      ->setStatusCode(500);
    $this
      ->deliver()
      ->prepare($this
      ->getRequest())
      ->send();
    exit;
  }
}