blob: 721c78ca7250561f627ad5b64b8d73b2c1c5b0e7 [file] [log] [blame]
Derek Jones8ede1a22011-10-05 13:34:52 -05001##############
2Error Handling
3##############
4
5CodeIgniter lets you build error reporting into your applications using
6the functions described below. In addition, it has an error logging
7class that permits error and debugging messages to be saved as text
8files.
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
16Unlike most systems in CodeIgniter, the error functions are simple
17procedural interfaces that are available globally throughout the
18application. This approach permits error messages to get triggered
19without having to worry about class/function scoping.
20
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -070021CodeIgniter also returns a status code whenever a portion of the core
22calls ``exit()``. This exit status code is separate from the HTTP status
23code, and serves as a notice to other processes that may be watching of
Derek Jonesb8c283a2013-07-19 16:02:53 -070024whether the script completed successfully, or if not, what kind of
25problem it encountered that caused it to abort. These values are
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -070026defined in *application/config/constants.php*. While exit status codes
27are most useful in CLI settings, returning the proper code helps server
28software keep track of your scripts and the health of your application.
29
Derek Jones8ede1a22011-10-05 13:34:52 -050030The following functions let you generate errors:
31
Derek Jonesb8c283a2013-07-19 16:02:53 -070032.. function:: show_error($message, $status_code, $heading = 'An Error Was Encountered')
Andrey Andreev16a704c2012-11-09 17:25:00 +020033
34 :param mixed $message: Error message
35 :param int $status_code: HTTP Response status code
36 :param string $heading: Error page heading
Andrey Andreev9228f852014-02-07 23:40:22 +020037 :rtype: void
Derek Jones8ede1a22011-10-05 13:34:52 -050038
39This function will display the error message supplied to it using the
Andrey Andreev16a704c2012-11-09 17:25:00 +020040following error template::
Derek Jones8ede1a22011-10-05 13:34:52 -050041
vlakoff52301c72013-03-29 14:23:34 +010042 application/views/errors/error_general.php
Derek Jones8ede1a22011-10-05 13:34:52 -050043
Andrey Andreev16a704c2012-11-09 17:25:00 +020044The optional parameter ``$status_code`` determines what HTTP status
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -070045code should be sent with the error. If ``$status_code`` is less than 100,
46the HTTP status code will be set to 500, and the exit status code will
47be set to ``$status_code + EXIT__AUTO_MIN``. If that value is larger than
48``EXIT__AUTO_MAX``, or if ``$status_code`` is 100 or higher, the exit
Derek Jonesb8c283a2013-07-19 16:02:53 -070049status code will be set to ``EXIT_ERROR``. You can check in
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -070050*application/config/constants.php* for more detail.
Derek Jones8ede1a22011-10-05 13:34:52 -050051
Derek Jonesb8c283a2013-07-19 16:02:53 -070052.. function:: show_404($page = '', $log_error = TRUE)
Andrey Andreev16a704c2012-11-09 17:25:00 +020053
54 :param string $page: URI string
55 :param bool $log_error: Whether to log the error
Andrey Andreev9228f852014-02-07 23:40:22 +020056 :rtype: void
Derek Jones8ede1a22011-10-05 13:34:52 -050057
58This function will display the 404 error message supplied to it using
Andrey Andreev16a704c2012-11-09 17:25:00 +020059the following error template::
Derek Jones8ede1a22011-10-05 13:34:52 -050060
vlakoff52301c72013-03-29 14:23:34 +010061 application/views/errors/error_404.php
Derek Jones8ede1a22011-10-05 13:34:52 -050062
63The function expects the string passed to it to be the file path to the
Daniel Hunsaker50dfe012013-03-04 02:05:20 -070064page that isn't found. The exit status code will be set to ``EXIT_UNKNOWN_FILE``.
Daniel Hunsaker3b5b7f42013-02-22 19:17:56 -070065Note that CodeIgniter automatically shows 404 messages if controllers are
66not found.
Derek Jones8ede1a22011-10-05 13:34:52 -050067
Andrey Andreev16a704c2012-11-09 17:25:00 +020068CodeIgniter automatically logs any ``show_404()`` calls. Setting the
Derek Jones8ede1a22011-10-05 13:34:52 -050069optional second parameter to FALSE will skip logging.
70
Derek Jonesb8c283a2013-07-19 16:02:53 -070071.. function:: log_message($level, $message, $php_error = FALSE)
Andrey Andreev16a704c2012-11-09 17:25:00 +020072
vlakoffd0c30ab2013-05-07 07:49:23 +020073 :param string $level: Log level: 'error', 'debug' or 'info'
Andrey Andreev16a704c2012-11-09 17:25:00 +020074 :param string $message: Message to log
vlakoffd0c30ab2013-05-07 07:49:23 +020075 :param bool $php_error: Whether we're logging a native PHP error message
Andrey Andreev9228f852014-02-07 23:40:22 +020076 :rtype: void
Derek Jones8ede1a22011-10-05 13:34:52 -050077
78This function lets you write messages to your log files. You must supply
79one of three "levels" in the first parameter, indicating what type of
80message it is (debug, error, info), with the message itself in the
Andrey Andreev16a704c2012-11-09 17:25:00 +020081second parameter.
Derek Jones8ede1a22011-10-05 13:34:52 -050082
Andrey Andreev16a704c2012-11-09 17:25:00 +020083Example::
84
85 if ($some_var == '')
Derek Jones9713cce2011-10-05 17:26:43 -050086 {
Andrey Andreev16a704c2012-11-09 17:25:00 +020087 log_message('error', 'Some variable did not contain a value.');
Derek Jones9713cce2011-10-05 17:26:43 -050088 }
89 else
90 {
Andrey Andreev16a704c2012-11-09 17:25:00 +020091 log_message('debug', 'Some variable was correctly set');
Derek Jones9713cce2011-10-05 17:26:43 -050092 }
93
94 log_message('info', 'The purpose of some variable is to provide some value.');
Derek Jones8ede1a22011-10-05 13:34:52 -050095
96There are three message types:
97
98#. Error Messages. These are actual errors, such as PHP errors or user
99 errors.
100#. Debug Messages. These are messages that assist in debugging. For
101 example, if a class has been initialized, you could log this as
102 debugging info.
103#. Informational Messages. These are the lowest priority messages,
Andrey Andreev90726b82015-01-20 12:39:22 +0200104 simply giving information regarding some process.
Derek Jones8ede1a22011-10-05 13:34:52 -0500105
Andrey Andreev16a704c2012-11-09 17:25:00 +0200106.. note:: In order for the log file to actually be written, the *logs*
107 directory must be writable. In addition, you must set the "threshold"
108 for logging in *application/config/config.php*. You might, for example,
109 only want error messages to be logged, and not the other two types.
110 If you set it to zero logging will be disabled.