troy
troy

Reputation: 1027

error handling in codeigniter

In my project to catch all the PHP errors I have set up my error handling mechanism as follows:

  1. I have set error_reporting() in index.php file which overrides anything in the php.ini file
  2. An error handler is set in system/codeigniter/CodeIgniter.php using set_error_handler - this error handler, _exception_handler, is found in system/codeigniter/Common.php
  3. The _exception_handler function ignores E_STRICT errors, calls the show_php_error function From the Exceptions system library if the severity is that specified by your error_reporting() function in index.php and logs the error according to whatever you have set up in your config.php file
  4. The handler returns FALSE so after this PHP goes on to handle the error however it normally would according your error_reporting level and display_errors setting.

The thing that is puzzling me is that E_ERROR errors i.e. fatal errors don’t seem to be being caught by _exception_handler at all. It’s not just that show_php_error isn’t being called, it looks like the function just isn’t being called for them. This is obviously a problem as it means that they aren’t get handled by show_php_error or logged. For example if I deliberately mistype $this->load->views('foo'); in a controller, the handler doesn’t get called.

Any suggestion about error handling would be much appreciated, thanks!

Upvotes: 10

Views: 24333

Answers (3)

krunal panchal
krunal panchal

Reputation: 427

Simply you can handle all type of error in one file which is display your client because of php error or any other error is not good to display client.

Simply place file Common_Exceptions.php in core folder . Common is my because in config file I have declare $config['subclass_prefix'] = 'Common_';

Copy system\core\Exceptions.php file and paste in core\Common_Excepions file (class Common_Exceptions extends CI_Exceptions) and do your change in this file and call view for your want and display to client when error come.

NOTE: $config['log_threshold'] = 1; enable in config file for errorlog write and after you see what error come.

One more suggestion on view file which is display when error is come there place time so when you see in log then match this time and find which error is come that time

Upvotes: 0

tix3
tix3

Reputation: 1152

Now this is a rather big debate: Whether you should catch the fatal errors or not. Some say that they are FATAL so you dont know in which condition is the system but I will go with the "try to do the cleanup if the error occured". In order to catch ALL fatal errors you will need to setup a pre_system hook. go to application/config/hooks.php and enter

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

after that go to hooks directory and add your handling of the 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 are using the register_shutdown_function to run a function that checks if an error had occured and if it had send it via email to the developer. This setup is working flawlessly for over 2 years in several CI projects that I have been working with.

Upvotes: 6

Gergelin
Gergelin

Reputation: 9

I've found this answer under an other question (https://stackoverflow.com/a/3675357), and i think it is also useful for anyone reading this question. "For codeigniter specific error handling, you can override its 'Exception' library class by creating My_Exception class in your application/libraries folder. Copy original library function signatures into it and put in your code. It will surely work."

Upvotes: 0

Related Questions