Skip to content

Commit

Permalink
[doctrineGH-3318] Bugfix: Wraps exceptions in DBAL\Exception in Prima…
Browse files Browse the repository at this point in the history
…ryReplicaConnection
  • Loading branch information
beberlei committed Dec 7, 2020
1 parent 373c24d commit 8efe1dd
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
25 changes: 23 additions & 2 deletions lib/Doctrine/DBAL/Connections/PrimaryReadReplicaConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Doctrine\DBAL\Event\ConnectionEventArgs;
use Doctrine\DBAL\Events;
use InvalidArgumentException;
use Throwable;

use function array_rand;
use function assert;
Expand Down Expand Up @@ -89,6 +90,17 @@ class PrimaryReadReplicaConnection extends Connection
*/
protected $keepReplica = false;

/**
* Should query() method wrap exceptions or throw native driver exceptions?
*
* For backwards compatibility reasons this bugfix for issue
* https://github.com/doctrine/dbal/issues/3118
* requires to opt-in in 2.x and 3.x.
*
* @var bool
*/
protected $wrapNativeExceptionsBugfix = false;

/**
* Creates Primary Replica Connection.
*
Expand Down Expand Up @@ -123,7 +135,8 @@ public function __construct(
}
}

$this->keepReplica = (bool) ($params['keepReplica'] ?? false);
$this->keepReplica = (bool) ($params['keepReplica'] ?? false);
$this->wrapNativeExceptionsBugfix = (bool) ($params['optinToWrapNativeExceptionsBugfix'] ?? false);

parent::__construct($params, $driver, $config, $eventManager);
}
Expand Down Expand Up @@ -419,7 +432,15 @@ public function query()
$logger->startQuery($args[0]);
}

$statement = $this->_conn->query(...$args);
try {
$statement = $this->_conn->query(...$args);
} catch (Throwable $e) {
if (! $this->wrapNativeExceptionsBugfix) {
throw $e;
}

$this->handleExceptionDuringQuery($e, $args[0]);
}

$statement->setFetchMode($this->defaultFetchMode);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
namespace Doctrine\Tests\DBAL\Functional;

use Doctrine\DBAL\Connections\PrimaryReadReplicaConnection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\Exception;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Schema\Table;
Expand Down Expand Up @@ -243,4 +245,25 @@ public function testQueryOnReplica(): void

self::assertEquals(1, $data[0]['num']);
}

public function testNoOptinToWrapExceptionsBugfixThrowsNativeException(): void
{
$connection = DriverManager::getConnection($this->createPrimaryReadReplicaConnectionParams());

$this->expectException(Exception::class);

$connection->query('lets force an exception');
}

public function testOptinToWrapExceptionsInteadOfNativeExceptions(): void
{
$params = $this->createPrimaryReadReplicaConnectionParams();
$params['optinToWrapNativeExceptionsBugfix'] = true;

$connection = DriverManager::getConnection($params);

$this->expectException(DBALException::class);

$connection->query('lets force an exception');
}
}

0 comments on commit 8efe1dd

Please sign in to comment.