开发者

How do I manage errors with strict reporting in PHP?

I use a custom error handler with complete error reporting in PHP for tracking errors. This works great for debugging and logs all my errors in a database to investigate later.

Anyway, this method now disables the usage of @ to ignore an error when one occurs. I now have the issue where I try to rename a directory on my system, because it may occasionally throw an error (if files are being accessed within it).

I would like to be able to catch this error in my code, to prevent executing the rest of the function, but I also do not want this error to appear in my error logging database (considering this error is开发者_C百科 'managed' within the code, there is no need to see that it failed).

Is there a simple solution to this? I try using try / catch but it still appears to throw the error.


You can convert all errors/warnings/notices to exceptions

function exceptions_error_handler($severity, $message, $filename, $lineno) {
  if (error_reporting() == 0) {
    return;
  }
  if (error_reporting() & $severity) {
    throw new ErrorException($message, 0, $severity, $filename, $lineno);
  }
}
set_error_handler('exceptions_error_handler');

I think it is better to handle exceptions, than php native errors.


@zerkms' solution would work fine, but my error handler is already completed so to extend this to give me the functionality, I have simply included:

if ( error_reporting() == 0 )
  return;

at the start of my handler. This way, if the @ is used on a function, the error is still thrown, but ignored at the start of the handler (hence, not logged into the database, etc) and I will still get the boolean result from a function, such as rename().

This also still enabled me to use a try/catch solution on code if need be.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