CodeIgniter - Error Handling

CodeIgniter - Error Handling

CodeIgniter - Error Handling

Many times when using the application, we encounter errors. This is very annoying for users if errors are not handled properly. CodeIgniter provides a simple error handling mechanism.

You would like the messages to be displayed when the application is in development mode, not in production mode, because error messages can be easily resolved during development.

You can change your application's environment by changing the line below from the index.php file . This can be set to anything, but three values ​​are commonly used for this purpose (development, testing, production).

define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');

Different environments require different levels of error messages. By default, bugs are shown in development mode and hidden in test mode. CodeIgniter provides three functions as shown below for error handling.

  • The show_error() function displays errors in HTML format at the top of the screen.

The show_error() function displays errors in HTML format at the top of the screen.

Syntax

show_error( $message, $status_code, $heading = 'Error detected' )

parameters

  • $message ( mixed ) — Error message
  • $ status_code ( int ) - HTTP response status code
  • $heading ( string ) — Page title error

return type

mixed

Syntax

parameters

$message ( mixed ) — Error message

$ status_code ( int ) - HTTP response status code

$heading ( string ) — Page title error

return type

  • The show_404() function displays an error if you are trying to access a page that does not exist.

The show_404() function displays an error if you are trying to access a page that does not exist.

Syntax

show_404( $page=", $log_error=TRUE )

parameters

  • $page ( string ) — URI string
  • $log_error ( bool ) - whether to log an error

return type

invalid

Syntax

parameters

$page ( string ) — URI string

$log_error ( bool ) - whether to log an error

return type

  • The log_message() function is used to write log messages. This is useful when you want to write custom messages.

The log_message() function is used to write log messages. This is useful when you want to write custom messages.

Syntax

log_message( $level, $message, $php_error = FALSE )

parameters

  • $level ( string ) - Log level: "error", "debug", or "information"
  • $message ( string ) — Login message
  • $php_error ( bool ) - Are we logging our own PHP error message

return type

invalid

Syntax

parameters

$level ( string ) - Log level: "error", "debug", or "information"

$message ( string ) — Login message

$php_error ( bool ) - Are we logging our own PHP error message

return type

Logging can be enabled in the application/config/config.php file . Below is a screenshot of the config.php file where you can set the threshold value.

/*

|------------------------------------------------- -------------------------------

| Error Logging Threshold

|------------------------------------------------- -------------------------------

| You can enable error logging by setting a threshold over zero. The

| threshold determines what gets logged. Threshold options are:

|

| 0 = Disable logging, Error logging TURNED OFF

| 1 = Error Message (including PHP errors)

| 2 = Debug message

| 3 = Informational Messages

| 4 = All messages

|

| You can also pass an array with threshold levels to show individual error types

|

| array(2) = Debug Message, without Error Messages

| For a live site you'll usually only enable Errors (1) to be logged otherwise

| your log files will fill up very fast.

|

*/

$config['log_threshold'] = 0;

You can find the log messages in the /log/ app . Make sure this directory is writable before including the log files.

Various error message templates can be found in application/views/errors/cli or application/views/errors/html .