Skip to content

Commit eb21b5c

Browse files
authored
[10.x] Add connection name to QueryException (#43190)
1 parent d52ef24 commit eb21b5c

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/Illuminate/Database/Connection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ protected function runQueryCallback($query, $bindings, Closure $callback)
757757
// lot more helpful to the developer instead of just the database's errors.
758758
catch (Exception $e) {
759759
throw new QueryException(
760-
$query, $this->prepareBindings($bindings), $e
760+
$this->getName(), $query, $this->prepareBindings($bindings), $e
761761
);
762762
}
763763
}

src/Illuminate/Database/QueryException.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@
88

99
class QueryException extends PDOException
1010
{
11+
/**
12+
* The database connection name.
13+
*
14+
* @var string
15+
*/
16+
protected $connectionName;
17+
1118
/**
1219
* The SQL for the query.
1320
*
@@ -25,19 +32,21 @@ class QueryException extends PDOException
2532
/**
2633
* Create a new query exception instance.
2734
*
35+
* @param string $connectionName
2836
* @param string $sql
2937
* @param array $bindings
3038
* @param \Throwable $previous
3139
* @return void
3240
*/
33-
public function __construct($sql, array $bindings, Throwable $previous)
41+
public function __construct($connectionName, $sql, array $bindings, Throwable $previous)
3442
{
3543
parent::__construct('', 0, $previous);
3644

45+
$this->connectionName = $connectionName;
3746
$this->sql = $sql;
3847
$this->bindings = $bindings;
3948
$this->code = $previous->getCode();
40-
$this->message = $this->formatMessage($sql, $bindings, $previous);
49+
$this->message = $this->formatMessage($connectionName, $sql, $bindings, $previous);
4150

4251
if ($previous instanceof PDOException) {
4352
$this->errorInfo = $previous->errorInfo;
@@ -47,14 +56,25 @@ public function __construct($sql, array $bindings, Throwable $previous)
4756
/**
4857
* Format the SQL error message.
4958
*
59+
* @param string $connectionName
5060
* @param string $sql
5161
* @param array $bindings
5262
* @param \Throwable $previous
5363
* @return string
5464
*/
55-
protected function formatMessage($sql, $bindings, Throwable $previous)
65+
protected function formatMessage($connectionName, $sql, $bindings, Throwable $previous)
66+
{
67+
return $previous->getMessage().' (Connection: '.$connectionName.', SQL: '.Str::replaceArray('?', $bindings, $sql).')';
68+
}
69+
70+
/**
71+
* Get the connection name for the query.
72+
*
73+
* @return string
74+
*/
75+
public function getConnectionName()
5676
{
57-
return $previous->getMessage().' (SQL: '.Str::replaceArray('?', $bindings, $sql).')';
77+
return $this->connectionName;
5878
}
5979

6080
/**

tests/Database/DatabaseConnectionTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,15 @@ public function testTransactionRetriesOnSerializationFailure()
297297
public function testTransactionMethodRetriesOnDeadlock()
298298
{
299299
$this->expectException(QueryException::class);
300-
$this->expectExceptionMessage('Deadlock found when trying to get lock (SQL: )');
300+
$this->expectExceptionMessage('Deadlock found when trying to get lock (Connection: conn, SQL: )');
301301

302302
$pdo = $this->getMockBuilder(DatabaseConnectionTestMockPDO::class)->onlyMethods(['beginTransaction', 'commit', 'rollBack'])->getMock();
303303
$mock = $this->getMockConnection([], $pdo);
304304
$pdo->expects($this->exactly(3))->method('beginTransaction');
305305
$pdo->expects($this->exactly(3))->method('rollBack');
306306
$pdo->expects($this->never())->method('commit');
307307
$mock->transaction(function () {
308-
throw new QueryException('', [], new Exception('Deadlock found when trying to get lock'));
308+
throw new QueryException('conn', '', [], new Exception('Deadlock found when trying to get lock'));
309309
}, 3);
310310
}
311311

@@ -328,7 +328,7 @@ public function testTransactionMethodRollsbackAndThrows()
328328
public function testOnLostConnectionPDOIsNotSwappedWithinATransaction()
329329
{
330330
$this->expectException(QueryException::class);
331-
$this->expectExceptionMessage('server has gone away (SQL: foo)');
331+
$this->expectExceptionMessage('server has gone away (Connection: , SQL: foo)');
332332

333333
$pdo = m::mock(PDO::class);
334334
$pdo->shouldReceive('beginTransaction')->once();
@@ -374,14 +374,14 @@ public function testRunMethodRetriesOnFailure()
374374
$mock->expects($this->once())->method('tryAgainIfCausedByLostConnection');
375375

376376
$method->invokeArgs($mock, ['', [], function () {
377-
throw new QueryException('', [], new Exception);
377+
throw new QueryException('', '', [], new Exception);
378378
}]);
379379
}
380380

381381
public function testRunMethodNeverRetriesIfWithinTransaction()
382382
{
383383
$this->expectException(QueryException::class);
384-
$this->expectExceptionMessage('(SQL: ) (SQL: )');
384+
$this->expectExceptionMessage('(Connection: conn, SQL: ) (Connection: , SQL: )');
385385

386386
$method = (new ReflectionClass(Connection::class))->getMethod('run');
387387
$method->setAccessible(true);
@@ -393,7 +393,7 @@ public function testRunMethodNeverRetriesIfWithinTransaction()
393393
$mock->beginTransaction();
394394

395395
$method->invokeArgs($mock, ['', [], function () {
396-
throw new QueryException('', [], new Exception);
396+
throw new QueryException('conn', '', [], new Exception);
397397
}]);
398398
}
399399

0 commit comments

Comments
 (0)