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

codeigniter4 / CodeIgniter4 / 6997881217

26 Nov 2023 08:37PM UTC coverage: 85.221% (-0.04%) from 85.263%
6997881217

push

github

web-flow
Merge pull request #8239 from kenjis/fix-ExceptionHandler-exception-display

fix: ExceptionHandler displays incorrect Exception classname

3 of 18 new or added lines in 2 files covered. (16.67%)

1 existing line in 1 file now uncovered.

18585 of 21808 relevant lines covered (85.22%)

199.39 hits per line

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

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

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

12
namespace CodeIgniter\Debug;
13

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

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

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

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

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

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

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

77
    private ?Throwable $exceptionCaughtByExceptionHandler = null;
78

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

85
        $this->config = $config;
1,675✔
86

87
        // workaround for upgraded users
88
        // This causes "Deprecated: Creation of dynamic property" in PHP 8.2.
89
        // @TODO remove this after dropping PHP 8.1 support.
90
        if (! isset($this->config->sensitiveDataInTrace)) {
1,675✔
91
            $this->config->sensitiveDataInTrace = [];
×
92
        }
93
        if (! isset($this->config->logDeprecations, $this->config->deprecationLogLevel)) {
1,675✔
94
            $this->config->logDeprecations     = false;
×
95
            $this->config->deprecationLogLevel = LogLevel::WARNING;
×
96
        }
97
    }
98

99
    /**
100
     * Responsible for registering the error, exception and shutdown
101
     * handling of our application.
102
     *
103
     * @codeCoverageIgnore
104
     *
105
     * @return void
106
     */
107
    public function initialize()
108
    {
109
        set_exception_handler([$this, 'exceptionHandler']);
110
        set_error_handler([$this, 'errorHandler']);
111
        register_shutdown_function([$this, 'shutdownHandler']);
112
    }
113

114
    /**
115
     * Catches any uncaught errors and exceptions, including most Fatal errors
116
     * (Yay PHP7!). Will log the error, display it if display_errors is on,
117
     * and fire an event that allows custom actions to be taken at this point.
118
     *
119
     * @return void
120
     */
121
    public function exceptionHandler(Throwable $exception)
122
    {
123
        $this->exceptionCaughtByExceptionHandler = $exception;
×
124

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

127
        if ($this->config->log === true && ! in_array($statusCode, $this->config->ignoreCodes, true)) {
×
NEW
128
            log_message('critical', get_class($exception) . ": {message}\nin {exFile} on line {exLine}.\n{trace}", [
×
129
                'message' => $exception->getMessage(),
×
130
                'exFile'  => clean_path($exception->getFile()), // {file} refers to THIS file
×
131
                'exLine'  => $exception->getLine(), // {line} refers to THIS line
×
132
                'trace'   => self::renderBacktrace($exception->getTrace()),
×
133
            ]);
×
134

135
            // Get the first exception.
NEW
136
            $last = $exception;
×
137

NEW
138
            while ($prevException = $last->getPrevious()) {
×
NEW
139
                $last = $prevException;
×
140

NEW
141
                log_message('critical', '[Caused by] ' . get_class($prevException) . ": {message}\nin {exFile} on line {exLine}.\n{trace}", [
×
NEW
142
                    'message' => $prevException->getMessage(),
×
NEW
143
                    'exFile'  => clean_path($prevException->getFile()), // {file} refers to THIS file
×
NEW
144
                    'exLine'  => $prevException->getLine(), // {line} refers to THIS line
×
NEW
145
                    'trace'   => self::renderBacktrace($prevException->getTrace()),
×
NEW
146
                ]);
×
147
            }
148
        }
149

150
        $this->request  = Services::request();
×
151
        $this->response = Services::response();
×
152

UNCOV
153
        if (method_exists($this->config, 'handler')) {
×
154
            // Use new ExceptionHandler
155
            $handler = $this->config->handler($statusCode, $exception);
×
156
            $handler->handle(
×
157
                $exception,
×
158
                $this->request,
×
159
                $this->response,
×
160
                $statusCode,
×
161
                $exitCode
×
162
            );
×
163

164
            return;
×
165
        }
166

167
        // For backward compatibility
168
        if (! is_cli()) {
×
169
            try {
170
                $this->response->setStatusCode($statusCode);
×
171
            } catch (HTTPException $e) {
×
172
                // Workaround for invalid HTTP status code.
173
                $statusCode = 500;
×
174
                $this->response->setStatusCode($statusCode);
×
175
            }
176

177
            if (! headers_sent()) {
×
178
                header(sprintf('HTTP/%s %s %s', $this->request->getProtocolVersion(), $this->response->getStatusCode(), $this->response->getReasonPhrase()), true, $statusCode);
×
179
            }
180

181
            if (strpos($this->request->getHeaderLine('accept'), 'text/html') === false) {
×
182
                $this->respond(ENVIRONMENT === 'development' ? $this->collectVars($exception, $statusCode) : '', $statusCode)->send();
×
183

184
                exit($exitCode);
×
185
            }
186
        }
187

188
        $this->render($exception, $statusCode);
×
189

190
        exit($exitCode);
×
191
    }
