Skip to content

Commit

Permalink
Merge pull request #8239 from kenjis/fix-ExceptionHandler-exception-d…
Browse files Browse the repository at this point in the history
…isplay

fix: ExceptionHandler displays incorrect Exception classname
  • Loading branch information
kenjis authored Nov 26, 2023
2 parents 99c1a4b + 886aa89 commit 8657525
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 12 deletions.
17 changes: 13 additions & 4 deletions app/Views/errors/cli/error_exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,26 @@
use CodeIgniter\CLI\CLI;

// The main Exception
CLI::newLine();
CLI::write('[' . get_class($exception) . ']', 'light_gray', 'red');
CLI::newLine();
CLI::write($message);
CLI::newLine();
CLI::write('at ' . CLI::color(clean_path($exception->getFile()) . ':' . $exception->getLine(), 'green'));
CLI::newLine();

$last = $exception;

while ($prevException = $last->getPrevious()) {
$last = $prevException;

CLI::write(' Caused by:');
CLI::write(' [' . get_class($prevException) . ']', 'red');
CLI::write(' ' . $prevException->getMessage());
CLI::write(' at ' . CLI::color(clean_path($prevException->getFile()) . ':' . $prevException->getLine(), 'green'));
CLI::newLine();
}

// The backtrace
if (defined('SHOW_DEBUG_BACKTRACE') && SHOW_DEBUG_BACKTRACE) {
$backtraces = $exception->getTrace();
$backtraces = $last->getTrace();

if ($backtraces) {
CLI::write('Backtrace:', 'green');
Expand Down
23 changes: 23 additions & 0 deletions app/Views/errors/html/error_exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@
<?php endif; ?>
</div>

<div class="container">
<?php
$last = $exception;

while ($prevException = $last->getPrevious()) {
$last = $prevException;
?>

<pre>
Caused by:
<?= esc(get_class($prevException)), esc($prevException->getCode() ? ' #' . $prevException->getCode() : '') ?>

<?= nl2br(esc($prevException->getMessage())) ?>
<a href="https://www.duckduckgo.com/?q=<?= urlencode(get_class($prevException) . ' ' . preg_replace('#\'.*\'|".*"#Us', '', $prevException->getMessage())) ?>"
rel="noreferrer" target="_blank">search &rarr;</a>
<?= esc(clean_path($prevException->getFile()) . ':' . $prevException->getLine()) ?>
</pre>

<?php
}
?>
</div>

<?php if (defined('SHOW_DEBUG_BACKTRACE') && SHOW_DEBUG_BACKTRACE) : ?>
<div class="container">

Expand Down
9 changes: 8 additions & 1 deletion system/Debug/BaseExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,14 @@ abstract public function handle(
*/
protected function collectVars(Throwable $exception, int $statusCode): array
{
$trace = $exception->getTrace();
// Get the first exception.
$firstException = $exception;

while ($prevException = $firstException->getPrevious()) {
$firstException = $prevException;
}

$trace = $firstException->getTrace();

if ($this->config->sensitiveDataInTrace !== []) {
$trace = $this->maskSensitiveData($trace, $this->config->sensitiveDataInTrace);
Expand Down
30 changes: 23 additions & 7 deletions system/Debug/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,31 @@ public function exceptionHandler(Throwable $exception)
[$statusCode, $exitCode] = $this->determineCodes($exception);

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

// Get the first exception.
$last = $exception;

while ($prevException = $last->getPrevious()) {
$last = $prevException;

log_message('critical', '[Caused by] ' . get_class($prevException) . ": {message}\nin {exFile} on line {exLine}.\n{trace}", [
'message' => $prevException->getMessage(),
'exFile' => clean_path($prevException->getFile()), // {file} refers to THIS file
'exLine' => $prevException->getLine(), // {line} refers to THIS line
'trace' => self::renderBacktrace($prevException->getTrace()),
]);
}
}

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

// Get the first exception.
while ($prevException = $exception->getPrevious()) {
$exception = $prevException;
}

if (method_exists($this->config, 'handler')) {
// Use new ExceptionHandler
$handler = $this->config->handler($statusCode, $exception);
Expand Down Expand Up @@ -325,7 +334,14 @@ protected function render(Throwable $exception, int $statusCode)
*/
protected function collectVars(Throwable $exception, int $statusCode): array
{
$trace = $exception->getTrace();
// Get the first exception.
$firstException = $exception;

while ($prevException = $firstException->getPrevious()) {
$firstException = $prevException;
}

$trace = $firstException->getTrace();

if ($this->config->sensitiveDataInTrace !== []) {
$trace = $this->maskSensitiveData($trace, $this->config->sensitiveDataInTrace);
Expand Down
8 changes: 8 additions & 0 deletions user_guide_src/source/installation/upgrade_444.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ Please refer to the upgrade instructions corresponding to your installation meth
Mandatory File Changes
**********************

Error Files
===========

Update the following files to show correct error messages:

- app/Views/errors/cli/error_exception.php
- app/Views/errors/html/error_exception.php

****************
Breaking Changes
****************
Expand Down

0 comments on commit 8657525

Please sign in to comment.