• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

codeigniter4 / CodeIgniter4 / 21568681844

01 Feb 2026 07:16PM UTC coverage: 85.41% (+1.0%) from 84.387%
21568681844

push

github

web-flow
Merge pull request #9916 from codeigniter4/4.7

4.7.0 Merge code

1603 of 1888 new or added lines in 101 files covered. (84.9%)

31 existing lines in 11 files now uncovered.

22163 of 25949 relevant lines covered (85.41%)

205.52 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

36.91
/system/Debug/Exceptions.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of CodeIgniter 4 framework.
7
 *
8
 * (c) CodeIgniter Foundation <admin@codeigniter.com>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace CodeIgniter\Debug;
15

16
use CodeIgniter\API\ResponseTrait;
17
use CodeIgniter\Exceptions\HasExitCodeInterface;
18
use CodeIgniter\Exceptions\HTTPExceptionInterface;
19
use CodeIgniter\Exceptions\PageNotFoundException;
20
use CodeIgniter\HTTP\Exceptions\HTTPException;
21
use CodeIgniter\HTTP\RequestInterface;
22
use CodeIgniter\HTTP\ResponseInterface;
23
use Config\Exceptions as ExceptionsConfig;
24
use Config\Paths;
25
use ErrorException;
26
use Psr\Log\LogLevel;
27
use Throwable;
28

29
/**
30
 * Exceptions manager
31
 *
32
 * @see \CodeIgniter\Debug\ExceptionsTest
33
 */
