Skip to content

Commit 8657525

Browse files
authored
Merge pull request #8239 from kenjis/fix-ExceptionHandler-exception-display
fix: ExceptionHandler displays incorrect Exception classname
2 parents 99c1a4b + 886aa89 commit 8657525

File tree

5 files changed

+75
-12
lines changed

5 files changed

+75
-12
lines changed

app/Views/errors/cli/error_exception.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,26 @@
33
use CodeIgniter\CLI\CLI;
44

55
// The main Exception
6-
CLI::newLine();
76
CLI::write('[' . get_class($exception) . ']', 'light_gray', 'red');
8-
CLI::newLine();
97
CLI::write($message);
10-
CLI::newLine();
118
CLI::write('at ' . CLI::color(clean_path($exception->getFile()) . ':' . $exception->getLine(), 'green'));
129
CLI::newLine();
1310

11+
$last = $exception;
12+
13+
while ($prevException = $last->getPrevious()) {
14+
$last = $prevException;
15+
16+
CLI::write(' Caused by:');
17+
CLI::write(' [' . get_class($prevException) . ']', 'red');
18+
CLI::write(' ' . $prevException->getMessage());
19+
CLI::write(' at ' . CLI::color(clean_path($prevException->getFile()) . ':' . $prevException->getLine(), 'green'));
20+
CLI::newLine();
21+
}
22+
1423
// The backtrace
1524
if (defined('SHOW_DEBUG_BACKTRACE') && SHOW_DEBUG_BACKTRACE) {
16-
$backtraces = $exception->getTrace();
25+
$backtraces = $last->getTrace();
1726

1827
if ($backtraces) {
1928
CLI::write('Backtrace:', 'green');

app/Views/errors/html/error_exception.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,29 @@
4444
<?php endif; ?>
4545
</div>
4646

47+
<div class="container">
48+
<?php
49+
$last = $exception;
50+
51+
while ($prevException = $last->getPrevious()) {
52+
$last = $prevException;
53+
?>
54+
55+
<pre>
56+
Caused by:
57+
<?= esc(get_class($prevException)), esc($prevException->getCode() ? ' #' . $prevException->getCode() : '') ?>
58+
59+
<?= nl2br(esc($prevException->getMessage())) ?>
60+
<a href="https://www.duckduckgo.com/?q=<?= urlencode(get_class($prevException) . ' ' . preg_replace('#\'.*\'|".*"#Us', '', $prevException->getMessage())) ?>"
61+
rel="noreferrer" target="_blank">search &rarr;</a>
62+
<?= esc(clean_path($prevException->getFile()) . ':' . $prevException->getLine()) ?>
63+
</pre>
64+
65+
<?php
66+
}
67+
?>
68+
</div>
69+
4770
<?php if (defined('SHOW_DEBUG_BACKTRACE') && SHOW_DEBUG_BACKTRACE) : ?>
4871
<div class="container">
4972

system/Debug/BaseExceptionHandler.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,14 @@ abstract public function handle(
6767
*/
6868
protected function collectVars(Throwable $exception, int $statusCode): array
6969
{
70-
$trace = $exception->getTrace();
70+
// Get the first exception.
71+
$firstException = $exception;
72+
73+
while ($prevException = $firstException->getPrevious()) {
74+
$firstException = $prevException;
75+
}
76+
77+
$trace = $firstException->getTrace();
7178

7279
if ($this->config->sensitiveDataInTrace !== []) {
7380
$trace = $this->maskSensitiveData($trace, $this->config->sensitiveDataInTrace);

system/Debug/Exceptions.php

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,22 +125,31 @@ public function exceptionHandler(Throwable $exception)
125125
[$statusCode, $exitCode] = $this->determineCodes($exception);
126126

127127
if ($this->config->log === true && ! in_array($statusCode, $this->config->ignoreCodes, true)) {
128-
log_message('critical', "{message}\nin {exFile} on line {exLine}.\n{trace}", [
128+
log_message('critical', get_class($exception) . ": {message}\nin {exFile} on line {exLine}.\n{trace}", [
129129
'message' => $exception->getMessage(),
130130
'exFile' => clean_path($exception->getFile()), // {file} refers to THIS file
131131
'exLine' => $exception->getLine(), // {line} refers to THIS line
132132
'trace' => self::renderBacktrace($exception->getTrace()),
133133
]);
134+
135+
// Get the first exception.
136+
$last = $exception;
137+
138+
while ($prevException = $last->getPrevious()) {
139+
$last = $prevException;
140+
141+
log_message('critical', '[Caused by] ' . get_class($prevException) . ": {message}\nin {exFile} on line {exLine}.\n{trace}", [
142+
'message' => $prevException->getMessage(),
143+
'exFile' => clean_path($prevException->getFile()), // {file} refers to THIS file
144+
'exLine' => $prevException->getLine(), // {line} refers to THIS line
145+
'trace' => self::renderBacktrace($prevException->getTrace()),
146+
]);
147+
}
134148
}
135149

136150
$this->request = Services::request();
137151
$this->response = Services::response();
138152

139-
// Get the first exception.
140-
while ($prevException = $exception->getPrevious()) {
141-
$exception = $prevException;
142-
}
143-
144153
if (method_exists($this->config, 'handler')) {
145154
// Use new ExceptionHandler
146155
$handler = $this->config->handler($statusCode, $exception);
@@ -325,7 +334,14 @@ protected function render(Throwable $exception, int $statusCode)
325334
*/
326335
protected function collectVars(Throwable $exception, int $statusCode): array
327336
{
328-
$trace = $exception->getTrace();
337+
// Get the first exception.
338+
$firstException = $exception;
339+
340+
while ($prevException = $firstException->getPrevious()) {
341+
$firstException = $prevException;
342+
}
343+
344+
$trace = $firstException->getTrace();
329345

330346
if ($this->config->sensitiveDataInTrace !== []) {
331347
$trace = $this->maskSensitiveData($trace, $this->config->sensitiveDataInTrace);

user_guide_src/source/installation/upgrade_444.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ Please refer to the upgrade instructions corresponding to your installation meth
1616
Mandatory File Changes
1717
**********************
1818

19+
Error Files
20+
===========
21+
22+
Update the following files to show correct error messages:
23+
24+
- app/Views/errors/cli/error_exception.php
25+
- app/Views/errors/html/error_exception.php
26+
1927
****************
2028
Breaking Changes
2129
****************

0 commit comments

Comments
 (0)