192

193
    /**
194
     * The callback to be registered to `set_error_handler()`.
195
     *
196
     * @return bool
197
     *
198
     * @throws ErrorException
199
     *
200
     * @codeCoverageIgnore
201
     */
202
    public function errorHandler(int $severity, string $message, ?string $file = null, ?int $line = null)
203
    {
204
        if ($this->isDeprecationError($severity)) {
205
            if (! $this->config->logDeprecations || (bool) env('CODEIGNITER_SCREAM_DEPRECATIONS')) {
206
                throw new ErrorException($message, 0, $severity, $file, $line);
207
            }
208

209
            return $this->handleDeprecationError($message, $file, $line);
210
        }
211

212
        if ((error_reporting() & $severity) !== 0) {
213
            throw new ErrorException($message, 0, $severity, $file, $line);
214
        }
215

216
        return false; // return false to propagate the error to PHP standard error handler
217
    }
218

219
    /**
220
     * Checks to see if any errors have happened during shutdown that
221
     * need to be caught and handle them.
222
     *
223
     * @codeCoverageIgnore
224
     *
225
     * @return void
226
     */
227
    public function shutdownHandler()
228
    {
229
        $error = error_get_last();
230

231
        if ($error === null) {
232
            return;
233
        }
234

235
        ['type' => $type, 'message' => $message, 'file' => $file, 'line' => $line] = $error;
236

237
        if ($this->exceptionCaughtByExceptionHandler instanceof Throwable) {
238
            $message .= "\n【Previous Exception】\n"
239
                . get_class($this->exceptionCaughtByExceptionHandler) . "\n"
240
                . $this->exceptionCaughtByExceptionHandler->getMessage() . "\n"
241
                . $this->exceptionCaughtByExceptionHandler->getTraceAsString();
242
        }
243

244
        if (in_array($type, [E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE], true)) {
245
            $this->exceptionHandler(new ErrorException($message, 0, $type, $file, $line));
246
        }
247
    }
248

249
    /**
250
     * Determines the view to display based on the exception thrown,
251
     * whether an HTTP or CLI request, etc.
252
     *
253
     * @return string The path and filename of the view file to use
254
     *
255
     * @deprecated 4.4.0 No longer used. Moved to ExceptionHandler.
256
     */
257
    protected function determineView(Throwable $exception, string $templatePath): string
258
    {
259
        // Production environments should have a custom exception file.
260
        $view         = 'production.php';
×
261
        $templatePath = rtrim($templatePath, '\\/ ') . DIRECTORY_SEPARATOR;
×
262

263
        if (
264
            in_array(
×
265
                strtolower(ini_get('display_errors')),
×
266
                ['1', 'true', 'on', 'yes'],
×
267
                true
×
268
            )
×
269
        ) {
270
            $view = 'error_exception.php';
×
271
        }
272

273
        // 404 Errors
274
        if ($exception instanceof PageNotFoundException) {
×
275
            return 'error_404.php';
×
276
        }
277

278
        // Allow for custom views based upon the status code
279
        if (is_file($templatePath . 'error_' . $exception->getCode() . '.php')) {
×
280
            return 'error_' . $exception->getCode() . '.php';
×
281
        }
282

283
        return $view;
×
284
    }
