Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: uniform rendering of stack trace from failed DB operations #9364

Draft
wants to merge 2 commits into
base: 4.6
Choose a base branch
from
Draft
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
51 changes: 51 additions & 0 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -913,6 +913,57 @@ function remove_invisible_characters(string $str, bool $urlEncoded = true): stri
}
}

if (! function_exists('render_backtrace')) {
/**
* Renders a backtrace in a nice string format.
*
* @param list<array{
* file?: string,
* line?: int,
* class?: string,
* type?: string,
* function: string,
* args?: list<mixed>
* }> $backtrace
*/
function render_backtrace(array $backtrace): string
{
$backtraces = [];

foreach ($backtrace as $index => $trace) {
$frame = $trace + ['file' => '[internal function]', 'line' => 0, '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);

$args = implode(', ', array_map(static fn ($value): string => match (true) {
is_object($value) => sprintf('Object(%s)', $value::class),
is_array($value) => $value !== [] ? '[...]' : '[]',
$value === null => 'null',
is_resource($value) => sprintf('resource (%s)', get_resource_type($value)),
default => 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);
}
}

if (! function_exists('request')) {
/**
* Returns the shared Request.
Expand Down
7 changes: 6 additions & 1 deletion system/Database/MySQLi/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,12 @@ protected function execute(string $sql)
try {
return $this->connID->query($this->prepQuery($sql), $this->resultMode);
} catch (mysqli_sql_exception $e) {
log_message('error', (string) $e);
log_message('error', "{message}\nin {exFile} on line {exLine}.\n{trace}", [
'message' => $e->getMessage(),
'exFile' => clean_path($e->getFile()),
'exLine' => $e->getLine(),
'trace' => render_backtrace($e->getTrace()),
]);

if ($this->DBDebug) {
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
Expand Down
9 changes: 8 additions & 1 deletion system/Database/OCI8/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,14 @@ protected function execute(string $sql)

return $result;
} catch (ErrorException $e) {
log_message('error', (string) $e);
$trace = array_slice($e->getTrace(), 2); // remove call to error handler

log_message('error', "{message}\nin {exFile} on line {exLine}.\n{trace}", [
'message' => $e->getMessage(),
'exFile' => clean_path($e->getFile()),
'exLine' => $e->getLine(),
'trace' => render_backtrace($trace),
]);

if ($this->DBDebug) {
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
Expand Down
9 changes: 8 additions & 1 deletion system/Database/Postgre/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,14 @@ protected function execute(string $sql)
try {
return pg_query($this->connID, $sql);
} catch (ErrorException $e) {
log_message('error', (string) $e);
$trace = array_slice($e->getTrace(), 2); // remove the call to error handler

log_message('error', "{message}\nin {exFile} on line {exLine}.\n{trace}", [
'message' => $e->getMessage(),
'exFile' => clean_path($e->getFile()),
'exLine' => $e->getLine(),
'trace' => render_backtrace($trace),
]);

if ($this->DBDebug) {
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
Expand Down
26 changes: 18 additions & 8 deletions system/Database/SQLSRV/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,12 @@ public function getAllErrorMessages(): string
$errors = [];

foreach (sqlsrv_errors() as $error) {
$errors[] = $error['message']
. ' SQLSTATE: ' . $error['SQLSTATE'] . ', code: ' . $error['code'];
$errors[] = sprintf(
'%s SQLSTATE: %s, code: %s',
$error['message'],
$error['SQLSTATE'],
$error['code']
);
}

return implode("\n", $errors);
Expand Down Expand Up @@ -488,17 +492,23 @@ public function setDatabase(?string $databaseName = null)
*/
protected function execute(string $sql)
{
$stmt = ($this->scrollable === false || $this->isWriteType($sql)) ?
sqlsrv_query($this->connID, $sql) :
sqlsrv_query($this->connID, $sql, [], ['Scrollable' => $this->scrollable]);
$stmt = ($this->scrollable === false || $this->isWriteType($sql))
? sqlsrv_query($this->connID, $sql)
: sqlsrv_query($this->connID, $sql, [], ['Scrollable' => $this->scrollable]);

if ($stmt === false) {
$error = $this->error();
$trace = debug_backtrace();
$first = array_shift($trace);

log_message('error', $error['message']);
log_message('error', "{message}\nin {exFile} on line {exLine}.\n{trace}", [
'message' => $this->getAllErrorMessages(),
'exFile' => clean_path($first['file']),
'exLine' => $first['line'],
'trace' => render_backtrace($trace),
]);

if ($this->DBDebug) {
throw new DatabaseException($error['message']);
throw new DatabaseException($this->getAllErrorMessages());
}
}

Expand Down
7 changes: 6 additions & 1 deletion system/Database/SQLite3/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,12 @@ protected function execute(string $sql)
? $this->connID->exec($sql)
: $this->connID->query($sql);
} catch (Exception $e) {
log_message('error', (string) $e);
log_message('error', "{message}\nin {exFile} on line {exLine}.\n{trace}", [
'message' => $e->getMessage(),
'exFile' => clean_path($e->getFile()),
'exLine' => $e->getLine(),
'trace' => render_backtrace($e->getTrace()),
]);

if ($this->DBDebug) {
throw new DatabaseException($e->getMessage(), $e->getCode(), $e);
Expand Down
43 changes: 3 additions & 40 deletions system/Debug/Exceptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function exceptionHandler(Throwable $exception)
'routeInfo' => $routeInfo,
'exFile' => clean_path($exception->getFile()), // {file} refers to THIS file
'exLine' => $exception->getLine(), // {line} refers to THIS line
'trace' => self::renderBacktrace($exception->getTrace()),
'trace' => render_backtrace($exception->getTrace()),
]);

// Get the first exception.
Expand All @@ -149,7 +149,7 @@ public function exceptionHandler(Throwable $exception)
'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()),
'trace' => render_backtrace($prevException->getTrace()),
]);
}
}
Expand Down Expand Up @@ -527,7 +527,7 @@ private function handleDeprecationError(string $message, ?string $file = null, ?
'message' => $message,
'errFile' => clean_path($file ?? ''),
'errLine' => $line ?? 0,
'trace' => self::renderBacktrace($trace),
'trace' => render_backtrace($trace),
]
);

