You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: user_guide_src/source/general/errors.rst
+81-38Lines changed: 81 additions & 38 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,20 +19,29 @@ Using Exceptions
19
19
20
20
This section is a quick overview for newer programmers, or for developers who are not experienced with using exceptions.
21
21
22
+
What is Exceptions
23
+
------------------
24
+
22
25
Exceptions are simply events that happen when the exception is "thrown". This halts the current flow of the script, and
23
26
execution is then sent to the error handler which displays the appropriate error page:
24
27
25
28
.. literalinclude:: errors/001.php
26
29
30
+
Catching Exceptions
31
+
-------------------
32
+
27
33
If you are calling a method that might throw an exception, you can catch that exception using a ``try/catch`` block:
28
34
29
35
.. literalinclude:: errors/002.php
30
36
31
37
If the ``$userModel`` throws an exception, it is caught and the code within the catch block is executed. In this example,
32
38
the scripts dies, echoing the error message that the ``UserModel`` defined.
33
39
40
+
Catching Specific Exceptions
41
+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
42
+
34
43
In the example above, we catch any type of Exception. If we only want to watch for specific types of exceptions, like
35
-
a ``UnknownFileException``, we can specify that in the catch parameter. Any other exceptions that are thrown and are
44
+
a ``DataException``, we can specify that in the catch parameter. Any other exceptions that are thrown and are
36
45
not child classes of the caught exception will be passed on to the error handler:
37
46
38
47
.. literalinclude:: errors/003.php
@@ -65,7 +74,7 @@ See :ref:`setting-environment`.
65
74
Logging Exceptions
66
75
------------------
67
76
68
-
By default, all Exceptions other than 404 - Page Not Found exceptions are logged. This can be turned on and off
77
+
By default, all Exceptions other than "404 - Page Not Found" exceptions are logged. This can be turned on and off
69
78
by setting the ``$log`` value of **app/Config/Exceptions.php**:
70
79
71
80
.. literalinclude:: errors/005.php
@@ -74,8 +83,40 @@ To ignore logging on other status codes, you can set the status code to ignore i
74
83
75
84
.. literalinclude:: errors/006.php
76
85
77
-
.. note:: It is possible that logging still will not happen for exceptions if your current Log settings
78
-
are not set up to log **critical** errors, which all exceptions are logged as.
86
+
.. note:: It is possible that logging still will not happen for exceptions if your current
87
+
:ref:`Log settings <logging-configuration>`
88
+
are not set up to log ``critical`` errors, which all exceptions are logged as.
89
+
90
+
.. _logging_deprecation_warnings:
91
+
92
+
Logging Deprecation Warnings
93
+
----------------------------
94
+
95
+
.. versionadded:: 4.3.0
96
+
97
+
By default, all errors reported by ``error_reporting()`` will be thrown as an ``ErrorException`` object. These
98
+
include both ``E_DEPRECATED`` and ``E_USER_DEPRECATED`` errors. With the surge in use of PHP 8.1+, many users
99
+
may see exceptions thrown for `passing null to non-nullable arguments of internal functions <https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg>`_.
100
+
To ease the migration to PHP 8.1, you can instruct CodeIgniter to log the deprecations instead of throwing them.
101
+
102
+
First, make sure your copy of ``Config\Exceptions`` is updated with the two new properties and set as follows:
103
+
104
+
.. literalinclude:: errors/012.php
105
+
106
+
Next, depending on the log level you set in ``Config\Exceptions::$deprecationLogLevel``, check whether the
107
+
logger threshold defined in ``Config\Logger::$threshold`` covers the deprecation log level. If not, adjust
108
+
it accordingly.
109
+
110
+
.. literalinclude:: errors/013.php
111
+
112
+
After that, subsequent deprecations will be logged instead of thrown.
113
+
114
+
This feature also works with user deprecations:
115
+
116
+
.. literalinclude:: errors/014.php
117
+
118
+
For testing your application you may want to always throw on deprecations. You may configure this by
119
+
setting the environment variable ``CODEIGNITER_SCREAM_DEPRECATIONS`` to a truthy value.
79
120
80
121
Framework Exceptions
81
122
====================
@@ -85,15 +126,17 @@ The following framework exceptions are available:
85
126
PageNotFoundException
86
127
---------------------
87
128
88
-
This is used to signal a 404, Page Not Found error. When thrown, the system will show the view found at
89
-
**app/Views/errors/html/error_404.php**. You should customize all of the error views for your site.
90
-
If, in **app/Config/Routes.php**, you have specified a 404 Override, that will be called instead of the standard
91
-
404 page:
129
+
This is used to signal a 404, Page Not Found error:
92
130
93
131
.. literalinclude:: errors/007.php
94
132
95
133
You can pass a message into the exception that will be displayed in place of the default message on the 404 page.
96
134
135
+
For the default 404 view file location, see :ref:`http-status-code-and-error-views`.
136
+
137
+
If, in **app/Config/Routing.php** or **app/Config/Routes.php**, you have specified
138
+
a :ref:`404-override`, that will be called instead of the standard 404 page.
139
+
97
140
ConfigException
98
141
---------------
99
142
@@ -144,52 +187,52 @@ Specify HTTP Status Code in Your Exception
144
187
.. versionadded:: 4.3.0
145
188
146
189
Since v4.3.0, you can specify the HTTP status code for your Exception class to implement
When an exception implementing ``HTTPExceptionInterface`` is caught by CodeIgniter's exception handler, the Exception code will become the HTTP status code.
150
193
151
-
.. _error-specify-exit-code:
194
+
.. _http-status-code-and-error-views:
152
195
153
-
Specify Exit Code in Your Exception
154
-
===================================
196
+
HTTP Status Code and Error Views
197
+
================================
155
198
156
-
.. versionadded:: 4.3.0
199
+
The exception handler displays the error view corresponding to the HTTP status
200
+
code, if one exists.
157
201
158
-
Since v4.3.0, you can specify the exit code for your Exception class to implement
159
-
``HasExitCodeInterface``.
160
-
161
-
When an exception implementing ``HasExitCodeInterface`` is caught by CodeIgniter's exception handler, the code returned from the ``getExitCode()`` method will become the exit code.
202
+
For example, ``PageNotFoundException`` implements the ``HTTPExceptionInterface``,
203
+
so its exception code ``404`` will be the HTTP status code. Therefore if it is
204
+
thrown, the system will show the **error_404.php** in the **app/Views/errors/html**
205
+
folder when it is a web request. If it is invoked via CLI, the system will show
206
+
the **error_404.php** in the **app/Views/errors/cli** folder.
162
207
163
-
.. _logging_deprecation_warnings:
208
+
If there is no view file corresponding to the HTTP status code, **production.php**
209
+
or **error_exception.php** will be displayed.
164
210
165
-
Logging Deprecation Warnings
166
-
============================
167
-
168
-
.. versionadded:: 4.3.0
169
-
170
-
By default, all errors reported by ``error_reporting()`` will be thrown as an ``ErrorException`` object. These
171
-
include both ``E_DEPRECATED`` and ``E_USER_DEPRECATED`` errors. With the surge in use of PHP 8.1+, many users
172
-
may see exceptions thrown for `passing null to non-nullable arguments of internal functions <https://wiki.php.net/rfc/deprecate_null_to_scalar_internal_arg>`_.
173
-
To ease the migration to PHP 8.1, you can instruct CodeIgniter to log the deprecations instead of throwing them.
211
+
.. note:: If ``display_errors`` is on in the PHP INI configuration,
212
+
**error_exception.php** is selected and a detailed error report is displayed.
174
213
175
-
First, make sure your copy of ``Config\Exceptions`` is updated with the two new properties and set as follows:
214
+
You should customize all of the error views in the **app/Views/errors/html** folder
215
+
for your site.
176
216
177
-
.. literalinclude:: errors/012.php
217
+
You can also create error views for specific HTTP status code. For example, if
218
+
you want to create an error view for "400 Bad Request", add **error_400.php**.
178
219
179
-
Next, depending on the log level you set in ``Config\Exceptions::$deprecationLogLevel``, check whether the
180
-
logger threshold defined in ``Config\Logger::$threshold`` covers the deprecation log level. If not, adjust
181
-
it accordingly.
220
+
.. warning:: If an error view file with the corresponding HTTP status code exists,
221
+
the exception handler will display that file regardless of the environment.
222
+
The view file must be implemented in such a way that it does not display
223
+
detailed error messages in production environment by yourself.
182
224
183
-
.. literalinclude:: errors/013.php
225
+
.. _error-specify-exit-code:
184
226
185
-
After that, subsequent deprecations will be logged instead of thrown.
227
+
Specify Exit Code in Your Exception
228
+
===================================
186
229
187
-
This feature also works with user deprecations:
230
+
.. versionadded:: 4.3.0
188
231
189
-
.. literalinclude:: errors/014.php
232
+
Since v4.3.0, you can specify the exit code for your Exception class to implement
233
+
``CodeIgniter\Exceptions\HasExitCodeInterface``.
190
234
191
-
For testing your application you may want to always throw on deprecations. You may configure this by
192
-
setting the environment variable ``CODEIGNITER_SCREAM_DEPRECATIONS`` to a truthy value.
235
+
When an exception implementing ``HasExitCodeInterface`` is caught by CodeIgniter's exception handler, the code returned from the ``getExitCode()`` method will become the exit code.
0 commit comments