1

I am working with an application that we have added a custom error handler to. The reason we added this was so that we can be immediately notified and track issues in our application.

The problem here is that in debug mode, I have switched the error handler to print out all errors. The goal here being to eliminate all errors as they happen.

Now, I am trying to pull variables from a database that may or may not be serialized. So, the only way that I currently know of to detect if they are serialized, is to add a test. (IE if (unserialize($var)) { // do it }).

I can add the mute operator to the function call, but the custom error handler ignores it. Is there a way to detect whether the given error was muted? Or is there a better way to do this?

I am unable to modify database structure or data.

1 Answer 1

5

I can add the mute operator to the function call, but the custom error handler ignores it.

Actually, the "mute" operator will work just fine in this case. By checking the error reporting setting inside your custom error handler you can obey individual cases employing the @ suppression operator:

set_error_handler(function($errNo, $errStr, $errFile, $errLine) {
    if (0 !== error_reporting()) {
        $msg = "$errStr in $errFile on line $errLine";
        throw new ErrorException($msg, $errNo);
    }
});

If error_reporting() === 0 then the @ suppression operator was used. As long as you check that value before launching into the rest of your handling routine you'll be fine.

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

4 Comments

Without altering the answer I would add that it's best to avoid this situation altogether by cleaning up your data before it's stored in the database to avoid the additional overhead of starting up PHP's error handling process on the downstream end. Of course, that's not always possible ...
@hakre I just tested in PHP 5.4.3 and the $errNo parameter does not equal zero when the @ operator is used in this case: @print_r($array['nonexistent_index']); ...
Well, never trust one's older getting brainy ;) Thanks for testing and reporting back.
I tested on mine and I get an actual error code as well (Notice - 8) however your solution @rdlowrey does work.

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.