blob: 8c941aadb187b6199f07ca35bcf046c768f8849d [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
21The following functions let you generate errors:
22
Andrey Andreev16a704c2012-11-09 17:25:00 +020023show_error()
24============
25
26.. php:function:: show_error($message, $status_code, $heading = 'An Error Was Encountered')
27
28 :param mixed $message: Error message
29 :param int $status_code: HTTP Response status code
30 :param string $heading: Error page heading
31 :returns: void
Derek Jones8ede1a22011-10-05 13:34:52 -050032
33This function will display the error message supplied to it using the
Andrey Andreev16a704c2012-11-09 17:25:00 +020034following error template::
Derek Jones8ede1a22011-10-05 13:34:52 -050035
Andrey Andreev16a704c2012-11-09 17:25:00 +020036 application/errors/error_general.php
Derek Jones8ede1a22011-10-05 13:34:52 -050037
Andrey Andreev16a704c2012-11-09 17:25:00 +020038The optional parameter ``$status_code`` determines what HTTP status
39code should be sent with the error.
Derek Jones8ede1a22011-10-05 13:34:52 -050040
Andrey Andreev16a704c2012-11-09 17:25:00 +020041show_404()
42==========
43
44.. php:function:: show_404($page = '', $log_error = TRUE)
45
46 :param string $page: URI string
47 :param bool $log_error: Whether to log the error
48 :returns: void
Derek Jones8ede1a22011-10-05 13:34:52 -050049
50This function will display the 404 error message supplied to it using
Andrey Andreev16a704c2012-11-09 17:25:00 +020051the following error template::
Derek Jones8ede1a22011-10-05 13:34:52 -050052
Andrey Andreev16a704c2012-11-09 17:25:00 +020053 application/errors/error_404.php
Derek Jones8ede1a22011-10-05 13:34:52 -050054
55The function expects the string passed to it to be the file path to the
56page that isn't found. Note that CodeIgniter automatically shows 404
57messages if controllers are not found.
58
Andrey Andreev16a704c2012-11-09 17:25:00 +020059CodeIgniter automatically logs any ``show_404()`` calls. Setting the
Derek Jones8ede1a22011-10-05 13:34:52 -050060optional second parameter to FALSE will skip logging.
61
Andrey Andreev16a704c2012-11-09 17:25:00 +020062log_message()
63=============
64
65.. php:function:: log_message($level = 'error', $message, $php_error = FALSE)
66
67 :param string $level: Log level
68 :param string $message: Message to log
69 :param bool $php_error: Whether we're loggin a native PHP error message
70 :returns: void
Derek Jones8ede1a22011-10-05 13:34:52 -050071
72This function lets you write messages to your log files. You must supply
73one of three "levels" in the first parameter, indicating what type of
74message it is (debug, error, info), with the message itself in the
Andrey Andreev16a704c2012-11-09 17:25:00 +020075second parameter.
Derek Jones8ede1a22011-10-05 13:34:52 -050076
Andrey Andreev16a704c2012-11-09 17:25:00 +020077Example::
78
79 if ($some_var == '')
Derek Jones9713cce2011-10-05 17:26:43 -050080 {
Andrey Andreev16a704c2012-11-09 17:25:00 +020081 log_message('error', 'Some variable did not contain a value.');
Derek Jones9713cce2011-10-05 17:26:43 -050082 }
83 else
84 {
Andrey Andreev16a704c2012-11-09 17:25:00 +020085 log_message('debug', 'Some variable was correctly set');
Derek Jones9713cce2011-10-05 17:26:43 -050086 }
87
88 log_message('info', 'The purpose of some variable is to provide some value.');
Derek Jones8ede1a22011-10-05 13:34:52 -050089
90There are three message types:
91
92#. Error Messages. These are actual errors, such as PHP errors or user
93 errors.
94#. Debug Messages. These are messages that assist in debugging. For
95 example, if a class has been initialized, you could log this as
96 debugging info.
97#. Informational Messages. These are the lowest priority messages,
98 simply giving information regarding some process. CodeIgniter doesn't
99 natively generate any info messages but you may want to in your
100 application.
101
Andrey Andreev16a704c2012-11-09 17:25:00 +0200102.. note:: In order for the log file to actually be written, the *logs*
103 directory must be writable. In addition, you must set the "threshold"
104 for logging in *application/config/config.php*. You might, for example,
105 only want error messages to be logged, and not the other two types.
106 If you set it to zero logging will be disabled.