php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Unexpected stop of the script after an exception breaking the expected workflow

Open josepsanzcamp opened this issue 4 years ago • 4 comments

Description

Hi, I don't know if this can be considered an error or a feature, depending of the view point, but I have detected that some of my code have a different workflow from php >= 8.0.

The following code:

<?php

// phpcs:disable PSR1.Files.SideEffects

function __error_handler($type, $message, $file, $line)
{
    print_r(array(
        "function" => __FUNCTION__,
        "phperror" => "${message} (code ${type})",
        "details" => "Error on file " . basename($file) . ":" . $line,
        //~ "backtrace" => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
    ));
}

function __exception_handler($e)
{
    print_r(array(
        "function" => __FUNCTION__,
        "exception" => $e->getMessage() . " (code " . $e->getCode() . ")",
        "details" => "Error on file " . basename($e->getFile()) . ":" . $e->getLine(),
        //~ "backtrace" => $e->getTrace()
    ));
}

function __shutdown_handler()
{
    $error = error_get_last();
    $types = array(E_ERROR,E_PARSE,E_CORE_ERROR,E_COMPILE_ERROR,E_USER_ERROR,E_RECOVERABLE_ERROR);
    if (is_array($error) && isset($error["type"]) && in_array($error["type"], $types)) {
        print_r(array(
            "function" => __FUNCTION__,
            "phperror" => "${error["message"]}",
            "details" => "Error on file " . basename($error["file"]) . ":" . $error["line"],
            //~ "backtrace" => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
        ));
    } else {
        print_r(array(
            "function" => __FUNCTION__,
            //~ "backtrace" => debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS)
        ));
    }
}

error_reporting(E_ALL);
set_error_handler("__error_handler");
set_exception_handler("__exception_handler");
register_shutdown_function("__shutdown_handler");

$a = array(1,2,3);
$b = array(1,2);
$c = array_combine($a, $b);

echo "OK OK OK!!!\n";
die();

Resulted in this output in php >= 8.0:

Array
(
    [function] => __exception_handler
    [exception] => array_combine(): Argument #1 ($keys) and argument #2 ($values) must have the same number of elements (code 0)
    [details] => Error on file error.php:51
)
Array
(
    [function] => __shutdown_handler
)

But I expected this output instead as in php <= 7.4:

Array
(
    [function] => __error_handler
    [phperror] => array_combine(): Both parameters should have an equal number of elements (code 2)
    [details] => Error on file error.php:51
)
OK OK OK!!!
Array
(
    [function] => __shutdown_handler
)

The main problem is that the code stops after the exception and the OK OK OK!!! not appear.

I understand that the correct is use the try catch to control this exception, but to do compatible with a lot of existing code, is there some method to allow the runtime machine to continue executing the code after the exception at the next point as in php7.4???

Note: I have used the 7.4.27 and 8.0.15 php versions to do the tests

Thanks in advance.

Josep.

PHP Version

PHP version 8.0.15

Operating System

Debian GNU/Linux 11.2 (bullseye)

josepsanzcamp avatar Feb 04 '22 10:02 josepsanzcamp

Hi @josepsanzcamp

From the documentation:

https://www.php.net/manual/en/function.set-exception-handler.php

Sets the default exception handler if an exception is not caught within a try/catch block. Execution will stop after the callback is called.

So this is indeed the correct behavior. What changed in PHP 8 is just that this was previously a warning:

https://3v4l.org/vgMvd

array_combine([1], [1, 2]);

PHP <8.0 Warning: array_combine(): Both parameters should have an equal number of elements PHP >=8.0 Uncaught ValueError: array_combine(): Argument #1 ($keys) and argument #2 ($values) must have the same number of elements

Errors are different than exceptions in that they don't stop execution after an error has been triggered and caught by an error handler.

As for the feature request, not stopping after the exception would be an incredibly dangerous change. So unfortunately there's no simple solution other than to fix your code. Sorry.

iluuu1994 avatar Feb 04 '22 11:02 iluuu1994

Thanks for your clarification.

I appreciate a lot your quick response.

Josep.

josepsanzcamp avatar Feb 04 '22 11:02 josepsanzcamp

Hi @iluuu1994

How can I know what error interface use each function??? I'm referring to the error or exception that I can process using the error_handler or exception_handler.

For example, an error occurred in the array_combine throws an exception, but an error ocurred in the fopen throws an error, exists some mechanism to unify the errors or know for each function what kind of error and what error channel can use???

Thanks in advance.

Josep.

josepsanzcamp avatar Feb 04 '22 11:02 josepsanzcamp

Unfortunately, there's no way to know other than looking at the documentation. In general, you should shoot for neither of them being triggered instead of suppressing them. You might want to log warnings in production and fix them as they often get converted to exceptions in a later PHP version.

iluuu1994 avatar Feb 04 '22 12:02 iluuu1994