Skip to content

Commit 32a6132

Browse files
committed
docs: add user guide
1 parent 71b6b74 commit 32a6132

File tree

4 files changed

+108
-0
lines changed

4 files changed

+108
-0
lines changed

user_guide_src/source/general/errors.rst

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,40 @@ This feature also works with user deprecations:
168168

169169
For testing your application you may want to always throw on deprecations. You may configure this by
170170
setting the environment variable ``CODEIGNITER_SCREAM_DEPRECATIONS`` to a truthy value.
171+
172+
.. _custom-exception-handlers:
173+
174+
Custom Exception Handlers
175+
=========================
176+
177+
.. versionadded:: 4.4.0
178+
179+
If you need more control over how exceptions are displayed you can now define your own handlers and
180+
specify when they apply.
181+
182+
Defining the New Handler
183+
------------------------
184+
185+
The first step is to create a new class which implements ``CodeIgniter\Debug\ExceptionHandlerInterface``.
186+
You can also extend ``CodeIgniter\Debug\BaseExceptionHandler``.
187+
This class includes a number of utility methods that are used by the default exception handler.
188+
The new handler must implement a single method: ``handle()``:
189+
190+
.. literalinclude:: errors/015.php
191+
192+
This example defines the minimum amount of code typically needed - display a view and exit with the proper
193+
exit code. However, the ``BaseExceptionHandler`` provides a number of other helper functions and objects.
194+
195+
Configuring the New Handler
196+
---------------------------
197+
198+
Telling CodeIgniter to use your new exception handler class is done in the **app/Config/Exceptions.php**
199+
configuration file's ``handler()`` method:
200+
201+
.. literalinclude:: errors/016.php
202+
203+
You can use any logic your application needs to determine whether it should handle the exception, but the
204+
two most common are checking on the HTTP status code or the type of exception. If your class should handle
205+
it then return a new instance of that class:
206+
207+
.. literalinclude:: errors/017.php
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Libraries;
4+
5+
use CodeIgniter\Debug\BaseExceptionHandler;
6+
use CodeIgniter\Debug\ExceptionHandlerInterface;
7+
use CodeIgniter\HTTP\RequestInterface;
8+
use CodeIgniter\HTTP\ResponseInterface;
9+
use Throwable;
10+
11+
class MyExceptionHandler extends BaseExceptionHandler implements ExceptionHandlerInterface
12+
{
13+
// You can override the view path.
14+
protected ?string $viewPath = APPPATH . 'Views/exception/';
15+
16+
public function handle(
17+
Throwable $exception,
18+
RequestInterface $request,
19+
ResponseInterface $response,
20+
int $statusCode,
21+
int $exitCode
22+
): void {
23+
$this->render($exception, $statusCode, $this->viewPath . "error_{$statusCode}.php");
24+
25+
exit($exitCode);
26+
}
27+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
use CodeIgniter\Debug\ExceptionHandler;
7+
use CodeIgniter\Debug\ExceptionHandlerInterface;
8+
use Throwable;
9+
10+
class Exceptions extends BaseConfig
11+
{
12+
// ...
13+
14+
public function handler(int $statusCode, Throwable $exception): ExceptionHandlerInterface
15+
{
16+
return new ExceptionHandler($this);
17+
}
18+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace Config;
4+
5+
use CodeIgniter\Config\BaseConfig;
6+
use CodeIgniter\Debug\ExceptionHandlerInterface;
7+
use CodeIgniter\Exceptions\PageNotFoundException;
8+
use Throwable;
9+
10+
class Exceptions extends BaseConfig
11+
{
12+
// ...
13+
14+
public function handler(int $statusCode, Throwable $exception): ExceptionHandlerInterface
15+
{
16+
if (in_array($statusCode, [400, 404, 500], true)) {
17+
return new \App\Libraries\MyExceptionHandler($this);
18+
}
19+
20+
if ($exception instanceof PageNotFoundException) {
21+
return new \App\Libraries\MyExceptionHandler($this);
22+
}
23+
24+
return new \CodeIgniter\Debug\ExceptionHandler($this);
25+
}
26+
}

0 commit comments

Comments
 (0)