285

286
    /**
287
     * Given an exception and status code will display the error to the client.
288
     *
289
     * @return void
290
     *
291
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
292
     */
293
    protected function render(Throwable $exception, int $statusCode)
294
    {
295
        // Determine possible directories of error views
296
        $path    = $this->viewPath;
×
297
        $altPath = rtrim((new Paths())->viewDirectory, '\\/ ') . DIRECTORY_SEPARATOR . 'errors' . DIRECTORY_SEPARATOR;
×
298

299
        $path    .= (is_cli() ? 'cli' : 'html') . DIRECTORY_SEPARATOR;
×
300
        $altPath .= (is_cli() ? 'cli' : 'html') . DIRECTORY_SEPARATOR;
×
301

302
        // Determine the views
303
        $view    = $this->determineView($exception, $path);
×
304
        $altView = $this->determineView($exception, $altPath);
×
305

306
        // Check if the view exists
307
        if (is_file($path . $view)) {
×
308
            $viewFile = $path . $view;
×
309
        } elseif (is_file($altPath . $altView)) {
×
310
            $viewFile = $altPath . $altView;
×
311
        }
312

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

316
            exit(1);
×
317
        }
318

319
        echo(function () use ($exception, $statusCode, $viewFile): string {
×
320
            $vars = $this->collectVars($exception, $statusCode);
×
321
            extract($vars, EXTR_SKIP);
×
322

323
            ob_start();
×
324
            include $viewFile;
×
325

326
            return ob_get_clean();
×
327
        })();
×
328
    }
329

330
    /**
331
     * Gathers the variables that will be made available to the view.
332
     *
333
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
334
     */
335
    protected function collectVars(Throwable $exception, int $statusCode): array
336
    {
337
        // Get the first exception.
NEW
338
        $firstException = $exception;
×
339

NEW
340
        while ($prevException = $firstException->getPrevious()) {
×
NEW
341
            $firstException = $prevException;
×
342
        }
343

NEW
344
        $trace = $firstException->getTrace();
×
345

346
        if ($this->config->sensitiveDataInTrace !== []) {
×
347
            $trace = $this->maskSensitiveData($trace, $this->config->sensitiveDataInTrace);
×
348
        }
349

350
        return [
×
351
            'title'   => get_class($exception),
×
352
            'type'    => get_class($exception),
×
353
            'code'    => $statusCode,
×
354
            'message' => $exception->getMessage(),
×
355
            'file'    => $exception->getFile(),
×
356
            'line'    => $exception->getLine(),
×
357
            'trace'   => $trace,
×
358
        ];
×
359
    }
360

361
    /**
362
     * Mask sensitive data in the trace.
363
     *
364
     * @param array $trace
365
     *
366
     * @return array
367
     *
368
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
369
     */
370
    protected function maskSensitiveData($trace, array $keysToMask, string $path = '')
371
    {
372
        foreach ($trace as $i => $line) {
×
373
            $trace[$i]['args'] = $this->maskData($line['args'], $keysToMask);
×
374
        }
375

376
        return $trace;
×
377
    }
378

379
    /**
380
     * @param array|object $args
381
     *
382
     * @return array|object
383
     *
384
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
385
     */
386
    private function maskData($args, array $keysToMask, string $path = '')
387
    {
388
        foreach ($keysToMask as $keyToMask) {
×
389
            $explode = explode('/', $keyToMask);
×
390
            $index   = end($explode);
×
391

392
            if (strpos(strrev($path . '/' . $index), strrev($keyToMask)) === 0) {
×
393
                if (is_array($args) && array_key_exists($index, $args)) {
×
394
                    $args[$index] = '******************';
×
395
                } elseif (
396
                    is_object($args) && property_exists($args, $index)
×
397
                    && isset($args->{$index}) && is_scalar($args->{$index})
×
398
                ) {
399
                    $args->{$index} = '******************';
×
400
                }
401
            }
402
        }
403

404
        if (is_array($args)) {
×
405
            foreach ($args as $pathKey => $subarray) {
×
406
                $args[$pathKey] = $this->maskData($subarray, $keysToMask, $path . '/' . $pathKey);
×
407
            }
408
        } elseif (is_object($args)) {
×
409
            foreach ($args as $pathKey => $subarray) {
×
410
                $args->{$pathKey} = $this->maskData($subarray, $keysToMask, $path . '/' . $pathKey);
×
411
            }
412
        }
413

414
        return $args;
×
415
    }
