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 | |
| 21 | The following functions let you generate errors: |
| 22 | |
| 23 | show_error('message' [, int $status_code= 500 ] ) |
| 24 | =================================================== |
| 25 | |
| 26 | This function will display the error message supplied to it using the |
| 27 | following error template: |
| 28 | |
| 29 | application/errors/error_general.php |
| 30 | |
| 31 | The optional parameter $status_code determines what HTTP status code |
| 32 | should be sent with the error. |
| 33 | |
| 34 | show_404('page' [, 'log_error']) |
| 35 | ================================== |
| 36 | |
| 37 | This function will display the 404 error message supplied to it using |
| 38 | the following error template: |
| 39 | |
| 40 | application/errors/error_404.php |
| 41 | |
| 42 | The function expects the string passed to it to be the file path to the |
| 43 | page that isn't found. Note that CodeIgniter automatically shows 404 |
| 44 | messages if controllers are not found. |
| 45 | |
| 46 | CodeIgniter automatically logs any show_404() calls. Setting the |
| 47 | optional second parameter to FALSE will skip logging. |
| 48 | |
| 49 | log_message('level', 'message') |
| 50 | ================================ |
| 51 | |
| 52 | This function lets you write messages to your log files. You must supply |
| 53 | one of three "levels" in the first parameter, indicating what type of |
| 54 | message it is (debug, error, info), with the message itself in the |
| 55 | second parameter. Example:: |
| 56 | |
Derek Jones | 9713cce | 2011-10-05 17:26:43 -0500 | [diff] [blame] | 57 | if ($some_var == "") |
| 58 | { |
| 59 | log_message('error', 'Some variable did not contain a value.'); |
| 60 | } |
| 61 | else |
| 62 | { |
| 63 | log_message('debug', 'Some variable was correctly set'); |
| 64 | } |
| 65 | |
| 66 | 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] | 67 | |
| 68 | There are three message types: |
| 69 | |
| 70 | #. Error Messages. These are actual errors, such as PHP errors or user |
| 71 | errors. |
| 72 | #. Debug Messages. These are messages that assist in debugging. For |
| 73 | example, if a class has been initialized, you could log this as |
| 74 | debugging info. |
| 75 | #. Informational Messages. These are the lowest priority messages, |
| 76 | simply giving information regarding some process. CodeIgniter doesn't |
| 77 | natively generate any info messages but you may want to in your |
| 78 | application. |
| 79 | |
| 80 | .. note:: In order for the log file to actually be written, the "logs" |
| 81 | folder must be writable. In addition, you must set the "threshold" for |
| 82 | logging in application/config/config.php. You might, for example, only |
| 83 | want error messages to be logged, and not the other two types. If you |
| 84 | set it to zero logging will be disabled. |