Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass php error to default php error handler in my custom error handler

I can't manage the following: I have an error handler which catches all E_WARNINGS but I only want to handle some of the warnings all the other I want to ignore and pass them to the default PHP error handler (which also takes all the other error types except the E_WARNINGS).

Is this even possible? Look at my simple error handler:

set_error_handler(function($errno, $errstr, $errfile, $errline, $errcontext) {
    if($errfile != 'somefile_for_example.php') {
        // I don't care about this error throw it somewhere else!
    }

    echo 'error in this file handles my custom error handler';
    return true;
}, E_WARNING);
like image 249
TiMESPLiNTER Avatar asked Jul 12 '26 10:07

TiMESPLiNTER


1 Answers

PHP documentation says at: http://php.net/manual/en/function.set-error-handler.php

It is important to remember that the standard PHP error handler is completely bypassed for the error types specified by error_types unless the callback function returns FALSE.

Maybe then this should work:

$old_error_handler = set_error_handler(
    function($errno, $errstr, $errfile, $errline, $errcontext) {

        if($errfile != 'somefile_for_example.php') {
            return false;
        }

        echo 'error in this file handles my custom error handler';
        return true;
    }, E_WARNING);

-

[Edit] Another user (Philipp) commented that set_error_handler returns old handler, but only for another custom one, not for the default handler.

-

In any case, when programming error handlers, one must always be extra careful with programming errors, as they cannot be handled (maybe test the functions by themselves first).

like image 141
vicenteherrera Avatar answered Jul 14 '26 02:07

vicenteherrera



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!