note 74881 added to function.set-error-handler
| From: | silkensedai at online dot fr | Date: | Wed, 02 May 2007 07:37:14 +0000 |
| Subject: | note 74881 added to function.set-error-handler | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126111@lists.php.net to get a copy of this message | ||
i made an error handler that print also the backtrace and that can die on some errors. It can be
useful if you want to die on every error you find.
<?php
function my_error_handler($errno, $errstr, $errfile, $errline){
$errno = $errno & error_reporting();
if($errno == 0) return;
if(!defined('E_STRICT')) define('E_STRICT', 2048);
if(!defined('E_RECOVERABLE_ERROR')) define('E_RECOVERABLE_ERROR', 4096);
print "<pre>\n<b>";
switch($errno){
case E_ERROR: print "Error"; break;
case E_WARNING: print "Warning"; break;
case E_PARSE: print "Parse Error"; break;
case E_NOTICE: print "Notice"; break;
case E_CORE_ERROR: print "Core Error"; break;
case E_CORE_WARNING: print "Core Warning"; break;
case E_COMPILE_ERROR: print "Compile Error"; break;
case E_COMPILE_WARNING: print "Compile Warning"; break;
case E_USER_ERROR: print "User Error"; break;
case E_USER_WARNING: print "User Warning"; break;
case E_USER_NOTICE: print "User Notice"; break;
case E_STRICT: print "Strict Notice"; break;
case E_RECOVERABLE_ERROR: print "Recoverable Error"; break;
default: print "Unknown error ($errno)"; break;
}
print ":</b> <i>$errstr</i> in <b>$errfile</b> on line
<b>$errline</b>\n";
if(function_exists('debug_backtrace')){
//print "backtrace:\n";
$backtrace = debug_backtrace();
array_shift($backtrace);
foreach($backtrace as $i=>$l){
print "[$i] in function
<b>{$l['class']}{$l['type']}{$l['function']}</b>";
if($l['file']) print " in
<b>{$l['file']}</b>";
if($l['line']) print " on line
<b>{$l['line']}</b>";
print "\n";
}
}
print "\n</pre>";
if(isset($GLOBALS['error_fatal'])){
if($GLOBALS['error_fatal'] & $errno) die('fatal');
}
}
function error_fatal($mask = NULL){
if(!is_null($mask)){
$GLOBALS['error_fatal'] = $mask;
}elseif(!isset($GLOBALS['die_on'])){
$GLOBALS['error_fatal'] = 0;
}
return $GLOBALS['error_fatal'];
}
?>
Usage :
<?php
error_reporting(E_ALL); // will report all errors
set_error_handler('my_error_handler');
error_fatal(E_ALL^E_NOTICE); // will die on any error except E_NOTICE
?>
----
Server IP: 195.221.21.36
Probable Submitter: 194.206.50.185
----
Manual Page -- http://www.php.net/manual/en/function.set-error-handler.php
Edit -- https://master.php.net/note/edit/74881
Del: integrated -- https://master.php.net/note/delete/74881/integrated
Del: useless -- https://master.php.net/note/delete/74881/useless
Del: bad code -- https://master.php.net/note/delete/74881/bad+code
Del: spam -- https://master.php.net/note/delete/74881/spam
Del: non-english -- https://master.php.net/note/delete/74881/non-english
Del: in docs -- https://master.php.net/note/delete/74881/in+docs
Del: other reasons-- https://master.php.net/note/delete/74881
Reject -- https://master.php.net/note/reject/74881
Search -- https://master.php.net/manage/user-notes.php