416

417
    /**
418
     * Determines the HTTP status code and the exit status code for this request.
419
     */
420
    protected function determineCodes(Throwable $exception): array
421
    {
422
        $statusCode = 500;
1✔
423
        $exitStatus = EXIT_ERROR;
1✔
424

425
        if ($exception instanceof HTTPExceptionInterface) {
1✔
426
            $statusCode = $exception->getCode();
×
427
        }
428

429
        if ($exception instanceof HasExitCodeInterface) {
1✔
430
            $exitStatus = $exception->getExitCode();
1✔
431
        }
432

433
        return [$statusCode, $exitStatus];
1✔
434
    }
435

436
    private function isDeprecationError(int $error): bool
437
    {
438
        $deprecations = E_DEPRECATED | E_USER_DEPRECATED;
65✔
439

440
        return ($error & $deprecations) !== 0;
65✔
441
    }
442

443
    /**
444
     * @return true
445
     */
446
    private function handleDeprecationError(string $message, ?string $file = null, ?int $line = null): bool
447
    {
448
        // Remove the trace of the error handler.
449
        $trace = array_slice(debug_backtrace(), 2);
2✔
450

451
        log_message(
2✔
452
            $this->config->deprecationLogLevel,
2✔
453
            "[DEPRECATED] {message} in {errFile} on line {errLine}.\n{trace}",
2✔
454
            [
2✔
455
                'message' => $message,
2✔
456
                'errFile' => clean_path($file ?? ''),
2✔
457
                'errLine' => $line ?? 0,
2✔
458
                'trace'   => self::renderBacktrace($trace),
2✔
459
            ]
2✔
460
        );
2✔
461

462
        return true;
2✔
463
    }
464

465
    // --------------------------------------------------------------------
466
    // Display Methods
467
    // --------------------------------------------------------------------
468

469
    /**
470
     * This makes nicer looking paths for the error output.
471
     *
472
     * @deprecated Use dedicated `clean_path()` function.
473
     */
474
    public static function cleanPath(string $file): string
475
    {
476
        switch (true) {
477
            case strpos($file, APPPATH) === 0:
×
478
                $file = 'APPPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(APPPATH));
×
479
                break;
×
480

481
            case strpos($file, SYSTEMPATH) === 0:
×
482
                $file = 'SYSTEMPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(SYSTEMPATH));
×
483
                break;
×
484

485
            case strpos($file, FCPATH) === 0:
×
486
                $file = 'FCPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(FCPATH));
×
487
                break;
×
488

489
            case defined('VENDORPATH') && strpos($file, VENDORPATH) === 0:
×
490
                $file = 'VENDORPATH' . DIRECTORY_SEPARATOR . substr($file, strlen(VENDORPATH));
×
491
                break;
×
492
        }
493

494
        return $file;
×
495
    }
496

497
    /**
498
     * Describes memory usage in real-world units. Intended for use
499
     * with memory_get_usage, etc.
500
     *
501
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
502
     */
503
    public static function describeMemory(int $bytes): string
504
    {
505
        if ($bytes < 1024) {
×
506
            return $bytes . 'B';
×
507
        }
508

509
        if ($bytes < 1_048_576) {
×
510
            return round($bytes / 1024, 2) . 'KB';
×
511
        }
512

513
        return round($bytes / 1_048_576, 2) . 'MB';
×
514
    }
515

516
    /**
517
     * Creates a syntax-highlighted version of a PHP file.
518
     *
519
     * @return bool|string
520
     *
521
     * @deprecated 4.4.0 No longer used. Moved to BaseExceptionHandler.
522
     */
523
    public static function highlightFile(string $file, int $lineNumber, int $lines = 15)
