Skip to content

Commit d2d65cd

Browse files
committed
feat: use ExceptionHandler
1 parent e753169 commit d2d65cd

File tree

1 file changed

+40
-6
lines changed

1 file changed

+40
-6
lines changed

system/Debug/Exceptions.php

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
use CodeIgniter\Exceptions\HasExitCodeInterface;
1616
use CodeIgniter\Exceptions\HTTPExceptionInterface;
1717
use CodeIgniter\Exceptions\PageNotFoundException;
18-
use CodeIgniter\HTTP\CLIRequest;
1918
use CodeIgniter\HTTP\Exceptions\HTTPException;
20-
use CodeIgniter\HTTP\IncomingRequest;
19+
use CodeIgniter\HTTP\RequestInterface;
2120
use CodeIgniter\HTTP\ResponseInterface;
2221
use Config\Exceptions as ExceptionsConfig;
2322
use Config\Paths;
23+
use Config\Services;
2424
use ErrorException;
2525
use Psr\Log\LogLevel;
2626
use Throwable;
@@ -36,6 +36,8 @@ class Exceptions
3636
* Nesting level of the output buffering mechanism
3737
*
3838
* @var int
39+
*
40+
* @deprecated No longer used. Moved to BaseExceptionHandler.
3941
*/
4042
public $ob_level;
4143

@@ -44,6 +46,8 @@ class Exceptions
4446
* cli and html error view directories.
4547
*
4648
* @var string
49+
*
50+
* @deprecated No longer used. Moved to BaseExceptionHandler.
4751
*/
4852
protected $viewPath;
4953

@@ -57,7 +61,7 @@ class Exceptions
5761
/**
5862
* The request.
5963
*
60-
* @var CLIRequest|IncomingRequest
64+
* @var RequestInterface
6165
*/
6266
protected $request;
6367

@@ -71,12 +75,14 @@ class Exceptions
7175
private ?Throwable $exceptionCaughtByExceptionHandler = null;
7276

7377
/**
74-
* @param CLIRequest|IncomingRequest $request
78+
* @param RequestInterface $request
7579
*/
7680
public function __construct(ExceptionsConfig $config, $request, ResponseInterface $response)
7781
{
82+
// For backward compatibility
7883
$this->ob_level = ob_get_level();
7984
$this->viewPath = rtrim($config->errorViewPath, '\\/ ') . DIRECTORY_SEPARATOR;
85+
8086
$this->config = $config;
8187
$this->request = $request;
8288
$this->response = $response;
@@ -110,15 +116,31 @@ public function initialize()
110116
* Catches any uncaught errors and exceptions, including most Fatal errors
111117
* (Yay PHP7!). Will log the error, display it if display_errors is on,
112118
* and fire an event that allows custom actions to be taken at this point.
113-
*
114-
* @codeCoverageIgnore
115119
*/
116120
public function exceptionHandler(Throwable $exception)
117121
{
118122
$this->exceptionCaughtByExceptionHandler = $exception;
119123

120124
[$statusCode, $exitCode] = $this->determineCodes($exception);
121125

126+
$this->request = Services::request();
127+
$this->response = Services::response();
128+
129+
if (method_exists($this->config, 'handler')) {
130+
// Use new ExceptionHandler
131+
$handler = $this->config->handler($statusCode, $exception);
132+
$handler->handle(
133+
$exception,
134+
$this->request,
135+
$this->response,
136+
$statusCode,
137+
$exitCode
138+
);
139+
140+
return;
141+
}
142+
143+
// For backward compatibility
122144
if ($this->config->log === true && ! in_array($statusCode, $this->config->ignoreCodes, true)) {
123145
log_message('critical', "{message}\nin {exFile} on line {exLine}.\n{trace}", [
124146
'message' => $exception->getMessage(),
@@ -212,6 +234,8 @@ public function shutdownHandler()
212234
* whether an HTTP or CLI request, etc.
213235
*
214236
* @return string The path and filename of the view file to use
237+
*
238+
* @deprecated No longer used. Moved to ExceptionHandler.
215239
*/
216240
protected function determineView(Throwable $exception, string $templatePath): string
217241
{
@@ -238,6 +262,8 @@ protected function determineView(Throwable $exception, string $templatePath): st
238262

239263
/**
240264
* Given an exception and status code will display the error to the client.
265+
*
266+
* @deprecated No longer used. Moved to BaseExceptionHandler.
241267
*/
242268
protected function render(Throwable $exception, int $statusCode)
243269
{
@@ -282,6 +308,8 @@ protected function render(Throwable $exception, int $statusCode)
282308

283309
/**
284310
* Gathers the variables that will be made available to the view.
311+
*
312+
* @deprecated No longer used. Moved to BaseExceptionHandler.
285313
*/
286314
protected function collectVars(Throwable $exception, int $statusCode): array
287315
{
@@ -306,6 +334,8 @@ protected function collectVars(Throwable $exception, int $statusCode): array
306334
* Mask sensitive data in the trace.
307335
*
308336
* @param array|object $trace
337+
*
338+
* @deprecated No longer used. Moved to BaseExceptionHandler.
309339
*/
310340
protected function maskSensitiveData(&$trace, array $keysToMask, string $path = '')
311341
{
@@ -416,6 +446,8 @@ public static function cleanPath(string $file): string
416446
/**
417447
* Describes memory usage in real-world units. Intended for use
418448
* with memory_get_usage, etc.
449+
*
450+
* @deprecated No longer used. Moved to BaseExceptionHandler.
419451
*/
420452
public static function describeMemory(int $bytes): string
421453
{
@@ -434,6 +466,8 @@ public static function describeMemory(int $bytes): string
434466
* Creates a syntax-highlighted version of a PHP file.
435467
*
436468
* @return bool|string
469+
*
470+
* @deprecated No longer used. Moved to BaseExceptionHandler.
437471
*/
438472
public static function highlightFile(string $file, int $lineNumber, int $lines = 15)
439473
{

0 commit comments

Comments
 (0)