Skip to content

Commit

Permalink
fix: bug where ExceptionHandler displays incorrect Exception classname
Browse files Browse the repository at this point in the history
It showed the first Exception, but devs must catch the thrown Exception.
  • Loading branch information
kenjis committed Nov 21, 2023
1 parent 45ce53b commit 74e236b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 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 = $trace;

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
14 changes: 8 additions & 6 deletions system/Debug/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,6 @@ public function exceptionHandler(Throwable $exception)
$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 +320,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

0 comments on commit 74e236b

Please sign in to comment.