524
    {
525
        if (empty($file) || ! is_readable($file)) {
×
526
            return false;
×
527
        }
528

529
        // Set our highlight colors:
530
        if (function_exists('ini_set')) {
×
531
            ini_set('highlight.comment', '#767a7e; font-style: italic');
×
532
            ini_set('highlight.default', '#c7c7c7');
×
533
            ini_set('highlight.html', '#06B');
×
534
            ini_set('highlight.keyword', '#f1ce61;');
×
535
            ini_set('highlight.string', '#869d6a');
×
536
        }
537

538
        try {
539
            $source = file_get_contents($file);
×
540
        } catch (Throwable $e) {
×
541
            return false;
×
542
        }
543

544
        $source = str_replace(["\r\n", "\r"], "\n", $source);
×
545
        $source = explode("\n", highlight_string($source, true));
×
546
        $source = str_replace('<br />', "\n", $source[1]);
×
547
        $source = explode("\n", str_replace("\r\n", "\n", $source));
×
548

549
        // Get just the part to show
550
        $start = max($lineNumber - (int) round($lines / 2), 0);
×
551

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

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

558
        $out = '';
×
559
        // Because the highlighting may have an uneven number
560
        // of open and close span tags on one line, we need
561
        // to ensure we can close them all to get the lines
562
        // showing correctly.
563
        $spans = 1;
×
564

565
        foreach ($source as $n => $row) {
×
566
            $spans += substr_count($row, '<span') - substr_count($row, '</span');
×
567
            $row = str_replace(["\r", "\n"], ['', ''], $row);
×
568

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

572
                $out .= sprintf(
×
573
                    "<span class='line highlight'><span class='number'>{$format}</span> %s\n</span>%s",
×
574
                    $n + $start + 1,
×
575
                    strip_tags($row),
×
576
                    implode('', $tags[0])
×
577
                );
×
578
            } else {
579
                $out .= sprintf('<span class="line"><span class="number">' . $format . '</span> %s', $n + $start + 1, $row) . "\n";
×
580
            }
581
        }
582

583
        if ($spans > 0) {
×
584
            $out .= str_repeat('</span>', $spans);
×
585
        }
586

587
        return '<pre><code>' . $out . '</code></pre>';
×
588
    }
589

590
    private static function renderBacktrace(array $backtrace): string
591
    {
592
        $backtraces = [];
3✔
593

594
        foreach ($backtrace as $index => $trace) {
3✔
595
            $frame = $trace + ['file' => '[internal function]', 'line' => '', 'class' => '', 'type' => '', 'args' => []];
3✔
596

597
            if ($frame['file'] !== '[internal function]') {
3✔
598
                $frame['file'] = sprintf('%s(%s)', $frame['file'], $frame['line']);
3✔
599
            }
600

601
            unset($frame['line']);
3✔
602
            $idx = $index;
3✔
603
            $idx = str_pad((string) ++$idx, 2, ' ', STR_PAD_LEFT);
3✔
604

605
            $args = implode(', ', array_map(static function ($value): string {
3✔
606
                switch (true) {
607
                    case is_object($value):
3✔
608
                        return sprintf('Object(%s)', get_class($value));
2✔
609

610
                    case is_array($value):
3✔
611
                        return $value !== [] ? '[...]' : '[]';
2✔
612

613
                    case $value === null:
3✔
614
                        return 'null';
1✔
615

616
                    case is_resource($value):
3✔
617
                        return sprintf('resource (%s)', get_resource_type($value));
×
618

619
                    default:
620
                        return var_export($value, true);
3✔
621
                }
622
            }, $frame['args']));
3✔
623

624
            $backtraces[] = sprintf(
3✔
625
                '%s %s: %s%s%s(%s)',
3✔
626
                $idx,
3✔
627
                clean_path($frame['file']),
3✔
628
                $frame['class'],
3✔
629
                $frame['type'],
3✔
630
                $frame['function'],
3✔
631
                $args
3✔
632
            );
3✔
633
        }
634

635
        return implode("\n", $backtraces);
3✔
636
    }
637
}
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

© 2025 Coveralls, Inc