Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 56 additions & 2 deletions system/Debug/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ 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', $exception->getMessage() . "\n{trace}", [
'trace' => $exception->getTraceAsString(),
log_message('critical', "{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()),
]);
}

Expand Down Expand Up @@ -434,4 +437,55 @@ public static function highlightFile(string $file, int $lineNumber, int $lines =

return '<pre><code>' . $out . '</code></pre>';
}

private static function renderBacktrace(array $backtrace): string
{
$backtraces = [];

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

if ($frame['file'] !== '[internal function]') {
$frame['file'] = sprintf('%s(%s)', $frame['file'], $frame['line']);
}

unset($frame['line']);
$idx = $index;
$idx = str_pad((string) ++$idx, 2, ' ', STR_PAD_LEFT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you start with 1, not 0?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that's more "natural" way of counting. No offense to zero-based array counting. 😂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$idx = str_pad((string) ++$idx, 2, '#', STR_PAD_LEFT);

This is like the original.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is like the original.

This would fail on idx >= 10. https://3v4l.org/DAqmo

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://3v4l.org/dFGjg

echo '#'.str_pad(++$idx, 3, ' ', STR_PAD_RIGHT).'Some more text', PHP_EOL;


$args = implode(', ', array_map(static function ($value): string {
switch (true) {
case is_object($value):
return sprintf('Object(%s)', get_class($value));

case is_array($value):
return $value !== [] ? '[...]' : '[]';

case $value === null:
return 'null';

case is_resource($value):
return sprintf('resource (%s)', get_resource_type($value));

case is_string($value):
return var_export(clean_path($value), true);

default:
return var_export($value, true);
}
}, $frame['args']));

$backtraces[] = sprintf(
'%s %s: %s%s%s(%s)',
$idx,
clean_path($frame['file']),
$frame['class'],
$frame['type'],
$frame['function'],
$args
);
}

return implode("\n", $backtraces);
}
}
16 changes: 16 additions & 0 deletions tests/system/Debug/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,20 @@ public function testDetermineCodes(): void
$this->assertSame([500, 1], $determineCodes(new RuntimeException('That.', 600)));
$this->assertSame([404, 1], $determineCodes(new RuntimeException('There.', 404)));
}

public function testRenderBacktrace(): void
{
$renderer = self::getPrivateMethodInvoker(Exceptions::class, 'renderBacktrace');
$exception = new RuntimeException('This.');

$renderedBacktrace = $renderer($exception->getTrace());
$renderedBacktrace = explode("\n", $renderedBacktrace);

foreach ($renderedBacktrace as $trace) {
$this->assertMatchesRegularExpression(
'/^\s*\d* .+(?:\(\d+\))?: \S+(?:(?:\->|::)\S+)?\(.*\)$/',
$trace
);
}
}
}
2 changes: 2 additions & 0 deletions user_guide_src/source/changelogs/v4.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Enhancements
- Added new OCI8 driver for database.
- It can access Oracle Database and supports SQL and PL/SQL statements.
- The ``spark routes`` command now shows closure routes, auto routtes, and filters. See :ref:`URI Routing <spark-routes>`.
- Exception information logged through ``log_message()`` has now improved. It now includes the file and line where the exception originated. It also does not truncate the message anymore.
- The log format has also changed. If users are depending on the log format in their apps, the new log format is "<1-based count> <cleaned filepath>(<line>): <class><function><args>"

Changes
*******
Expand Down