You are here

function backtrace_error_handler in Devel 8

Same name and namespace in other branches
  1. 8.3 devel.module \backtrace_error_handler()
  2. 8.2 devel.module \backtrace_error_handler()
  3. 5 devel.module \backtrace_error_handler()
  4. 6 devel.module \backtrace_error_handler()
  5. 7 devel.module \backtrace_error_handler()
  6. 4.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 196
This module holds functions useful for Drupal development. Please contribute!

Code

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

  // Hide stack trace and parameters from unqualified users.
  if (!\Drupal::currentUser()
    ->hasPermission('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.)
    if ($error_level & error_reporting()) {
      $types = drupal_error_levels();
      list($severity_msg, $severity_level) = $types[$error_level];
      $backtrace = debug_backtrace();
      $caller = Error::getLastCaller($backtrace);

      // We treat recoverable errors as fatal.
      _drupal_log_error(array(
        '%type' => isset($types[$error_level]) ? $severity_msg : 'Unknown error',
        '@message' => $message,
        '%function' => $caller['function'],
        '%file' => $caller['file'],
        '%line' => $caller['line'],
        'severity_level' => $severity_level,
        'backtrace' => $backtrace,
      ), $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;
    }
    $types = drupal_error_levels();
    list($severity_msg, $severity_level) = $types[$error_level];
    $backtrace = debug_backtrace();
    $caller = Error::getLastCaller($backtrace);
    $variables = array(
      '%type' => isset($types[$error_level]) ? $severity_msg : 'Unknown error',
      '@message' => $message,
      '%function' => $caller['function'],
      '%file' => $caller['file'],
      '%line' => $caller['line'],
    );
    $msg = t('%type: @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 (\Drupal::config('system.logging')
      ->get('error_level') != 'hide') {
      $error_handlers = devel_get_handlers();
      if (!empty($error_handlers[DEVEL_ERROR_HANDLER_STANDARD])) {
        drupal_set_message($msg, $severity_level <= RfcLogLevel::NOTICE ? 'error' : 'warning', TRUE);
      }
      if (!empty($error_handlers[DEVEL_ERROR_HANDLER_BACKTRACE_KINT])) {
        print kpr(ddebug_backtrace(TRUE, 1), TRUE, $msg);
      }
      if (!empty($error_handlers[DEVEL_ERROR_HANDLER_BACKTRACE_DPM])) {
        dpm(ddebug_backtrace(TRUE, 1), $msg, 'warning');
      }
    }
    \Drupal::logger('php')
      ->log($severity_level, $msg);
  }
}