Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct handling of E_USER_ERROR as fatal error if registerErrorHandl… #1670

Merged
merged 2 commits into from
Jun 9, 2022

Conversation

WoZ
Copy link
Contributor

@WoZ WoZ commented May 11, 2022

…er is called with callPrevious, fixes #1669

@WoZ
Copy link
Contributor Author

WoZ commented May 11, 2022

It's impossible to test shutdown functions with PHPUnit, so I did a manual check.

callPrev == true

use Monolog\ErrorHandler;

$logger = new Monolog\Logger('main');
$logger->pushHandler(new Monolog\Handler\StreamHandler('php://stderr', Monolog\Logger::DEBUG));

$errorHandler = new ErrorHandler($logger);
$errorHandler->registerErrorHandler([], true);
$errorHandler->registerFatalHandler();
$errorHandler->registerExceptionHandler();

trigger_error('Error', E_USER_ERROR);

Before Fix.

php-fpm_1  | NOTICE: PHP message: PHP Fatal error:  Error in /var/www/html/public/logger/index.php on line 15
php-fpm_1  | [2022-05-11T17:29:08.872957+00:00] main.ALERT: Fatal Error (E_USER_ERROR): Error {"code":256,"message":"Error","file":"/var/www/html/public/logger/index.php","line":15,"trace":[{"file":"/var/www/html/public/logger/index.php","line":15,"function":"trigger_error"}]} []

After Fix

php-fpm_1  | NOTICE: PHP message: PHP Fatal error:  Error in /var/www/html/public/logger/index.php on line 15
php-fpm_1  | [2022-05-11T17:25:43.907508+00:00] main.ALERT: Fatal Error (E_USER_ERROR): Error {"code":256,"message":"Error","file":"/var/www/html/public/logger/index.php","line":15,"trace":[{"file":"/var/www/html/public/logger/index.php","line":15,"function":"trigger_error"}]} []

Conclusion: No changes, as expected

callPrev == false

use Monolog\ErrorHandler;

$logger = new Monolog\Logger('main');
$logger->pushHandler(new Monolog\Handler\StreamHandler('php://stderr', Monolog\Logger::DEBUG));

$errorHandler = new ErrorHandler($logger);
$errorHandler->registerErrorHandler([], false);
$errorHandler->registerFatalHandler();
$errorHandler->registerExceptionHandler();

trigger_error('Error', E_USER_ERROR);

Before Fix.
Empty log.

After fix

php-fpm_1  | [2022-05-11T17:27:36.493950+00:00] main.ALERT: Fatal Error (E_USER_ERROR): Error {"code":256,"message":"Error","file":"/var/www/html/public/logger/index.php","line":15,"trace":[{"file":"/var/www/html/public/logger/index.php","line":15,"function":"trigger_error"}]} []

Conclusion: Fixes the issue.

Classical Fatal Error (non-cacthable by error handler

use Monolog\ErrorHandler;

$logger = new Monolog\Logger('main');
$logger->pushHandler(new Monolog\Handler\StreamHandler('php://stderr', Monolog\Logger::DEBUG));

$errorHandler = new ErrorHandler($logger);
$errorHandler->registerErrorHandler([], true);
$errorHandler->registerFatalHandler();
$errorHandler->registerExceptionHandler();

include('../error_handling/error_generators/fatal_error.php');
/** Code of error_handling/error_generators/fatal_error.php
 * <?php
 * function my_function(array $a) { echo $a; }
 * my_function(123.0);
 **/

Before fix.

php-fpm_1  | [2022-05-11T17:34:11.272915+00:00] main.ERROR: Uncaught Exception TypeError: "Argument 1 passed to my_function() must be of the type array, float given, called in /var/www/html/public/error_handling/error_generators/fatal_error.php on line 3" at /var/www/html/public/error_handling/error_generators/fatal_error.php line 2 {"exception":"[object] (TypeError(code: 0): Argument 1 passed to my_function() must be of the type array, float given, called in /var/www/html/public/error_handling/error_generators/fatal_error.php on line 3 at /var/www/html/public/error_handling/error_generators/fatal_error.php:2)"} []

After fix.

php-fpm_1  | [2022-05-11T17:36:50.263150+00:00] main.ERROR: Uncaught Exception TypeError: "Argument 1 passed to my_function() must be of the type array, float given, called in /var/www/html/public/error_handling/error_generators/fatal_error.php on line 3" at /var/www/html/public/error_handling/error_generators/fatal_error.php line 2 {"exception":"[object] (TypeError(code: 0): Argument 1 passed to my_function() must be of the type array, float given, called in /var/www/html/public/error_handling/error_generators/fatal_error.php on line 3 at /var/www/html/public/error_handling/error_generators/fatal_error.php:2)"} []

Conclusion: Matching, No degradation

@Seldaek
Copy link
Owner

Seldaek commented Jun 8, 2022

Patch looks good aside from the type I commented on, thanks! 👍🏻

@Seldaek Seldaek added this to the 2.x milestone Jun 8, 2022
@Seldaek Seldaek added the Bug label Jun 8, 2022
Correct handling of E_USER_ERROR as fatal error if registerErrorHandler is called with callPrevious, fixes Seldaek#1669

Co-authored-by: Jordi Boggiano <j.boggiano@seld.be>
@Seldaek Seldaek merged commit 64854f0 into Seldaek:2.x Jun 9, 2022
@Seldaek
Copy link
Owner

Seldaek commented Jun 9, 2022

Thanks

@lyrixx lyrixx mentioned this pull request Aug 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants