Error handling in codeigniter

In my project, to catch all the PHP errors, I created an error handling mechanism as follows:

  • I set error_reporting() in an index.php file that overrides anything in the php.ini
  • The error handler is installed in system/codeigniter/CodeIgniter.php using set_error_handler - this error handler, _exception_handler , is found in system/codeigniter/Common.php
  • The _exception_handler function ignores _exception_handler errors, calls show_php_error from the system exception library if the severity is what your error_reporting() function indicates in index.php and logs the error according to what you set in your config.php file
  • The handler returns FALSE , so after that PHP continues to process however, as a rule, this will correspond to your level of error_reporting and display_errors .

What puzzles me is that the errors are E_ERROR , i.e. fatal errors do not fall into _exception_handler at _exception_handler . It's not only that show_php_error not being called, it looks like the function is just not being called for them. This is obviously a problem, as it means that they arent processed by show_php_error or are logged. For example, if I intentionally make a mistake $this->load->views('foo'); in the controller, the handler will not be called.

Any suggestion on error handling would be greatly appreciated, thanks!

+8
php codeigniter
source share
2 answers

Now this is a pretty big discussion: Should you catch fatal errors or not. Some say they are FATAL, so you donโ€™t know what state the system is in, but Iโ€™ll go with โ€œtry to clean up if an error occursโ€. To catch ALL fatal errors, you need to configure the pre_system binding. go to application / config / hooks.php and type

 $hook['pre_system'][] = array( 'class' => 'PHPFatalError', 'function' => 'setHandler', 'filename' => 'PHPFatalError.php', 'filepath' => 'hooks' ); 

after that go to the hooks directory and add the handling of this error:

 <?php class PHPFatalError { public function setHandler() { register_shutdown_function('handleShutdown'); } } function handleShutdown() { if (($error = error_get_last())) { ob_start(); echo "<pre>"; var_dump($error); echo "</pre>"; $message = ob_get_clean(); sendEmail($message); ob_start(); echo '{"status":"error","message":"Internal application error!"}'; ob_flush(); exit(); } } 

as you can see, we use the register_shutdown_function function to run a function that checks if an error has occurred and if it emailed it to the developer. This setup has been working flawlessly for over two years in several CI projects I have worked with.

+4
source share

I found this answer on a different question ( https://stackoverflow.com/a/3/3128/ ) and I think this is also useful for anyone reading this question. "To handle codeigniter-specific errors, you can override your Exception library class by creating the My_Exception class in the applications / libraries folder. Copy the original library library signatures into it and enter the code. Work."

0
source share

All Articles