Driver::connect() should throw only driver-level exceptions

parent 4b2fed55
...@@ -10,6 +10,7 @@ use Doctrine\DBAL\Cache\CacheException; ...@@ -10,6 +10,7 @@ use Doctrine\DBAL\Cache\CacheException;
use Doctrine\DBAL\Cache\CachingResult; use Doctrine\DBAL\Cache\CachingResult;
use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Driver\Connection as DriverConnection; use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Driver\PingableConnection; use Doctrine\DBAL\Driver\PingableConnection;
use Doctrine\DBAL\Driver\Result as DriverResult; use Doctrine\DBAL\Driver\Result as DriverResult;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
...@@ -342,6 +343,8 @@ class Connection implements DriverConnection ...@@ -342,6 +343,8 @@ class Connection implements DriverConnection
* *
* @return bool TRUE if the connection was successfully established, FALSE if * @return bool TRUE if the connection was successfully established, FALSE if
* the connection is already open. * the connection is already open.
*
* @throws DBALException
*/ */
public function connect() public function connect()
{ {
...@@ -353,7 +356,12 @@ class Connection implements DriverConnection ...@@ -353,7 +356,12 @@ class Connection implements DriverConnection
$user = $this->params['user'] ?? null; $user = $this->params['user'] ?? null;
$password = $this->params['password'] ?? null; $password = $this->params['password'] ?? null;
try {
$this->_conn = $this->_driver->connect($this->params, $user, $password, $driverOptions); $this->_conn = $this->_driver->connect($this->params, $user, $password, $driverOptions);
} catch (DriverException $e) {
throw DBALException::driverException($this->_driver, $e);
}
$this->isConnected = true; $this->isConnected = true;
$this->transactionNestingLevel = 0; $this->transactionNestingLevel = 0;
...@@ -420,7 +428,7 @@ class Connection implements DriverConnection ...@@ -420,7 +428,7 @@ class Connection implements DriverConnection
if ($this->_conn === null) { if ($this->_conn === null) {
try { try {
$this->connect(); $this->connect();
} catch (Throwable $originalException) { } catch (DBALException $originalException) {
if (! isset($this->params['dbname'])) { if (! isset($this->params['dbname'])) {
throw $originalException; throw $originalException;
} }
...@@ -432,7 +440,7 @@ class Connection implements DriverConnection ...@@ -432,7 +440,7 @@ class Connection implements DriverConnection
try { try {
$this->connect(); $this->connect();
} catch (Throwable $fallbackException) { } catch (DBALException $fallbackException) {
// Either the platform does not support database-less connections // Either the platform does not support database-less connections
// or something else went wrong. // or something else went wrong.
// Reset connection parameters and rethrow the original exception. // Reset connection parameters and rethrow the original exception.
......
...@@ -5,8 +5,10 @@ namespace Doctrine\DBAL\Connections; ...@@ -5,8 +5,10 @@ namespace Doctrine\DBAL\Connections;
use Doctrine\Common\EventManager; use Doctrine\Common\EventManager;
use Doctrine\DBAL\Configuration; use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection; use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\Connection as DriverConnection; use Doctrine\DBAL\Driver\Connection as DriverConnection;
use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Driver\Result; use Doctrine\DBAL\Driver\Result;
use Doctrine\DBAL\Driver\Statement; use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Event\ConnectionEventArgs; use Doctrine\DBAL\Event\ConnectionEventArgs;
...@@ -230,7 +232,11 @@ class PrimaryReadReplicaConnection extends Connection ...@@ -230,7 +232,11 @@ class PrimaryReadReplicaConnection extends Connection
$user = $connectionParams['user'] ?? null; $user = $connectionParams['user'] ?? null;
$password = $connectionParams['password'] ?? null; $password = $connectionParams['password'] ?? null;
try {
return $this->_driver->connect($connectionParams, $user, $password, $driverOptions); return $this->_driver->connect($connectionParams, $user, $password, $driverOptions);
} catch (DriverException $e) {
throw DBALException::driverException($this->_driver, $e);
}
} }
/** /**
......
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Doctrine\DBAL; namespace Doctrine\DBAL;
use Doctrine\DBAL\Driver\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager; use Doctrine\DBAL\Schema\AbstractSchemaManager;
...@@ -22,6 +23,8 @@ interface Driver ...@@ -22,6 +23,8 @@ interface Driver
* @param mixed[] $driverOptions The driver options to use when connecting. * @param mixed[] $driverOptions The driver options to use when connecting.
* *
* @return \Doctrine\DBAL\Driver\Connection The database connection. * @return \Doctrine\DBAL\Driver\Connection The database connection.
*
* @throws DriverException
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []); public function connect(array $params, $username = null, $password = null, array $driverOptions = []);
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace Doctrine\DBAL\Driver\Mysqli; namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\AbstractMySQLDriver;
class Driver extends AbstractMySQLDriver class Driver extends AbstractMySQLDriver
...@@ -12,11 +11,7 @@ class Driver extends AbstractMySQLDriver ...@@ -12,11 +11,7 @@ class Driver extends AbstractMySQLDriver
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{ {
try {
return new MysqliConnection($params, (string) $username, (string) $password, $driverOptions); return new MysqliConnection($params, (string) $username, (string) $password, $driverOptions);
} catch (MysqliException $e) {
throw DBALException::driverException($this, $e);
}
} }
/** /**
......
...@@ -2,7 +2,6 @@ ...@@ -2,7 +2,6 @@
namespace Doctrine\DBAL\Driver\OCI8; namespace Doctrine\DBAL\Driver\OCI8;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractOracleDriver; use Doctrine\DBAL\Driver\AbstractOracleDriver;
use const OCI_NO_AUTO_COMMIT; use const OCI_NO_AUTO_COMMIT;
...@@ -17,7 +16,6 @@ class Driver extends AbstractOracleDriver ...@@ -17,7 +16,6 @@ class Driver extends AbstractOracleDriver
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{ {
try {
return new OCI8Connection( return new OCI8Connection(
(string) $username, (string) $username,
(string) $password, (string) $password,
...@@ -26,9 +24,6 @@ class Driver extends AbstractOracleDriver ...@@ -26,9 +24,6 @@ class Driver extends AbstractOracleDriver
$params['sessionMode'] ?? OCI_NO_AUTO_COMMIT, $params['sessionMode'] ?? OCI_NO_AUTO_COMMIT,
$params['persistent'] ?? false $params['persistent'] ?? false
); );
} catch (OCI8Exception $e) {
throw DBALException::driverException($this, $e);
}
} }
/** /**
......
...@@ -2,11 +2,9 @@ ...@@ -2,11 +2,9 @@
namespace Doctrine\DBAL\Driver\PDOMySql; namespace Doctrine\DBAL\Driver\PDOMySql;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOConnection;
use PDO; use PDO;
use PDOException;
/** /**
* PDO MySql driver. * PDO MySql driver.
...@@ -22,18 +20,12 @@ class Driver extends AbstractMySQLDriver ...@@ -22,18 +20,12 @@ class Driver extends AbstractMySQLDriver
$driverOptions[PDO::ATTR_PERSISTENT] = true; $driverOptions[PDO::ATTR_PERSISTENT] = true;
} }
try { return new PDOConnection(
$conn = new PDOConnection(
$this->constructPdoDsn($params), $this->constructPdoDsn($params),
$username, $username,
$password, $password,
$driverOptions $driverOptions
); );
} catch (PDOException $e) {
throw DBALException::driverException($this, $e);
}
return $conn;
} }
/** /**
......
...@@ -2,11 +2,9 @@ ...@@ -2,11 +2,9 @@
namespace Doctrine\DBAL\Driver\PDOOracle; namespace Doctrine\DBAL\Driver\PDOOracle;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractOracleDriver; use Doctrine\DBAL\Driver\AbstractOracleDriver;
use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOConnection;
use PDO; use PDO;
use PDOException;
/** /**
* PDO Oracle driver. * PDO Oracle driver.
...@@ -27,16 +25,12 @@ class Driver extends AbstractOracleDriver ...@@ -27,16 +25,12 @@ class Driver extends AbstractOracleDriver
$driverOptions[PDO::ATTR_PERSISTENT] = true; $driverOptions[PDO::ATTR_PERSISTENT] = true;
} }
try {
return new PDOConnection( return new PDOConnection(
$this->constructPdoDsn($params), $this->constructPdoDsn($params),
$username, $username,
$password, $password,
$driverOptions $driverOptions
); );
} catch (PDOException $e) {
throw DBALException::driverException($this, $e);
}
} }
/** /**
......
...@@ -2,11 +2,9 @@ ...@@ -2,11 +2,9 @@
namespace Doctrine\DBAL\Driver\PDOPgSql; namespace Doctrine\DBAL\Driver\PDOPgSql;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver; use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver;
use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOConnection;
use PDO; use PDO;
use PDOException;
use function defined; use function defined;
...@@ -24,7 +22,6 @@ class Driver extends AbstractPostgreSQLDriver ...@@ -24,7 +22,6 @@ class Driver extends AbstractPostgreSQLDriver
$driverOptions[PDO::ATTR_PERSISTENT] = true; $driverOptions[PDO::ATTR_PERSISTENT] = true;
} }
try {
$connection = new PDOConnection( $connection = new PDOConnection(
$this->_constructPdoDsn($params), $this->_constructPdoDsn($params),
$username, $username,
...@@ -50,9 +47,6 @@ class Driver extends AbstractPostgreSQLDriver ...@@ -50,9 +47,6 @@ class Driver extends AbstractPostgreSQLDriver
} }
return $connection; return $connection;
} catch (PDOException $e) {
throw DBALException::driverException($this, $e);
}
} }
/** /**
......
...@@ -2,11 +2,9 @@ ...@@ -2,11 +2,9 @@
namespace Doctrine\DBAL\Driver\PDOSqlite; namespace Doctrine\DBAL\Driver\PDOSqlite;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractSQLiteDriver; use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
use Doctrine\DBAL\Driver\PDOConnection; use Doctrine\DBAL\Driver\PDOConnection;
use Doctrine\DBAL\Platforms\SqlitePlatform; use Doctrine\DBAL\Platforms\SqlitePlatform;
use PDOException;
use function array_merge; use function array_merge;
...@@ -35,16 +33,12 @@ class Driver extends AbstractSQLiteDriver ...@@ -35,16 +33,12 @@ class Driver extends AbstractSQLiteDriver
unset($driverOptions['userDefinedFunctions']); unset($driverOptions['userDefinedFunctions']);
} }
try {
$connection = new PDOConnection( $connection = new PDOConnection(
$this->_constructPdoDsn($params), $this->_constructPdoDsn($params),
$username, $username,
$password, $password,
$driverOptions $driverOptions
); );
} catch (PDOException $ex) {
throw DBALException::driverException($this, $ex);
}
$pdo = $connection->getWrappedConnection(); $pdo = $connection->getWrappedConnection();
......
...@@ -21,7 +21,6 @@ class Driver extends AbstractSQLAnywhereDriver ...@@ -21,7 +21,6 @@ class Driver extends AbstractSQLAnywhereDriver
*/ */
public function connect(array $params, $username = null, $password = null, array $driverOptions = []) public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
{ {
try {
return new SQLAnywhereConnection( return new SQLAnywhereConnection(
$this->buildDsn( $this->buildDsn(
$params['host'] ?? null, $params['host'] ?? null,
...@@ -34,9 +33,6 @@ class Driver extends AbstractSQLAnywhereDriver ...@@ -34,9 +33,6 @@ class Driver extends AbstractSQLAnywhereDriver
), ),
$params['persistent'] ?? false $params['persistent'] ?? false
); );
} catch (SQLAnywhereException $e) {
throw DBALException::driverException($this, $e);
}
} }
/** /**
......
...@@ -20,7 +20,6 @@ use Doctrine\DBAL\Logging\DebugStack; ...@@ -20,7 +20,6 @@ use Doctrine\DBAL\Logging\DebugStack;
use Doctrine\DBAL\ParameterType; use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\VersionAwarePlatformDriver; use Doctrine\DBAL\VersionAwarePlatformDriver;
use Exception;
use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use stdClass; use stdClass;
...@@ -788,8 +787,8 @@ class ConnectionTest extends TestCase ...@@ -788,8 +787,8 @@ class ConnectionTest extends TestCase
$driverMock = $this->createMock(VersionAwarePlatformDriver::class); $driverMock = $this->createMock(VersionAwarePlatformDriver::class);
$connection = new Connection(['dbname' => 'foo'], $driverMock); $connection = new Connection(['dbname' => 'foo'], $driverMock);
$originalException = new Exception('Original exception'); $originalException = new DBALException('Original exception');
$fallbackException = new Exception('Fallback exception'); $fallbackException = new DBALException('Fallback exception');
$driverMock->expects(self::at(0)) $driverMock->expects(self::at(0))
->method('connect') ->method('connect')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment