Skip to content

Commit

Permalink
Merge pull request #5890 from maxm86545/pdoexception
Browse files Browse the repository at this point in the history
PDOException&DriverException
  • Loading branch information
derrabus committed Feb 2, 2023
2 parents 8ec4dd6 + ad53af9 commit 2828cbf
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
19 changes: 16 additions & 3 deletions src/Driver/PDO/Connection.php
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Driver\PDO;

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

public function beginTransaction(): bool
{
return $this->connection->beginTransaction();
try {
return $this->connection->beginTransaction();
} catch (PDOException $exception) {
throw DriverPDOException::new($exception);
}
}

public function commit(): bool
{
return $this->connection->commit();
try {
return $this->connection->commit();
} catch (PDOException $exception) {
throw DriverPDOException::new($exception);
}
}

public function rollBack(): bool
{
return $this->connection->rollBack();
try {
return $this->connection->rollBack();
} catch (PDOException $exception) {
throw DriverPDOException::new($exception);
}
}

public function getNativeConnection(): PDO
Expand Down
33 changes: 33 additions & 0 deletions src/Driver/PDO/PDOException.php
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Driver\PDO;

use Doctrine\DBAL\Driver\Exception as DriverException;

/**
* @internal
*
* @psalm-immutable
*/
final class PDOException extends \PDOException implements DriverException
{
private ?string $sqlState = null;

public static function new(\PDOException $previous): self
{
$exception = new self($previous->message, 0, $previous);

$exception->errorInfo = $previous->errorInfo;
$exception->code = $previous->code;
$exception->sqlState = $previous->errorInfo[0] ?? null;

return $exception;
}

public function getSQLState(): ?string
{
return $this->sqlState;
}
}
3 changes: 3 additions & 0 deletions tests/Functional/TransactionTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Tests\Functional;

use Doctrine\DBAL\Driver\Exception as DriverException;
use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use PDOException;
Expand Down Expand Up @@ -30,6 +31,8 @@ public function testCommitFalse(): void
try {
self::assertFalse(@$this->connection->commit()); // we will ignore `MySQL server has gone away` warnings
} catch (PDOException $e) {
self::assertInstanceOf(DriverException::class, $e);

/* For PDO, we are using ERRMODE EXCEPTION, so this catch should be
* necessary as the equivalent of the error control operator above.
* This seems to be the case only since PHP 8 */
Expand Down

0 comments on commit 2828cbf

Please sign in to comment.