34
class Exceptions
35
{
36
    use ResponseTrait;
37

38
    /**
39
     * Nesting level of the output buffering mechanism
40
     *
41
     * @var int
42
     *
43
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
44
     */
45
    public $ob_level;
46

47
    /**
48
     * The path to the directory containing the
49
     * cli and html error view directories.
50
     *
51
     * @var string
52
     *
53
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
54
     */
55
    protected $viewPath;
56

57
    /**
58
     * Config for debug exceptions.
59
     *
60
     * @var ExceptionsConfig
61
     */
62
    protected $config;
63

64
    /**
65
     * The request.
66
     *
67
     * @var RequestInterface|null
68
     */
69
    protected $request;
70

71
    /**
72
     * The outgoing response.
73
     *
74
     * @var ResponseInterface
75
     */
76
    protected $response;
77

78
    private ?Throwable $exceptionCaughtByExceptionHandler = null;
79

80
    public function __construct(ExceptionsConfig $config)
81
    {
82
        // For backward compatibility
83
        $this->ob_level = ob_get_level();
11✔
84
        $this->viewPath = rtrim($config->errorViewPath, '\\/ ') . DIRECTORY_SEPARATOR;
11✔
85

86
        $this->config = $config;
11✔
87
    }
88

89
    /**
90
     * Responsible for registering the error, exception and shutdown
91
     * handling of our application.
92
     *
93
     * @codeCoverageIgnore
94
     *
95
     * @return void
96
     */
97
    public function initialize()
98
    {
99
        set_exception_handler($this->exceptionHandler(...));
3✔
100
        set_error_handler($this->errorHandler(...));
3✔
101
        register_shutdown_function([$this, 'shutdownHandler']);
3✔
102
    }
103

104
    /**
105
     * Catches any uncaught errors and exceptions, including most Fatal errors
106
     * (Yay PHP7!). Will log the error, display it if display_errors is on,
107
     * and fire an event that allows custom actions to be taken at this point.
108
     *
109
     * @return void
110
     */
111
    public function exceptionHandler(Throwable $exception)
112
    {
113
        $this->exceptionCaughtByExceptionHandler = $exception;
×
114

115
        [$statusCode, $exitCode] = $this->determineCodes($exception);
×
116

117
        $this->request = service('request');
×
118

119
        if ($this->config->log === true && ! in_array($statusCode, $this->config->ignoreCodes, true)) {
×
120
            $uri       = $this->request->getPath() === '' ? '/' : $this->request->getPath();
×
121
            $routeInfo = '[Method: ' . $this->request->getMethod() . ', Route: ' . $uri . ']';
×
122

123
            log_message('critical', $exception::class . ": {message}\n{routeInfo}\nin {exFile} on line {exLine}.\n{trace}", [
×
124
                'message'   => $exception->getMessage(),
×
125
                'routeInfo' => $routeInfo,
×
126
                'exFile'    => clean_path($exception->getFile()), // {file} refers to THIS file
×
127
                'exLine'    => $exception->getLine(), // {line} refers to THIS line
×
NEW
128
                'trace'     => render_backtrace($exception->getTrace()),
×
129
            ]);
×
130

131
            // Get the first exception.
132
            $last = $exception;
×
133

134
            while ($prevException = $last->getPrevious()) {
×
135
                $last = $prevException;
×
136

137
                log_message('critical', '[Caused by] ' . $prevException::class . ": {message}\nin {exFile} on line {exLine}.\n{trace}", [
×
138
                    'message' => $prevException->getMessage(),
×
139
                    'exFile'  => clean_path($prevException->getFile()), // {file} refers to THIS file
×
140
                    'exLine'  => $prevException->getLine(), // {line} refers to THIS line
×
NEW
141
                    'trace'   => render_backtrace($prevException->getTrace()),
×
142
                ]);
×
143
            }
144
        }
145

146
        $this->response = service('response');
×
147

148
        if (method_exists($this->config, 'handler')) {
×
149
            // Use new ExceptionHandler
150
            $handler = $this->config->handler($statusCode, $exception);
×
151
            $handler->handle(
×
152
                $exception,
×
153
                $this->request,
×
154
                $this->response,
×
155
                $statusCode,
×
156
                $exitCode,
×
157
            );
×
158

159
            return;
×
160
        }
161

162
        // For backward compatibility
163
        if (! is_cli()) {
×
164
            try {
165
                $this->response->setStatusCode($statusCode);
×
166
            } catch (HTTPException) {
×
167
                // Workaround for invalid HTTP status code.
168
                $statusCode = 500;
×
169
                $this->response->setStatusCode($statusCode);
×
170
            }
171

172
            if (! headers_sent()) {
×
173
                header(sprintf('HTTP/%s %s %s', $this->request->getProtocolVersion(), $this->response->getStatusCode(), $this->response->getReasonPhrase()), true, $statusCode);
×
174
            }
175

176
            if (! str_contains($this->request->getHeaderLine('accept'), 'text/html')) {
×
177
                $this->respond(ENVIRONMENT === 'development' ? $this->collectVars($exception, $statusCode) : '', $statusCode)->send();
×
178

179
                exit($exitCode);
×
180
            }
181
        }
182

183
        $this->render($exception, $statusCode);
×
184

185
        exit($exitCode);
×
186
    }
187

188
    /**
189
     * The callback to be registered to `set_error_handler()`.
190
     *
191
     * @return bool
192
     *
193
     * @throws ErrorException
194
     *
195
     * @codeCoverageIgnore
196
     */
197
    public function errorHandler(int $severity, string $message, ?string $file = null, ?int $line = null)
198
    {
199
        if ($this->isDeprecationError($severity)) {
95✔
200
            if ($this->isSessionSidDeprecationError($message, $file, $line)) {
2✔
201
                return true;
×
202
            }
203

204
            if ($this->isImplicitNullableDeprecationError($message, $file, $line)) {
2✔
205
                return true;
×
206
            }
207

208
            if (! $this->config->logDeprecations || (bool) env('CODEIGNITER_SCREAM_DEPRECATIONS')) {
2✔
209
                throw new ErrorException($message, 0, $severity, $file, $line);
×
210
            }
211

212
            return $this->handleDeprecationError($message, $file, $line);
2✔
213
        }
214

215
        if ((error_reporting() & $severity) !== 0) {
93✔
216
            throw new ErrorException($message, 0, $severity, $file, $line);
60✔
217
        }
218

219
        return false; // return false to propagate the error to PHP standard error handler
33✔
220
    }
221

222
    /**
223
     * Handles session.sid_length and session.sid_bits_per_character deprecations
224
     * in PHP 8.4.
225
     */
226
    private function isSessionSidDeprecationError(string $message, ?string $file = null, ?int $line = null): bool
227
    {
228
        if (
229
            PHP_VERSION_ID >= 80400
2✔
230
            && str_contains($message, 'session.sid_')
2✔
231
        ) {
232
            log_message(
×
233
                LogLevel::WARNING,
×
234
                '[DEPRECATED] {message} in {errFile} on line {errLine}.',
×
235
                [
×
236
                    'message' => $message,
×
237
                    'errFile' => clean_path($file ?? ''),
×
238
                    'errLine' => $line ?? 0,
×
239
                ],
×
240
            );
×
241

242
            return true;
×
243
        }
244

245
        return false;
2✔
246
    }
247

248
    /**
249
     * Workaround to implicit nullable deprecation errors in PHP 8.4.
250
     *
251
     * "Implicitly marking parameter $xxx as nullable is deprecated,
252
     *  the explicit nullable type must be used instead"
253
     *
254
     * @TODO remove this before v4.6.0 release
255
     */
256
    private function isImplicitNullableDeprecationError(string $message, ?string $file = null, ?int $line = null): bool
257
    {
258
        if (
259
            PHP_VERSION_ID >= 80400
2✔
260
            && str_contains($message, 'the explicit nullable type must be used instead')
2✔
261
            // Only Kint and Faker, which cause this error, are logged.
262
            && (str_starts_with($message, 'Kint\\') || str_starts_with($message, 'Faker\\'))
2✔
263
        ) {
264
            log_message(
×
265
                LogLevel::WARNING,
×
266
                '[DEPRECATED] {message} in {errFile} on line {errLine}.',
×
267
                [
×
268
                    'message' => $message,
×
269
                    'errFile' => clean_path($file ?? ''),
×
270
                    'errLine' => $line ?? 0,
×
271
                ],
×
272
            );
×
273

274
            return true;
×
275
        }
276

277
        return false;
2✔
278
    }
279

280
    /**
281
     * Checks to see if any errors have happened during shutdown that
282
     * need to be caught and handle them.
283
     *
284
     * @codeCoverageIgnore
285
     *
286
     * @return void
287
     */
288
    public function shutdownHandler()
289
    {
290
        $error = error_get_last();
×
291

292
        if ($error === null) {
×
293
            return;
×
294
        }
295

296
        ['type' => $type, 'message' => $message, 'file' => $file, 'line' => $line] = $error;
×
297

298
        if ($this->exceptionCaughtByExceptionHandler instanceof Throwable) {
×
299
            $message .= "\n【Previous Exception】\n"
×
300
                . $this->exceptionCaughtByExceptionHandler::class . "\n"
×
301
                . $this->exceptionCaughtByExceptionHandler->getMessage() . "\n"
×
302
                . $this->exceptionCaughtByExceptionHandler->getTraceAsString();
×
303
        }
304

305
        if (in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE], true)) {
×
306
            $this->exceptionHandler(new ErrorException($message, 0, $type, $file, $line));
×
307
        }
