Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php, is there a way to turn all notices/warnings into an exception?

Tags:

I already set my error handler:

set_error_handler (function($errno, $errstr, $errfile,  $errline, array $errcontext) {
  $s = date('Ymd_His');
  switch ($errno)
  {
    case E_USER_ERROR:
      $s.= '_E_';
      break;
    case E_USER_WARNING:
      $s.= '_W_';
      break;
    case E_USER_NOTICE:
      $s.= '_N_';
      break;
    default:
      $s.= '_U_';
      break;
    }
    file_put_contents (APP_PATH_CACHE.'/log'.$s.'_'.rand(1,99999).'.html', print_r(get_defined_vars(), true));
}, E_ALL);

but can it be turn into an exception? So that I could see the flow.

like image 473
John Smith Avatar asked Sep 28 '15 11:09

John Smith


People also ask

How do I turn errors into exceptions in PHP?

Converting errors into exception is done by calling set_error_handler() and throw new ErrorException() in there...

How do I turn off PHP warnings?

Remember, the PHP ini configuration has an error_reporting directive that will be set by this function during runtime. error_reporting(0); To remove all errors, warnings, parse messages, and notices, the parameter that should be passed to the error_reporting function is zero.

How does PHP handle notice error?

By default, PHP sends an error log to the server's logging system or a file, depending on how the error_log configuration is set in the php. ini file. By using the error_log() function you can send error logs to a specified file or a remote destination.

Does PHP Notice stop execution?

Yes, it is possible. This question speaks to the more general issue of how to handle errors in PHP.


1 Answers

Yes. this is exactly why the ErrorException was created. see http://php.net/manual/en/class.errorexception.php

function exception_error_handler($severity, $message, $file, $line) {
    if (!(error_reporting() & $severity)) {
        // This error code is not included in error_reporting
        return;
    }
    throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");
like image 157
hanshenrik Avatar answered Sep 28 '22 05:09

hanshenrik