benanamen Posted February 8, 2017 Share Posted February 8, 2017 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 Referencehttp://php.net/class.errorexception Quote Link to comment https://forums.phpfreaks.com/topic/303134-convert-noticeswarnings-to-exceptions/ Share on other sites More sharing options...
requinix Posted February 8, 2017 Share Posted February 8, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303134-convert-noticeswarnings-to-exceptions/#findComment-1542453 Share on other sites More sharing options...
Jacques1 Posted February 8, 2017 Share Posted February 8, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303134-convert-noticeswarnings-to-exceptions/#findComment-1542454 Share on other sites More sharing options...
ignace Posted February 8, 2017 Share Posted February 8, 2017 (edited) 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 February 8, 2017 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/303134-convert-noticeswarnings-to-exceptions/#findComment-1542457 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.