308
    }
309

310
    /**
311
     * Determines the view to display based on the exception thrown,
312
     * whether an HTTP or CLI request, etc.
313
     *
314
     * @return string The path and filename of the view file to use
315
     *
316
     * @deprecated 4.4.0 No longer used. Moved to ExceptionHandler.
317
     */
318
    protected function determineView(Throwable $exception, string $templatePath): string
319
    {
320
        // Production environments should have a custom exception file.
321
        $view         = 'production.php';
1✔
322
        $templatePath = rtrim($templatePath, '\\/ ') . DIRECTORY_SEPARATOR;
1✔
323

324
        if (
325
            in_array(
1✔
326
                strtolower(ini_get('display_errors')),
1✔
327
                ['1', 'true', 'on', 'yes'],
1✔
328
                true,
1✔
329
            )
1✔
330
        ) {
331
            $view = 'error_exception.php';
1✔
332
        }
333

334
        // 404 Errors
335
        if ($exception instanceof PageNotFoundException) {
1✔
336
            return 'error_404.php';
1✔
337
        }
338

339
        // Allow for custom views based upon the status code
340
        if (is_file($templatePath . 'error_' . $exception->getCode() . '.php')) {
1✔
341
            return 'error_' . $exception->getCode() . '.php';
1✔
342
        }
343

344
        return $view;
1✔
345
    }
346

347
    /**
348
     * Given an exception and status code will display the error to the client.
349
     *
350
     * @return void
351
     *
352
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
353
     */
354
    protected function render(Throwable $exception, int $statusCode)
