How to override Drupal error handler in 6.x?

13 views
Skip to first unread message

kenorb

unread,
Nov 29, 2010, 5:15:16 AM11/29/10
to drupal-hackers
Drupal error handler is defined in common.inc
http://api.drupal.org/api/drupal/includes--common.inc/function/drupal_error_handler/6
And it's registered in _drupal_bootstrap_full() which is called from
bootstrap.inc during DRUPAL_BOOTSTRAP_FULL. It's after hook_boot, but
it's before hook_init (remember that init is not executed during
maintenance mode and update.

So we need to register it either in hook_boot and hook_init.
function foo_boot() {
set_error_handler('foo_api_drupal_error_handler');
}
function foo_init() {
set_error_handler('foo_api_drupal_error_handler');
}

/**
* Log errors as defined by administrator.
*
* Error levels:
* - 0 = Log errors to database.
* - 1 = Log errors to database and to screen.
*/
function foo_api_drupal_error_handler($errno, $message, $filename,
$line, $context) {
static $errors = array();
if (error_reporting() == 0 || !isset($errno, $message, $filename,
$line, $context)) {
return $errors;
}
drupal_error_handler($errno, $message, $filename, $line,
$context); // execute original callback
$types = array(1 => 'error', 2 => 'warning', 4 => 'parse error', 8
=> 'notice', 16 => 'core error', 32 => 'core warning', 64 => 'compile
error', 128 => 'compile warning', 256 => 'user error', 512 => 'user
warning', 1024 => 'user notice', 2048 => 'strict warning', 4096 =>
'recoverable fatal error');
$errors[$types[$errno]][] = array($message, $filename, $line,
$context); // save all errors into array
}
Reply all
Reply to author
Forward
0 new messages