How To Handle Errors in Codeigniter?

CodeIgniter lets you build error reporting into your applications using the functions such as..
show_error('message' [, int $status_code= 500 ] )
This function will display the error message supplied to it using the following error template: application/errors/error_general.php

show_404('page' [, 'log_error'])
This function will display the 404 error message supplied to it using the following error template: application/errors/error_404.php

log_message('level', 'message')
This function lets you write messages to your log files. You can three level options:  error, debug and info.
By default, on a clean install, CI will display ALL php errors of all severity. So if you make a mistake in your PHP coding, you'll see the errors in your browser..


CI uses PHP's error_reporting() function to define the level of error reporting, and you can find this in the main index.php file in the root of your CI install. It’s the first function you see.


Instead of E_ALL, you can change it to any of the predefined error constant that PHP understands to suit your needs. Obviously, once your site goes live, you should change from E_ALL to E_ERROR to show only messages for fatal run time problems. This will hide all php errors except those that will halt your script execution.

How to hide database error?
You can hide the database error by setting db_debug to FALSE in application/config/database.php.
It will display blank page if any database error will occur instead of displaying any kind of database table name or error on site.
  $db['default']['db_debug'] = FALSE; 
You can easily redirect to some other error page instead of showing blank page as follows.

$query = $this->db->get('your_table');  
if(!$query)
{
   show_error("This is where your error message will appear surrounded by this default template. ", 500 );
   exit;
}

When you call show_error(), you will generate page that looks like this. The layout can changed by modifying this file application/errors/error_general.php)


That's it. Feel free to comment.
Source: http://www.askaboutphp.com/172/codeigniter-handling-errors.html

Comments

Popular Posts