Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 1 | ############## |
| 2 | Error Handling |
| 3 | ############## |
| 4 | |
| 5 | CodeIgniter lets you build error reporting into your applications using |
| 6 | the functions described below. In addition, it has an error logging |
| 7 | class that permits error and debugging messages to be saved as text |
| 8 | files. |
| 9 | |
| 10 | .. note:: By default, CodeIgniter displays all PHP errors. You might |
| 11 | wish to change this behavior once your development is complete. You'll |
| 12 | find the error_reporting() function located at the top of your main |
| 13 | index.php file. Disabling error reporting will NOT prevent log files |
| 14 | from being written if there are errors. |
| 15 | |
| 16 | Unlike most systems in CodeIgniter, the error functions are simple |
| 17 | procedural interfaces that are available globally throughout the |
| 18 | application. This approach permits error messages to get triggered |
| 19 | without having to worry about class/function scoping. |
| 20 | |
Daniel Hunsaker | 3b5b7f4 | 2013-02-22 19:17:56 -0700 | [diff] [blame] | 21 | CodeIgniter also returns a status code whenever a portion of the core |
| 22 | calls ``exit()``. This exit status code is separate from the HTTP status |
| 23 | code, and serves as a notice to other processes that may be watching of |
| 24 | whether the script completed successfully, or if not, what kind of |
| 25 | problem it encountered that caused it to abort. These values are |
| 26 | defined in *application/config/constants.php*. While exit status codes |
| 27 | are most useful in CLI settings, returning the proper code helps server |
| 28 | software keep track of your scripts and the health of your application. |
| 29 | |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 30 | The following functions let you generate errors: |
| 31 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 32 | show_error() |
| 33 | ============ |
| 34 | |
| 35 | .. php:function:: show_error($message, $status_code, $heading = 'An Error Was Encountered') |
| 36 | |
| 37 | :param mixed $message: Error message |
| 38 | :param int $status_code: HTTP Response status code |
| 39 | :param string $heading: Error page heading |
| 40 | :returns: void |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 41 | |
| 42 | This function will display the error message supplied to it using the |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 43 | following error template:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 44 | |
vlakoff | 52301c7 | 2013-03-29 14:23:34 +0100 | [diff] [blame] | 45 | application/views/errors/error_general.php |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 46 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 47 | The optional parameter ``$status_code`` determines what HTTP status |
Daniel Hunsaker | 3b5b7f4 | 2013-02-22 19:17:56 -0700 | [diff] [blame] | 48 | code should be sent with the error. If ``$status_code`` is less than 100, |
| 49 | the HTTP status code will be set to 500, and the exit status code will |
| 50 | be set to ``$status_code + EXIT__AUTO_MIN``. If that value is larger than |
| 51 | ``EXIT__AUTO_MAX``, or if ``$status_code`` is 100 or higher, the exit |
Daniel Hunsaker | 50dfe01 | 2013-03-04 02:05:20 -0700 | [diff] [blame] | 52 | status code will be set to ``EXIT_ERROR``. You can check in |
Daniel Hunsaker | 3b5b7f4 | 2013-02-22 19:17:56 -0700 | [diff] [blame] | 53 | *application/config/constants.php* for more detail. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 54 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 55 | show_404() |
| 56 | ========== |
| 57 | |
| 58 | .. php:function:: show_404($page = '', $log_error = TRUE) |
| 59 | |
| 60 | :param string $page: URI string |
| 61 | :param bool $log_error: Whether to log the error |
| 62 | :returns: void |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 63 | |
| 64 | This function will display the 404 error message supplied to it using |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 65 | the following error template:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 66 | |
vlakoff | 52301c7 | 2013-03-29 14:23:34 +0100 | [diff] [blame] | 67 | application/views/errors/error_404.php |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 68 | |
| 69 | The function expects the string passed to it to be the file path to the |
Daniel Hunsaker | 50dfe01 | 2013-03-04 02:05:20 -0700 | [diff] [blame] | 70 | page that isn't found. The exit status code will be set to ``EXIT_UNKNOWN_FILE``. |
Daniel Hunsaker | 3b5b7f4 | 2013-02-22 19:17:56 -0700 | [diff] [blame] | 71 | Note that CodeIgniter automatically shows 404 messages if controllers are |
| 72 | not found. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 73 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 74 | CodeIgniter automatically logs any ``show_404()`` calls. Setting the |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 75 | optional second parameter to FALSE will skip logging. |
| 76 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 77 | log_message() |
| 78 | ============= |
| 79 | |
vlakoff | d0c30ab | 2013-05-07 07:49:23 +0200 | [diff] [blame] | 80 | .. php:function:: log_message($level, $message, $php_error = FALSE) |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 81 | |
vlakoff | d0c30ab | 2013-05-07 07:49:23 +0200 | [diff] [blame] | 82 | :param string $level: Log level: 'error', 'debug' or 'info' |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 83 | :param string $message: Message to log |
vlakoff | d0c30ab | 2013-05-07 07:49:23 +0200 | [diff] [blame] | 84 | :param bool $php_error: Whether we're logging a native PHP error message |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 85 | :returns: void |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | |
| 87 | This function lets you write messages to your log files. You must supply |
| 88 | one of three "levels" in the first parameter, indicating what type of |
| 89 | message it is (debug, error, info), with the message itself in the |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 90 | second parameter. |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 91 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 92 | Example:: |
| 93 | |
| 94 | if ($some_var == '') |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 95 | { |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 96 | log_message('error', 'Some variable did not contain a value.'); |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 97 | } |
| 98 | else |
| 99 | { |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 100 | log_message('debug', 'Some variable was correctly set'); |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | log_message('info', 'The purpose of some variable is to provide some value.'); |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 104 | |
| 105 | There are three message types: |
| 106 | |
| 107 | #. Error Messages. These are actual errors, such as PHP errors or user |
| 108 | errors. |
| 109 | #. Debug Messages. These are messages that assist in debugging. For |
| 110 | example, if a class has been initialized, you could log this as |
| 111 | debugging info. |
| 112 | #. Informational Messages. These are the lowest priority messages, |
| 113 | simply giving information regarding some process. CodeIgniter doesn't |
| 114 | natively generate any info messages but you may want to in your |
| 115 | application. |
| 116 | |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 117 | .. note:: In order for the log file to actually be written, the *logs* |
| 118 | directory must be writable. In addition, you must set the "threshold" |
| 119 | for logging in *application/config/config.php*. You might, for example, |
| 120 | only want error messages to be logged, and not the other two types. |
| 121 | If you set it to zero logging will be disabled. |