1.20.x errors.inc _backdrop_log_error($error, $fatal = FALSE)

Logs a PHP error or exception and displays an error page 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.

File

includes/errors.inc, line 181
Functions for error handling.

Code

function _backdrop_log_error($error, $fatal = FALSE) {
  // Initialize a maintenance theme if the bootstrap was not complete.
  // Do it early because backdrop_set_message() triggers a backdrop_theme_initialize().
  if ($fatal && (backdrop_get_bootstrap_phase() != BACKDROP_BOOTSTRAP_FULL)) {
    unset($GLOBALS['theme']);
    if (!defined('MAINTENANCE_MODE')) {
      define('MAINTENANCE_MODE', 'error');
    }
    backdrop_maintenance_theme();
  }

  // When running inside the testing framework, we relay the errors
  // to the tested site by the way of HTTP headers.
  $test_info = &$GLOBALS['backdrop_test_info'];
  if (!empty($test_info['in_child_site']) && !headers_sent() && (!defined('SIMPLETEST_COLLECT_ERRORS') || SIMPLETEST_COLLECT_ERRORS)) {
    // $number does not use backdrop_static as it should not be reset
    // as it uniquely identifies each PHP error.
    static $number = 0;
    $assertion = array(
      $error['!message'],
      $error['%type'],
      array(
        'function' => $error['%function'],
        'file' => $error['%file'],
        'line' => $error['%line'],
      ),
    );
    backdrop_add_http_header('X-Backdrop-Assertion-' . $number, rawurlencode(serialize($assertion)));
    $number++;
  }

  // Suppress errors here in the event that the database is not accessible,
  // preventing cascading errors.
  @watchdog('php', '%type: !message in %function (line %line of %file).', $error, $error['severity_level']);

  if ($fatal) {
    backdrop_add_http_header('Status', '500 Service unavailable (with message)');
  }

  if (backdrop_is_cli()) {
    if ($fatal) {
      // When called from CLI, output a plain text message.
      print html_entity_decode(strip_tags(t('%type: !message in %function (line %line of %file).', $error))) . "\n";
      exit;
    }
  }

  if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' && $fatal) {
    if (error_displayable($error)) {
      // When called from JavaScript, output the error message.
      print t('%type: !message in %function (line %line of %file).', $error);
    }
    exit;
  }
  else {
    // Display the message if the current error reporting level allows this type
    // of message to be displayed, and unconditionally in update.php.
    if (error_displayable($error)) {
      $class = 'error';

      // If error type is 'User notice' then treat it as debug information
      // instead of an error message, see dd().
      if ($error['%type'] == 'User notice') {
        $error['%type'] = 'Debug';
        $class = 'status';
      }

      backdrop_set_message(t('%type: !message in %function (line %line of %file).', $error), $class);
    }

    if ($fatal) {
      backdrop_set_title(t('Error'));
      // We fallback to a maintenance page at this point, because the page generation
      // itself can generate errors. Provide the error message to the end-user
      // but exclude PHP error information.
      print theme('maintenance_page', array('content' => $error['!message']));
      exit;
    }
  }
}