backup_cdi - SPLessons

CodeIgniter Error Handling

Home > Lesson > Chapter 10
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Error Handling

CodeIgniter Error Handling

shape Description

When using applications, the user may encounter some errors. Errors have to be handled to improve the performance of the application. The present chapter CodeIgniter Error Handling defines various errors occurring in application and the ways to handle those errors. Different levels are present in PHP to report about the errors. The error levels depends on the server configuration. The decision of showing the error to the user on screen totally depends on server setting called display_errors on which the developer access is not confirmed. So Codeigniter helps to show the errors on each page call using the function error_reporting(), which overrides the settings of display_errors. The function error_reporting() is present in main index.php file which has three main values called development, test, production used for reporting the errors. [php]define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');[/php] Development stage will display errors by default and testing and production stages solves the errors. Codeigniter error functions are globally available throughout the application which acts as an interface in a procedural manner. So, error messages are triggered without depending on function or class scope. CodeIgniter has the following three functions used in applications to report about the errors:

show_error()

shape Description

The template that sends function show_error() error is application/errors/error_general.php. Syntax
show_error('message',status_code);
In the above syntax, -> message parameter shows the error in the form of string and is compulsory. -> status_code represents the error in the form of integer which is the HTTP status code and is not mandatory. Eg: [php]show_error('File not found!');[/php]

show_404()

shape Description

The template that shows the message 404 error is application/errors/error_404.php. Syntax
show_404('page'[,'log_error']);
In the above syntax, -> page in the function show_404() represents that the path of the page was not found. Codeigniter shows shows_404 error automatically if controller file is missing. -> log_error is a boolean value i.e. true or false and is an optional parameter. When the function is set to value false, CodeIgniter does not show any show_404() calls and vice-versa if it is true. Eg:[php]show_404('welcome.php');[/php]

log_message()

shape Description

A log file is created each time the page runs which are controlled by the developer. log_message() writes the messages in log file. Syntax
log_message('level','message');
-> level represents error message type that is desired to log. Three levels of error message exists: debug, error and info. -> message parameter shows the error in the form of string. Eg: [php]log_message('error','username is empty');[/php] Message types in detail are: Log_threshold configuration has the entire control to allow an error to log. The logging threshold is present as $config[‘log_threshold’] in application/config/config.php. To log to any error message, set the value of $config['log_threshold'] to 1 according to the commented information given in the config.php file about it. By default it is set to 0 (zero) as shown below: [php]$config['log_threshold'] = 0;[/php] Error logging is turned off if the value is set to zero.

Summary

shape Key Points

CodeIgniter Error Handling chapter draws out following main points:
  • Codeigniter shows the errors on each page call using the function error_reporting().
  • show_error(), show_404() and log_message() are three functions used in applications to report about the errors.