Error handling in CodeIgniter
0 2336
- To prevent the application from errors, CodeIgniter provides an easy and speedy error handling mechanism.
- These errors make the application irritating and complex for users.
- By default, all PHP errors are displayed by Codeigniter. We can prevent these errors to display by using the error_reporting() PHP function at the top of the main index file. By disabling this function, we can prevent error log files from being written.
- In the Codeigniter framework, there are several error functions. These functions are basically simple procedural interfaces that are available throughout the application.
Related Topics:
Codeigniter Interview Questions
CodeIgniter Libraries
CodeIgniter Controllers
CodeIgniter - Error Handling
Codeigniter provides the following three methods to handles errors:
1 show_error(): This function is same as error_reporting function. It displays all errors at the top of the device screen in the HTML format. The return type of this function is mixed.
Syntax:
show_error($message, $status_code, $heading = 'An Error Was Encountered');
Explanation:
- $message: This parameter represents error messages. It has the type mixed.
- $status_code: This parameter represents HTTP response status code. The type of this parameter is int.
- $heading: This parameter represents the error heading. It can be a string.
Error templates used by this function are:
application/views/errors/html/error_general.php
or
application/views/errors/cli/error_general.php
2 show_404(): This function used to display the error 404 messages means it displays an error when the user trying to access a page that does not exists throughout the application. The return type of this function is void.
Syntax:
show_404($page = '', $log_error = TRUE)
Parameter Explanation:
- $page: This parameter is used to represents URL string. The type of this parameter is a string.
- $log_error: This parameter is used to determine whether to log the error. It is a Boolean type parameter.
Error templates used by this function are:
application/views/errors/html/error_404.php
Or
application/views/errors/cli/error_404.php
3 log_message(): This function is used to write the error messages into log files. The return type of this method is void.
Syntax:
log_message($level, $message, $php_error = FALSE)
Parameter Description:
- $level: This parameter represents log level. It has three possible values: 1. error 2. log 3. Info. It is a string-type parameter.
- $message: This parameter represents the message to LOG. It is a string-type parameter.
- $php_error: This parameter is used to determine whether we are logging a native PHP message or not. It is a Boolean type parameter.
Share:
Comments
Waiting for your comments