Expand Down Expand Up @@ -646,41 +646,4 @@ 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);

$args = implode(', ', array_map(static fn ($value): string => match (true) {
is_object($value) => sprintf('Object(%s)', $value::class),
is_array($value) => $value !== [] ? '[...]' : '[]',
$value === null => 'null',
is_resource($value) => sprintf('resource (%s)', get_resource_type($value)),
default => 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);
}
}
10 changes: 10 additions & 0 deletions tests/system/CommonFunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -805,4 +805,14 @@ public function testIsWindowsUsingMock(): void
$this->assertSame(str_contains(php_uname(), 'Windows'), is_windows());
$this->assertSame(defined('PHP_WINDOWS_VERSION_MAJOR'), is_windows());
}

public function testRenderBacktrace(): void
{
$trace = (new RuntimeException('Test exception'))->getTrace();
$renders = explode("\n", render_backtrace($trace));

foreach ($renders as $render) {
$this->assertMatchesRegularExpression('/^\s*\d* .+(?:\(\d+\))?: \S+(?:(?:\->|::)\S+)?\(.*\)$/', $render);
}
}
}
62 changes: 62 additions & 0 deletions tests/system/Database/Live/ExecuteLogMessageFormatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

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

namespace CodeIgniter\Database\Live;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\TestLogger;
use Config\Database;
use PHPUnit\Framework\Attributes\Group;
use PHPUnit\Framework\TestCase;
use ReflectionProperty;

/**
* @internal
*/
#[Group('DatabaseLive')]
final class ExecuteLogMessageFormatTest extends TestCase
{
public function testLogMessageWhenExecuteFailsShowFullStructuredBacktrace(): void
{
$db = Database::connect((new Database)->tests, false);
(new ReflectionProperty($db, 'DBDebug'))->setValue($db, false);

$sql = 'SELECT * FROM some_table WHERE id = ? AND status = ? AND author = ?';
$db->query($sql, [3, 'live', 'Rick']);

$message = match ($db->DBDriver) {
'MySQLi' => 'Table \'test.some_table\' doesn\'t exist',
'Postgre' => 'pg_query(): Query failed: ERROR: relation "some_table" does not exist',
'SQLite3' => 'Unable to prepare statement: no such table: some_table',
'OCI8' => 'oci_execute(): ORA-00942: table or view does not exist',
'SQLSRV' => '[Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Invalid object name \'some_table\'. SQLSTATE: 42S02, code: 208',
default => 'Unknown DB error',
};
$messageFromLogs = explode("\n", (new ReflectionProperty(TestLogger::class, 'op_logs'))->getValue()[0]['message']);

$this->assertSame($message, array_shift($messageFromLogs));

if ($db->DBDriver === 'Postgre') {
$messageFromLogs = array_slice($messageFromLogs, 2);
} elseif ($db->DBDriver === 'OCI8') {
$messageFromLogs = array_slice($messageFromLogs, 1);
}

$this->assertMatchesRegularExpression('/^in \S+ on line \d+\.$/', array_shift($messageFromLogs));

foreach ($messageFromLogs as $line) {
$this->assertMatchesRegularExpression('/^\s*\d* .+(?:\(\d+\))?: \S+(?:(?:\->|::)\S+)?\(.*\)$/', $line);
}
}
}
16 changes: 0 additions & 16 deletions tests/system/Debug/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,22 +128,6 @@ public function testDetermineCodes(): void
$this->assertSame([500, EXIT_DATABASE], $determineCodes(new DatabaseException('This.')));
}

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
);
}
}

public function testMaskSensitiveData(): void
{
$maskSensitiveData = $this->getPrivateMethodInvoker($this->exception, 'maskSensitiveData');
Expand Down
7 changes: 1 addition & 6 deletions utils/phpstan-baseline/missingType.iterableValue.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# total 1672 errors
# total 1670 errors

parameters:
ignoreErrors:
Expand Down Expand Up @@ -2417,11 +2417,6 @@ parameters:
count: 1
path: ../../system/Debug/Exceptions.php

-
message: '#^Method CodeIgniter\\Debug\\Exceptions\:\:renderBacktrace\(\) has parameter \$backtrace with no value type specified in iterable type array\.$#'
count: 1
path: ../../system/Debug/Exceptions.php

-
message: '#^Method CodeIgniter\\Debug\\Exceptions\:\:respond\(\) has parameter \$data with no value type specified in iterable type array\.$#'
count: 1
Expand Down
Loading