|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2012-10-03 09:59 UTC] gwarnants at gmail dot com
Description:
------------
Dear PHP Team,
I don't know if it's a bug but I discovered a strange behavior :
When setting a custom error handler, we can choose an error mask as a 2nd parameter to catch only some kinds of errors.
If I set 2 error handlers with differents masks, and trying to restore back my first handler by calling restore_error_handler(), my first handler is now running with the second error mask
Perhaps it is a normal behavior because set_error_handler() is setting a "global" error mask, but if it is, I think there is no
way to know the previous error_mask to restore it properly (that value isn't returned by error_reporting())
Regards,
Geoffray
Test script:
---------------
class CustomErrorHandler
{
public static function handler($errrno, $errstr)
{
echo "HANDLING: $errstr\n";
// i set an internal error_handler other catch WARNINGS ONLY
set_error_handler(array('CustomErrorHandler', 'internal_handler'), E_WARNING|E_USER_WARNING);
fopen('file_not_found.dat', 'r'); // will trigger a E_WARNING
restore_error_handler(); // doing this i think previous handler will be restored to E_ALL... but it's not ??
}
private static function internal_handler($errrno, $errstr)
{
echo " INTERNAL HANDLER: $errstr\n";
}
}
set_error_handler(array('CustomErrorHandler', 'handler'), E_ALL);
trigger_error('User notice 1', E_USER_NOTICE);
trigger_error('User warning 1', E_USER_WARNING);
trigger_error('User notice 2', E_USER_NOTICE); // will not be caught !
trigger_error('User warning 2', E_USER_WARNING);
Expected result:
----------------
HANDLING: User notice 1
INTERNAL HANDLER: fopen(file_not_found.dat): failed to open stream: No such file or directory
HANDLING: User warning 1
INTERNAL HANDLER: fopen(file_not_found.dat): failed to open stream: No such file or directory
HANDLING: User notice 2
INTERNAL HANDLER: fopen(file_not_found.dat): failed to open stream: No such file or directory
HANDLING: User warning 2
INTERNAL HANDLER: fopen(file_not_found.dat): failed to open stream: No such file or directory
Actual result:
--------------
HANDLING: User notice 1
INTERNAL HANDLER: fopen(file_not_found.dat): failed to open stream: No such file or directory
HANDLING: User warning 1
INTERNAL HANDLER: fopen(file_not_found.dat): failed to open stream: No such file or directory
Notice: User notice 2 in D:\wamp\www\custom_error_handler.php on line 29
HANDLING: User warning 2
INTERNAL HANDLER: fopen(file_not_found.dat): failed to open stream: No such file or directory
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 20:00:01 2025 UTC |
Here is a simpler example to illustrate the problem. As you see, the 2nd trigger_error will not be caught because the error reporting isn't restored to its initial state. set_error_handler('handler1', E_ALL); trigger_error('notice 1', E_USER_NOTICE); // OK trigger_error('notice 2', E_USER_NOTICE); // not caught ! function handler1($errrno, $errstr) { echo "HANDLER1 : $errstr\n"; set_error_handler('handler2', E_USER_WARNING); restore_error_handler(); } function handler2($errrno, $errstr) { }