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 |
Derek Jones | b8c283a | 2013-07-19 16:02:53 -0700 | [diff] [blame] | 24 | whether the script completed successfully, or if not, what kind of |
| 25 | problem it encountered that caused it to abort. These values are |
Daniel Hunsaker | 3b5b7f4 | 2013-02-22 19:17:56 -0700 | [diff] [blame] | 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 | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 32 | .. php:function:: show_error($message, $status_code, $heading = 'An Error Was Encountered') |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 33 | |
| 34 | :param mixed $message: Error message |
| 35 | :param int $status_code: HTTP Response status code |
| 36 | :param string $heading: Error page heading |
Andrey Andreev | 9228f85 | 2014-02-07 23:40:22 +0200 | [diff] [blame] | 37 | :rtype: void |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 38 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 39 | This function will display the error message supplied to it using |
| 40 | the error template appropriate to your execution:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 41 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 42 | application/views/errors/html/error_general.php |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 43 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 44 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 56 | |
Andrey Andreev | cd3d9db | 2015-02-02 13:41:01 +0200 | [diff] [blame] | 57 | .. php:function:: show_404($page = '', $log_error = TRUE) |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 58 | |
| 59 | :param string $page: URI string |
| 60 | :param bool $log_error: Whether to log the error |
Andrey Andreev | 9228f85 | 2014-02-07 23:40:22 +0200 | [diff] [blame] | 61 | :rtype: void |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 62 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 63 | This function will display the 404 error message supplied to it |
| 64 | using the error template appropriate to your execution:: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 65 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 66 | application/views/errors/html/error_404.php |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 67 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 68 | or: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 69 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 70 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 80 | |
Andrey Andreev | 3431636 | 2016-06-20 13:32:45 +0300 | [diff] [blame^] | 81 | .. php:function:: log_message($level, $message) |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 82 | |
vlakoff | d0c30ab | 2013-05-07 07:49:23 +0200 | [diff] [blame] | 83 | :param string $level: Log level: 'error', 'debug' or 'info' |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 84 | :param string $message: Message to log |
Andrey Andreev | 9228f85 | 2014-02-07 23:40:22 +0200 | [diff] [blame] | 85 | :rtype: void |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 86 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 87 | 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 91 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 92 | Example:: |
Andrey Andreev | 16a704c | 2012-11-09 17:25:00 +0200 | [diff] [blame] | 93 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 94 | 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 Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 102 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 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 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 105 | There are three message types: |
Derek Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 106 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 107 | #. 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 Jones | 8ede1a2 | 2011-10-05 13:34:52 -0500 | [diff] [blame] | 114 | |
Andrey Andreev | 7fd0c2d | 2015-02-02 11:52:35 +0200 | [diff] [blame] | 115 | .. 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. |