[php-src] Issue #12832: `set_error_handler()` does not set `error_get_last()` if handled

From: Date: Thu, 30 Nov 2023 01:12:37 +0000
Subject: [php-src] Issue #12832: `set_error_handler()` does not set `error_get_last()` if handled
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-245939@lists.php.net to get a copy of this message
Issue: https://github.com/php/php-src/issues/12832 Author: mvorisek ### Description related with https://github.com/sebastianbergmann/phpunit/issues/5587 PHPUnit 10 sets error handler to handle errors to a) log the errors, b) supress the error output. It achives that by returning true from the error handler [1]. When the error is handled with return value of true, the php docs say [2]: > It is important to remember that the standard PHP error handler is completely bypassed for the > error types specified by error_levels unless the callback function returns false. So it is documented well/expected. But such behaviour breaks code which expects error_get_last() to be set after some core function has returned a failure like: ``` $dirHandle = opendir($dirPath); if ($dirHandle === false) { $errorMessage = preg_replace('~^.+?:\s*~s', '', error_get_last()['message']); } ``` I tried to workaround by https://3v4l.org/kS1vM/rfc#vgit.master but an error emit from error handler itself is not handled. Actually it is a little worse, such error is emit before the original one! I want to achive the error output supressed by custom error handler, but still error_get_last() to be set and error_reporting(E_ALL) kept. Please advise if you can think of any hacky solution. For current master, I would be happy to discuss if error_get_last() should always be set even if an error is handled or doing so by introducing some error handler return value like 2 for it. [1] https://github.com/sebastianbergmann/phpunit/blob/10.4.2/src/Runner/ErrorHandler.php#L144 [2] https://www.php.net/manual/en/function.set-error-handler.php

« previous php.bugs (#245939) next »