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

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


$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.


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


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)) {
    if (!defined('MAINTENANCE_MODE')) {
      define('MAINTENANCE_MODE', 'error');

  // 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(
        'function' => $error['%function'],
        'file' => $error['%file'],
        'line' => $error['%line'],
    backdrop_add_http_header('X-Backdrop-Assertion-' . $number, rawurlencode(serialize($assertion)));

  // 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";

  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);
  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) {
      // 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']));