Jump to content

Recommended Posts

Interested in feedback regarding turning notices and warnings to exceptions with set_error_handler. To me, if you have warnings or notices the code is broke and needs to be fixed before it goes to production. If the app fatals you know it and HAVE to fix it. Pros? Cons?

 

Manual Reference
http://php.net/class.errorexception

Link to comment
https://forums.phpfreaks.com/topic/303134-convert-noticeswarnings-to-exceptions/
Share on other sites

Notices and warnings are not fatal, but if you turn them into exceptions then they won't be.

 

Go ahead and do that if you want in a development environment in order to find potential bugs, but don't do it in production - you don't want some random fluke somewhere to take down your site.

This is not a good idea, because warning and notices can happen even in perfectly written code. For example, it's sometimes valid or even necessary to perform actions that may fail: The password_hash() function checks different randomness sources like /dev/urandom, and that alone can trigger warnings (along the lines of: “Access to path restricted.”). Stopping the script on those warnings will render it unusable.

 

It's even worse if you use libraries or other third-party software, because a lot of programmers use notices and warnings for exactly this purpose: as notices and warnings. They don't assume you're turning everything into fatal errors.

 

Why do you need to stop the entire script? Just log all messages and check them regularly.

Ghehe I've been doing this for a long time now. Turning everything into an errorexception. Something along the lines of:

 

set_error_handler(function($errno, $errstr, ..) {
if ($errno === E_USER_DEPRECATED || $errno === E_DEPRECATED) {
$this->notifier->sendDeprecatedMail($this->getApplicationInfo(), $errstr, ['file' => $errfile, 'line' => $errline, 'context' => $context]);
return false;
}

if (!(error_reporting() & $errno)) {
return false;
}

throw new ErrorException($errno, $errstr, ..);
});

 

Didnt' knew about the password_hash() thing. Might need to revise :)

Edited by ignace
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.