355
    {
356
        // Determine possible directories of error views
357
        $path    = $this->viewPath;
×
358
        $altPath = rtrim((new Paths())->viewDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'errors' . DIRECTORY_SEPARATOR;
×
359

360
        $path    .= (is_cli() ? 'cli' : 'html') . DIRECTORY_SEPARATOR;
×
361
        $altPath .= (is_cli() ? 'cli' : 'html') . DIRECTORY_SEPARATOR;
×
362

363
        // Determine the views
364
        $view    = $this->determineView($exception, $path);
×
365
        $altView = $this->determineView($exception, $altPath);
×
366

367
        // Check if the view exists
368
        if (is_file($path . $view)) {
×
369
            $viewFile = $path . $view;
×
370
        } elseif (is_file($altPath . $altView)) {
×
371
            $viewFile = $altPath . $altView;
×
372
        }
373

374
        if (! isset($viewFile)) {
×
375
            echo 'The error view files were not found. Cannot render exception trace.';
×
376

377
            exit(1);
×
378
        }
379

380
        echo (function () use ($exception, $statusCode, $viewFile): string {
×
381
            $vars = $this->collectVars($exception, $statusCode);
×
382
            extract($vars, EXTR_SKIP);
×
383

384
            ob_start();
×
385
            include $viewFile;
×
386

387
            return ob_get_clean();
×
388
        })();
×
389
    }
390

391
    /**
392
     * Gathers the variables that will be made available to the view.
393
     *
394
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
395
     */
396
    protected function collectVars(Throwable $exception, int $statusCode): array
397
    {
398
        // Get the first exception.
399
        $firstException = $exception;
1✔
400

401
        while ($prevException = $firstException->getPrevious()) {
1✔
402
            $firstException = $prevException;
×
403
        }
404

405
        $trace = $firstException->getTrace();
1✔
406

407
        if ($this->config->sensitiveDataInTrace !== []) {
1✔
408
            $trace = $this->maskSensitiveData($trace, $this->config->sensitiveDataInTrace);
×
409
        }
410

411
        return [
1✔
412
            'title'   => $exception::class,
1✔
413
            'type'    => $exception::class,
1✔
414
            'code'    => $statusCode,
1✔
415
            'message' => $exception->getMessage(),
1✔
416
            'file'    => $exception->getFile(),
1✔
417
            'line'    => $exception->getLine(),
1✔
418
            'trace'   => $trace,
1✔
419
        ];
1✔
420
    }
421

422
    /**
423
     * Mask sensitive data in the trace.
424
     *
425
     * @param array $trace
426
     *
427
     * @return array
428
     *
429
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
430
     */
431
    protected function maskSensitiveData($trace, array $keysToMask, string $path = '')
432
    {
433
        foreach ($trace as $i => $line) {
2✔
434
            $trace[$i]['args'] = $this->maskData($line['args'], $keysToMask);
2✔
435
        }
436

437
        return $trace;
2✔
438
    }
439

440
    /**
441
     * @param array|object $args
442
     *
443
     * @return array|object
444
     *
445
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
446
     */
447
    private function maskData($args, array $keysToMask, string $path = '')
448
    {
449
        foreach ($keysToMask as $keyToMask) {
2✔
450
            $explode = explode('/', $keyToMask);
2✔
451
            $index   = end($explode);
2✔
452

453
            if (str_starts_with(strrev($path . '/' . $index), strrev($keyToMask))) {
2✔
454
                if (is_array($args) && array_key_exists($index, $args)) {
2✔
455
                    $args[$index] = '******************';
1✔
456
                } elseif (
457
                    is_object($args) && property_exists($args, $index)
2✔
458
                    && isset($args->{$index}) && is_scalar($args->{$index})
2✔
459
                ) {
460
                    $args->{$index} = '******************';
1✔
461
                }
462
            }
463
        }
464

465
        if (is_array($args)) {
2✔
466
            foreach ($args as $pathKey => $subarray) {
2✔
467
                $args[$pathKey] = $this->maskData($subarray, $keysToMask, $path . '/' . $pathKey);
1✔
468
            }
469
        } elseif (is_object($args)) {
1✔
470
            foreach ($args as $pathKey => $subarray) {
1✔
471
                $args->{$pathKey} = $this->maskData($subarray, $keysToMask, $path . '/' . $pathKey);
1✔
472
            }
473
        }
474

475
        return $args;
2✔
476
    }
477

478
    /**
479
     * Determines the HTTP status code and the exit status code for this request.
480
     */
481
    protected function determineCodes(Throwable $exception): array
482
    {
483
        $statusCode = 500;
1✔
484
        $exitStatus = EXIT_ERROR;
1✔
485

486
        if ($exception instanceof HTTPExceptionInterface) {
1✔
487
            $statusCode = $exception->getCode();
×
488
        }
489

490
        if ($exception instanceof HasExitCodeInterface) {
1✔
491
            $exitStatus = $exception->getExitCode();
1✔
492
        }
493

494
        return [$statusCode, $exitStatus];
1✔
495
    }
496

497
    private function isDeprecationError(int $error): bool
498
    {
499
        $deprecations = E_DEPRECATED | E_USER_DEPRECATED;
95✔
500

501
        return ($error & $deprecations) !== 0;
95✔
502
    }
503

504
    /**
505
     * @return true
506
     */
507
    private function handleDeprecationError(string $message, ?string $file = null, ?int $line = null): bool
508
    {
509
        // Remove the trace of the error handler.
510
        $trace = array_slice(debug_backtrace(), 2);
2✔
511

512
        log_message(
2✔
513
            $this->config->deprecationLogLevel,
2✔
514
            "[DEPRECATED] {message} in {errFile} on line {errLine}.\n{trace}",
2✔
515
            [
2✔
516
                'message' => $message,
2✔
517
                'errFile' => clean_path($file ?? ''),
2✔
518
                'errLine' => $line ?? 0,
2✔
519
                'trace'   => render_backtrace($trace),
2✔
520
            ],
2✔
521
        );
2✔
522

523
        return true;
2✔
524
    }
525

526
    // --------------------------------------------------------------------
527
    // Display Methods
528
    // --------------------------------------------------------------------
529

530
    /**
531
     * This makes nicer looking paths for the error output.
532
     *
533
     * @deprecated Use dedicated `clean_path()` function.
534
     */
535
    public static function cleanPath(string $file): string
536
    {
537
        return match (true) {
538
            str_starts_with($file, APPPATH)                             => 'APPPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(APPPATH)),
×
539
            str_starts_with($file, SYSTEMPATH)                          => 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(SYSTEMPATH)),
×
540
            str_starts_with($file, FCPATH)                              => 'FCPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(FCPATH)),
×
541
            defined('VENDORPATH') && str_starts_with($file, VENDORPATH) => 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(VENDORPATH)),
×
542
            default                                                     => $file,
×
543
        };
