Web Programming TutorialsError Handling in CodeIgniter

Error Handling in CodeIgniter

Today we are going to understand the error handling especially in CI in this Error Handling in CodeIgniter tutorial.

PHP has various levels of error reporting for different errors. What level of error we get when we run our script depends on our server configuration. The decision to show the error on screen or not, is controlled by the server setting called display_errors. The developer may or may not have access to the underlying server setting such as display_errors. So the underlying settings are unknown to us. These settings are made known to us by codeigniter by the use of error_reporting() function on every page call, which overrides the settings of display_errors. This error_reporting() is found in the main index.php file.

Error functions in codeigniter are simple procedural interfaces that are available globally throughout the application, hence the error messages are triggered without caring about the class/function scope.
CodeIgniter helps us to report errors into our applications by using the following functions:

  1. show_error():
    • This function displays the error message using the template in application/errors/error_general.php.
    • Syntax of show_error() function is:
    • show_error(‘message’,status_code);
    • Here, the parameter message is mandatory and is the string error message to be shown.
    • The parameter status_code is optional which is an integer value representing the HTTP status code to be sent with the error.
    • Example:

      show_error(‘File not found!’);
  2. show_404():
    • This function displays the 404 error message supplied to it by the template in application/errors/error_404.php.
    • Syntax of show_404() function is:
    • show_404(‘page’[,’log_error’]);
    • The first parameter page in show_404() function is the path of the page that is not found. In general we can see that 404 messages are shown by codeigniter automatically, if the controllers are not found.
    • The second parameter log_error is optional and has boolean value i.e. true or false. If it is set to value false, codeigniter will not log any show_404() calls and vice-versa if it is true.
    • Example:

      show_404(‘welcome.php’);
  3. log_message():
    • Everytime a page is run, a log file is generated. These log files are under developer’s control.
    • This function allows you to write messages in the log file.
    • Syntax of log_message() function is:
    • log_message(‘level’,’message’);
    • The first parameter level indicates which type of error message you want to log. There are 3 levels of error message debug, error and info.
    • The message is the second parameter.
    • Example:

      log_message(‘error’,’username is empty’);
    • The three message types in detail are:
    • Error messages: these are actual errors, such as PHP errors or user errors.
    • Debug messages: these are messages that help in debugging.
    • Information messages: these are messages that provide information about some process. These are low priority message. CodeIgniter doesn’t generate these messages on its own.
    • When an error occurs, it is made available for logging, if the logging threshold allows it. That means logging of errors is controlled by log_threshold configuration.
    • The logging threshold is available in application/config/config.php as $config[‘log_threshold’].
    • If you want to log your error messages then you can set your $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:
    • $config[‘log_threshold’]=0;
    • The value zero disables logging i.e. error logging is turned off.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Exclusive content

- Advertisement -

Latest article

21,501FansLike
4,106FollowersFollow
106,000SubscribersSubscribe

More article

- Advertisement -