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

Make error logs more informative #1998

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ protected function executeStatement(PreparedStatementDto $preparedStatementDto)
}
$stmt->execute();
} catch (Throwable $e) {
$this->handleStatementException($e, $sql, $stmt ?: null);
$this->handleStatementException($e, $params, $sql, $stmt ?: null);
}

return $stmt;
Expand All @@ -121,14 +121,15 @@ protected function executeStatement(PreparedStatementDto $preparedStatementDto)
* Logs an exception and adds the complete SQL statement to the exception.
*
* @param \Throwable $e The initial exception.
* @param array<mixed> $params Parameters for the SQL statement which triggered the exception.
* @param string|null $sql The SQL statement which triggered the exception.
* @param \Propel\Runtime\Connection\StatementInterface|\PDOStatement|null $stmt The prepared statement.
*
* @throws \Propel\Runtime\ActiveQuery\QueryExecutor\QueryExecutionException
*
* @return void
*/
protected function handleStatementException(Throwable $e, ?string $sql, $stmt = null): void
protected function handleStatementException(Throwable $e, array $params, ?string $sql, $stmt = null): void
{
$internalMessage = $e->getMessage();
Propel::log($internalMessage, Propel::LOG_ERR);
Expand All @@ -137,10 +138,11 @@ protected function handleStatementException(Throwable $e, ?string $sql, $stmt =
if ($isDebugMode && $stmt instanceof StatementWrapper) {
$sql = $stmt->getExecutedQueryString();
}
$publicMessage = "Unable to execute statement [$sql]";
if ($isDebugMode) {
$publicMessage .= PHP_EOL . "Reason: [$internalMessage]";
$paramMessage = '';
foreach ($params as $parameter) {
$paramMessage .= 'column=`' . $parameter['column'] . '` value=`' . $parameter['value'] . '`, ';
}
$publicMessage = "Unable to execute statement [$sql] with params [$paramMessage]. Reason: [$internalMessage]";

throw new QueryExecutionException($publicMessage, 0, $e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,26 @@ class AbstractQueryExecutorTest extends BookstoreTestBase
*/
public function queryExceptionOutputFormatDataProvider()
{
// [$useDebug, $sqlStatement, $internalErrorMessage, $expectedPublicMessage]
return [
[false, '<SQL>', '<ERROR>', 'Unable to execute statement [<SQL>]'],
[true, '<SQL>', '<ERROR>', "Unable to execute statement [<SQL>]\nReason: [<ERROR>]"],
[
'$useDebug' => false,
'$sqlStatement' => '<SQL>',
'$params' => [
['column' => 'some_column', 'value' => 'text'],
],
'$internalErrorMessage' => '<ERROR>',
'$expectedPublicMessage' => 'Unable to execute statement [<SQL>] with params [column=`some_column` value=`text`, ]. Reason: [<ERROR>]',
],
[
'$useDebug' => true,
'$sqlStatement' => '<SQL>',
'$params' => [
['column' => 'some_column', 'value' => 'text'],
['column' => 'id', 'value' => 1],
],
'$internalErrorMessage' => '<ERROR>',
'$expectedPublicMessage' => 'Unable to execute statement [<SQL>] with params [column=`some_column` value=`text`, column=`id` value=`1`, ]. Reason: [<ERROR>]',
],
];
}

Expand All @@ -39,26 +55,27 @@ public function queryExceptionOutputFormatDataProvider()
*
* @param bool $useDebug
* @param string $sqlStatement
* @param array $params
* @param string $internalErrorMessage
* @param string $expectedPublicMessage
*
* @return void
*/
public function testQueryExceptionOutputFormat($useDebug, $sqlStatement, $internalErrorMessage, $expectedPublicMessage)
public function testQueryExceptionOutputFormat($useDebug, $sqlStatement, $params, $internalErrorMessage, $expectedPublicMessage)
{
$query = BookQuery::create();
$con = new ConnectionWrapper($this->con->getWrappedConnection());
$con->useDebug($useDebug);

$c = new class ($query, $con) extends AbstractQueryExecutor {
public function simulateException($msg, $sql, $con)
public function simulateException($msg, $sql, $params, $con)
{
return $this->handleStatementException(new PropelException($msg), $sql);
return $this->handleStatementException(new PropelException($msg), $params, $sql);
}
};

try {
$c->simulateException($internalErrorMessage, $sqlStatement, $con);
$c->simulateException($internalErrorMessage, $sqlStatement, $params, $con);
$this->fail('Cannot test without exception');
} catch (QueryExecutionException $e) {
$this->assertEquals($expectedPublicMessage, $e->getMessage());
Expand Down
Loading