blob: a1cc3517a3a96a4ecf3fb1077f62dfa150805392 [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
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020032.. php: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
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +020039 This function will display the error message supplied to it using
40 the error template appropriate to your execution::
Derek Jones8ede1a22011-10-05 13:34:52 -050041
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +020042 application/views/errors/html/error_general.php
Derek Jones8ede1a22011-10-05 13:34:52 -050043
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +020044 or:
45
46 application/views/errors/cli/error_general.php
47
48 The optional parameter ``$status_code`` determines what HTTP status
49 code should be sent with the error. If ``$status_code`` is less
50 than 100, the HTTP status code will be set to 500, and the exit
51 status code will be set to ``$status_code + EXIT__AUTO_MIN``.
52 If that value is larger than ``EXIT__AUTO_MAX``, or if
53 ``$status_code`` is 100 or higher, the exit status code will be set
54 to ``EXIT_ERROR``.
55 You can check in *application/config/constants.php* for more detail.
Derek Jones8ede1a22011-10-05 13:34:52 -050056
Andrey Andreevcd3d9db2015-02-02 13:41:01 +020057.. php:function:: show_404($page = '', $log_error = TRUE)
Andrey Andreev16a704c2012-11-09 17:25:00 +020058
59 :param string $page: URI string
60 :param bool $log_error: Whether to log the error
Andrey Andreev9228f852014-02-07 23:40:22 +020061 :rtype: void
Derek Jones8ede1a22011-10-05 13:34:52 -050062
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +020063 This function will display the 404 error message supplied to it
64 using the error template appropriate to your execution::
Derek Jones8ede1a22011-10-05 13:34:52 -050065
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +020066 application/views/errors/html/error_404.php
Derek Jones8ede1a22011-10-05 13:34:52 -050067
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +020068 or:
Derek Jones8ede1a22011-10-05 13:34:52 -050069
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +020070 application/views/errors/cli/error_404.php
71
72 The function expects the string passed to it to be the file path to
73 the page that isn't found. The exit status code will be set to
74 ``EXIT_UNKNOWN_FILE``.
75 Note that CodeIgniter automatically shows 404 messages if
76 controllers are not found.
77
78 CodeIgniter automatically logs any ``show_404()`` calls. Setting the
79 optional second parameter to FALSE will skip logging.
Derek Jones8ede1a22011-10-05 13:34:52 -050080
Andrey Andreev34316362016-06-20 13:32:45 +030081.. php:function:: log_message($level, $message)
Andrey Andreev16a704c2012-11-09 17:25:00 +020082
vlakoffd0c30ab2013-05-07 07:49:23 +020083 :param string $level: Log level: 'error', 'debug' or 'info'
Andrey Andreev16a704c2012-11-09 17:25:00 +020084 :param string $message: Message to log
Andrey Andreev9228f852014-02-07 23:40:22 +020085 :rtype: void
Derek Jones8ede1a22011-10-05 13:34:52 -050086
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +020087 This function lets you write messages to your log files. You must
88 supply one of three "levels" in the first parameter, indicating what
89 type of message it is (debug, error, info), with the message itself
90 in the second parameter.
Derek Jones8ede1a22011-10-05 13:34:52 -050091
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +020092 Example::
Andrey Andreev16a704c2012-11-09 17:25:00 +020093
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +020094 if ($some_var == '')
95 {
96 log_message('error', 'Some variable did not contain a value.');
97 }
98 else
99 {
100 log_message('debug', 'Some variable was correctly set');
101 }
Derek Jones9713cce2011-10-05 17:26:43 -0500102
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +0200103 log_message('info', 'The purpose of some variable is to provide some value.');
Derek Jones8ede1a22011-10-05 13:34:52 -0500104
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +0200105 There are three message types:
Derek Jones8ede1a22011-10-05 13:34:52 -0500106
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +0200107 #. Error Messages. These are actual errors, such as PHP errors or
108 user 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.
Derek Jones8ede1a22011-10-05 13:34:52 -0500114
Andrey Andreev7fd0c2d2015-02-02 11:52:35 +0200115 .. note:: In order for the log file to actually be written, the
116 *logs/* directory must be writable. In addition, you must
117 set the "threshold" for logging in
118 *application/config/config.php*. You might, for example,
119 only want error messages to be logged, and not the other
120 two types. If you set it to zero logging will be disabled.