544
    }
545

546
    /**
547
     * Describes memory usage in real-world units. Intended for use
548
     * with memory_get_usage, etc.
549
     *
550
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
551
     */
552
    public static function describeMemory(int $bytes): string
553
    {
554
        if ($bytes < 1024) {
×
555
            return $bytes . 'B';
×
556
        }
557

558
        if ($bytes < 1_048_576) {
×
559
            return round($bytes / 1024, 2) . 'KB';
×
560
        }
561

562
        return round($bytes / 1_048_576, 2) . 'MB';
×
563
    }
564

565
    /**
566
     * Creates a syntax-highlighted version of a PHP file.
567
     *
568
     * @return bool|string
569
     *
570
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
571
     */
572
    public static function highlightFile(string $file, int $lineNumber, int $lines = 15)
573
    {
574
        if ($file === '' || ! is_readable($file)) {
×
575
            return false;
×
576
        }
577

578
        // Set our highlight colors:
579
        if (function_exists('ini_set')) {
×
580
            ini_set('highlight.comment', '#767a7e; font-style: italic');
×
581
            ini_set('highlight.default', '#c7c7c7');
×
582
            ini_set('highlight.html', '#06B');
×
583
            ini_set('highlight.keyword', '#f1ce61;');
×
584
            ini_set('highlight.string', '#869d6a');
×
585
        }
586

587
        try {
588
            $source = file_get_contents($file);
×
589
        } catch (Throwable) {
×
590
            return false;
×
591
        }
592

593
        $source = str_replace(["\r\n", "\r"], "\n", $source);
×
594
        $source = explode("\n", highlight_string($source, true));
×
595
        $source = str_replace('<br />', "\n", $source[1]);
×
596
        $source = explode("\n", str_replace("\r\n", "\n", $source));
×
597

598
        // Get just the part to show
599
        $start = max($lineNumber - (int) round($lines / 2), 0);
×
600

601
        // Get just the lines we need to display, while keeping line numbers...
602
        $source = array_splice($source, $start, $lines, true);
×
603

604
        // Used to format the line number in the source
605
        $format = '% ' . strlen((string) ($start + $lines)) . 'd';
×
606

607
        $out = '';
×
608
        // Because the highlighting may have an uneven number
609
        // of open and close span tags on one line, we need
610
        // to ensure we can close them all to get the lines
611
        // showing correctly.
612
        $spans = 1;
×
613

614
        foreach ($source as $n => $row) {
×
615
            $spans += substr_count($row, '<span') - substr_count($row, '</span');
×
616
            $row = str_replace(["\r", "\n"], ['', ''], $row);
×
617

618
            if (($n + $start + 1) === $lineNumber) {
×
619
                preg_match_all('#<[^>]+>#', $row, $tags);
×
620

621
                $out .= sprintf(
×
622
                    "<span class='line highlight'><span class='number'>{$format}</span> %s\n</span>%s",
×
623
                    $n + $start + 1,
×
624
                    strip_tags($row),
×
625
                    implode('', $tags[0]),
×
626
                );
×
627
            } else {
628
                $out .= sprintf('<span class="line"><span class="number">' . $format . '</span> %s', $n + $start + 1, $row) . "\n";
×
629
            }
630
        }
631

632
        if ($spans > 0) {
×
633
            $out .= str_repeat('</span>', $spans);
×
634
        }
635

636
        return '<pre><code>' . $out . '</code></pre>';
×
637
    }
638
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc