3

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);

2 Answers 2

4

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).

Sign up to request clarification or add additional context in comments.

1 Comment

return false; did the trick. I tried already with returning false but it didn't work but I probably had a wrong if condition or something like this. It works now with returning false to pass the error to the default PHP error handler.
-1

There isn't an easy solution to do this, if you really want the error handler of php.

If you call set_error_handler, you get the current error handler as return value, but only, if set_error_handler was called already. An possible solution to avoid this is to restore the error handler(restore_error_handler) trigger your own error(trigger_error) and set your own error handler again. Caveat from this is, you lost the information about error line, file and context. In addition, you can only trigger user errors, which means, you have to map each error type to an user error type.

I would suggest, to handle all errors in your custom handler - there's no real benefit in such workarounds.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.