Skip to content

Commit 2828cbf

Browse files
authored
Merge pull request #5890 from maxm86545/pdoexception
PDOException&DriverException
2 parents 8ec4dd6 + ad53af9 commit 2828cbf

File tree

3 files changed

+52
-3
lines changed

3 files changed

+52
-3
lines changed

src/Driver/PDO/Connection.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Doctrine\DBAL\Driver\PDO;
44

5+
use Doctrine\DBAL\Driver\PDO\PDOException as DriverPDOException;
56
use Doctrine\DBAL\Driver\Result as ResultInterface;
67
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
78
use Doctrine\DBAL\Driver\Statement as StatementInterface;
@@ -107,17 +108,29 @@ public function lastInsertId($name = null)
107108

108109
public function beginTransaction(): bool
109110
{
110-
return $this->connection->beginTransaction();
111+
try {
112+
return $this->connection->beginTransaction();
113+
} catch (PDOException $exception) {
114+
throw DriverPDOException::new($exception);
115+
}
111116
}
112117

113118
public function commit(): bool
114119
{
115-
return $this->connection->commit();
120+
try {
121+
return $this->connection->commit();
122+
} catch (PDOException $exception) {
123+
throw DriverPDOException::new($exception);
124+
}
116125
}
117126

118127
public function rollBack(): bool
119128
{
120-
return $this->connection->rollBack();
129+
try {
130+
return $this->connection->rollBack();
131+
} catch (PDOException $exception) {
132+
throw DriverPDOException::new($exception);
133+
}
121134
}
122135

123136
public function getNativeConnection(): PDO

src/Driver/PDO/PDOException.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Driver\PDO;
6+
7+
use Doctrine\DBAL\Driver\Exception as DriverException;
8+
9+
/**
10+
* @internal
11+
*
12+
* @psalm-immutable
13+
*/
14+
final class PDOException extends \PDOException implements DriverException
15+
{
16+
private ?string $sqlState = null;
17+
18+
public static function new(\PDOException $previous): self
19+
{
20+
$exception = new self($previous->message, 0, $previous);
21+
22+
$exception->errorInfo = $previous->errorInfo;
23+
$exception->code = $previous->code;
24+
$exception->sqlState = $previous->errorInfo[0] ?? null;
25+
26+
return $exception;
27+
}
28+
29+
public function getSQLState(): ?string
30+
{
31+
return $this->sqlState;
32+
}
33+
}

tests/Functional/TransactionTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Doctrine\DBAL\Tests\Functional;
44

5+
use Doctrine\DBAL\Driver\Exception as DriverException;
56
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
67
use Doctrine\DBAL\Tests\FunctionalTestCase;
78
use PDOException;
@@ -30,6 +31,8 @@ public function testCommitFalse(): void
3031
try {
3132
self::assertFalse(@$this->connection->commit()); // we will ignore `MySQL server has gone away` warnings
3233
} catch (PDOException $e) {
34+
self::assertInstanceOf(DriverException::class, $e);
35+
3336
/* For PDO, we are using ERRMODE EXCEPTION, so this catch should be
3437
* necessary as the equivalent of the error control operator above.
3538
* This seems to be the case only since PHP 8 */

0 commit comments

Comments
 (0)