Skip to content

Make PDO transaction methods throw PDOException&DriverException #5890

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

Merged
merged 1 commit into from
Feb 2, 2023
Merged
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
19 changes: 16 additions & 3 deletions src/Driver/PDO/Connection.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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