Same name and namespace in other branches
  1. 5.x-1.x devel.module \backtrace_error_handler()
  2. 6.x-1.x devel.module \backtrace_error_handler()
  3. 8.x-1.x devel.module \backtrace_error_handler()

Displays backtrace showing the route of calls to the current error.

Parameters

int $error_level: The level of the error raised.

string $message: The error message.

string $filename: The filename that the error was raised in.

int $line: The line number the error was raised at.

array $context: An array that points to the active symbol table at the point the error occurred.

1 string reference to 'backtrace_error_handler'
devel_set_handler in ./devel.module
Sets a new error handler or restores the prior one.

File

./devel.module, line 698
This module holds functions useful for Drupal development.

Code

function backtrace_error_handler($error_level, $message, $filename, $line, $context) {

  // Hide stack trace and parameters from unqualified users.
  if (!user_access('access devel information')) {

    // Do what core does in bootstrap.inc and errors.inc.
    // (We need to duplicate the core code here rather than calling it
    // to avoid having the backtrace_error_handler() on top of the call stack.)
    require_once DRUPAL_ROOT . '/includes/errors.inc';
    if ($error_level & error_reporting()) {
      $types = drupal_error_levels();
      list($severity_msg, $severity_level) = $types[$error_level];
      $backtrace = debug_backtrace();
      $caller = _drupal_get_last_caller($backtrace);
      if (!function_exists('filter_xss_admin')) {
        require_once DRUPAL_ROOT . '/includes/common.inc';
      }

      // We treat recoverable errors as fatal.
      _drupal_log_error(array(
        '%type' => isset($types[$error_level]) ? $severity_msg : 'Unknown error',
        // The standard PHP error handler considers that the error messages
        // are HTML. We mimick this behavior here.
        '!message' => filter_xss_admin($message),
        '%function' => $caller['function'],
        '%file' => $caller['file'],
        '%line' => $caller['line'],
        'severity_level' => $severity_level,
      ), $error_level == E_RECOVERABLE_ERROR);
    }
    return;
  }

  // Don't respond to the error if it was suppressed with a '@'
  if (error_reporting() == 0) {
    return;
  }

  // Don't respond to warning caused by ourselves.
  if (preg_match('#Cannot modify header information - headers already sent by \\([^\\)]*[/\\\\]devel[/\\\\]#', $message)) {
    return;
  }
  if ($error_level & error_reporting()) {

    // Only write each distinct NOTICE message once, as repeats do not give any
    // further information and can choke the page output.
    if ($error_level == E_NOTICE) {
      static $written = array();
      if (!empty($written[$line][$filename][$message])) {
        return;
      }
      $written[$line][$filename][$message] = TRUE;
    }
    require_once DRUPAL_ROOT . '/includes/errors.inc';
    $types = drupal_error_levels();
    $type = $types[$error_level];
    $backtrace = debug_backtrace();
    $variables = array(
      '%error' => $type[0],
      '%message' => $message,
      '%function' => $backtrace[1]['function'] . '()',
      '%file' => $filename,
      '%line' => $line,
    );
    $msg = t('%error: %message in %function (line %line of %file).', $variables);

    // Show message if error_level is ERROR_REPORTING_DISPLAY_SOME or higher.
    // (This is Drupal's error_level, which is different from $error_level,
    // and we purposely ignore the difference between _SOME and _ALL,
    // see #970688!)
    if (variable_get('error_level', 1) >= 1) {
      $error_handlers = devel_get_handlers();
      if (!empty($error_handlers[DEVEL_ERROR_HANDLER_STANDARD])) {
        drupal_set_message($msg, $type[1] <= WATCHDOG_ERROR ? 'error' : 'warning');
      }
      if (!empty($error_handlers[DEVEL_ERROR_HANDLER_BACKTRACE_KRUMO])) {
        print $msg . " =&gt;\n";
        ddebug_backtrace(FALSE, 1);
      }
      if (!empty($error_handlers[DEVEL_ERROR_HANDLER_BACKTRACE_DPM])) {
        dpm(ddebug_backtrace(TRUE, 1), $msg, 'warning');
      }
    }
    $watchdog = 'watchdog';
    $watchdog('php', $msg, NULL, $type[1]);
  }
}