Search code examples
phperror-handlingsuppress-warningserror-suppression

Proper way to handle errors suppressed by @?


As stated in the manual, calling error_reporting() will return 0 if the error came from an @ suppression. But since my production server is always set to error_reporting(0) (on top of the script), calling error_reporting() would always return to 0.

How can I actually catch or differentiate a legitimate error vs an error suppressed by @?

Right now my error handler is something like this:

if (error_reporting() == (E_ALL OR -1))
    echo 'display specific error';
elseif (error_reporting() == 0)
    echo 'display general error';

Note: This is related to my earlier question.


Solution

  • Mu. You should not set error_reporting to 0, even in production.

    In development, you'll want to set the error reporting level to the highest possible to be notified about any and all errors or notices or deprecation messages or possible issues that may pose problems in the future.

    In production, you'll want to set the error reporting level to only include errors and perhaps warnings, but not things like deprecation notices. This is to keep your error logs from superfluous information and be able to effectively track down production errors.

    Because you should always log errors.

    What you should not do in production is display errors on screen. That's what ini_set('display_errors', true|false) controls. You still want error reporting on production, just that that goes into a log file.