From b0a8ab0bed0a91e142e74144b00f7ede63f38b96 Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Thu, 4 Apr 2019 18:34:27 -0700 Subject: [PATCH] Reworking driver exceptions --- UPGRADE.md | 8 ++ .../DBAL/Driver/AbstractDriverException.php | 32 ++---- .../DBAL/Driver/AbstractMySQLDriver.php | 106 +++++++++--------- .../DBAL/Driver/AbstractOracleDriver.php | 32 +++--- .../DBAL/Driver/AbstractPostgreSQLDriver.php | 15 +-- .../DBAL/Driver/AbstractSQLAnywhereDriver.php | 44 ++++---- lib/Doctrine/DBAL/Driver/DriverException.php | 21 +--- .../DBAL/Driver/IBMDB2/DB2Connection.php | 17 ++- .../DBAL/Driver/IBMDB2/DB2Exception.php | 27 +++++ .../DBAL/Driver/IBMDB2/DB2Statement.php | 4 +- .../DBAL/Driver/Mysqli/MysqliConnection.php | 19 +--- .../DBAL/Driver/Mysqli/MysqliException.php | 11 ++ .../DBAL/Driver/Mysqli/MysqliStatement.php | 14 +-- .../DBAL/Driver/OCI8/OCI8Exception.php | 4 +- lib/Doctrine/DBAL/Driver/PDOException.php | 46 ++------ lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php | 2 +- lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php | 2 +- lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php | 2 +- lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php | 2 +- .../SQLAnywhere/SQLAnywhereException.php | 7 +- .../DBAL/Driver/SQLSrv/SQLSrvException.php | 16 ++- .../DBAL/Exception/DriverException.php | 51 ++------- .../DBAL/Schema/OracleSchemaManager.php | 2 +- .../DBAL/Schema/SQLServerSchemaManager.php | 2 +- .../Doctrine/Tests/DBAL/DBALExceptionTest.php | 9 +- .../Tests/DBAL/Driver/AbstractDriverTest.php | 36 ++---- .../DBAL/Driver/AbstractMySQLDriverTest.php | 100 ++++++++--------- .../DBAL/Driver/AbstractOracleDriverTest.php | 26 ++--- .../Driver/AbstractPostgreSQLDriverTest.php | 22 ++-- .../Driver/AbstractSQLAnywhereDriverTest.php | 40 +++---- .../DBAL/Driver/AbstractSQLiteDriverTest.php | 24 ++-- .../Tests/DBAL/Driver/PDOExceptionTest.php | 11 +- .../Tests/DBAL/Functional/WriteTest.php | 2 +- 33 files changed, 330 insertions(+), 426 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 8854436282e..fe6b3ff1b1f 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -1,5 +1,13 @@ # Upgrade to 3.0 +## BC BREAK Changes in driver exceptions + +1. The `Doctrine\DBAL\Driver\DriverException::getErrorCode()` method is removed. In order to obtain the driver error code, please use `::getCode()`. +2. `Doctrine\DBAL\Driver\PDOException` no longer extends `PDOException`. +3. The value returned by `Doctrine\DBAL\Driver\PDOException::getSQLState()` no longer falls back to the driver error code. + +The method was used internally and is no longer needed. + ## BC BREAK `DB2SchemaManager::_getPortableForeignKeyRuleDef()` removed The method was used internally and is no longer needed. diff --git a/lib/Doctrine/DBAL/Driver/AbstractDriverException.php b/lib/Doctrine/DBAL/Driver/AbstractDriverException.php index 59c08090a4c..5f217f22dc2 100644 --- a/lib/Doctrine/DBAL/Driver/AbstractDriverException.php +++ b/lib/Doctrine/DBAL/Driver/AbstractDriverException.php @@ -5,19 +5,13 @@ namespace Doctrine\DBAL\Driver; use Exception; +use Throwable; /** * Abstract base implementation of the {@link DriverException} interface. */ abstract class AbstractDriverException extends Exception implements DriverException { - /** - * The driver specific error code. - * - * @var int|string|null - */ - private $errorCode; - /** * The SQLSTATE of the driver. * @@ -26,30 +20,22 @@ abstract class AbstractDriverException extends Exception implements DriverExcept private $sqlState; /** - * @param string $message The driver error message. - * @param string|null $sqlState The SQLSTATE the driver is in at the time the error occurred, if any. - * @param int|string|null $errorCode The driver specific error code if any. + * @param string $message The driver error message. + * @param string|null $sqlState The SQLSTATE the driver is in at the time the error occurred, if any. + * @param int $code The driver specific error code if any. + * @param Throwable|null $previous The previous throwable used for the exception chaining. */ - public function __construct($message, $sqlState = null, $errorCode = null) + public function __construct(string $message, ?string $sqlState = null, int $code = 0, ?Throwable $previous = null) { - parent::__construct($message); - - $this->errorCode = $errorCode; - $this->sqlState = $sqlState; - } + parent::__construct($message, $code, $previous); - /** - * {@inheritdoc} - */ - public function getErrorCode() - { - return $this->errorCode; + $this->sqlState = $sqlState; } /** * {@inheritdoc} */ - public function getSQLState() + public function getSQLState() : ?string { return $this->sqlState; } diff --git a/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php b/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php index 9229b29bfef..c876723d7ab 100644 --- a/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php +++ b/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php @@ -31,77 +31,77 @@ abstract class AbstractMySQLDriver implements Driver, ExceptionConverterDriver, */ public function convertException($message, DriverException $exception) { - switch ($exception->getErrorCode()) { - case '1213': + switch ($exception->getCode()) { + case 1213: return new Exception\DeadlockException($message, $exception); - case '1205': + case 1205: return new Exception\LockWaitTimeoutException($message, $exception); - case '1050': + case 1050: return new Exception\TableExistsException($message, $exception); - case '1051': - case '1146': + case 1051: + case 1146: return new Exception\TableNotFoundException($message, $exception); - case '1216': - case '1217': - case '1451': - case '1452': - case '1701': + case 1216: + case 1217: + case 1451: + case 1452: + case 1701: return new Exception\ForeignKeyConstraintViolationException($message, $exception); - case '1062': - case '1557': - case '1569': - case '1586': + case 1062: + case 1557: + case 1569: + case 1586: return new Exception\UniqueConstraintViolationException($message, $exception); - case '1054': - case '1166': - case '1611': + case 1054: + case 1166: + case 1611: return new Exception\InvalidFieldNameException($message, $exception); - case '1052': - case '1060': - case '1110': + case 1052: + case 1060: + case 1110: return new Exception\NonUniqueFieldNameException($message, $exception); - case '1064': - case '1149': - case '1287': - case '1341': - case '1342': - case '1343': - case '1344': - case '1382': - case '1479': - case '1541': - case '1554': - case '1626': + case 1064: + case 1149: + case 1287: + case 1341: + case 1342: + case 1343: + case 1344: + case 1382: + case 1479: + case 1541: + case 1554: + case 1626: return new Exception\SyntaxErrorException($message, $exception); - case '1044': - case '1045': - case '1046': - case '1049': - case '1095': - case '1142': - case '1143': - case '1227': - case '1370': - case '1429': - case '2002': - case '2005': + case 1044: + case 1045: + case 1046: + case 1049: + case 1095: + case 1142: + case 1143: + case 1227: + case 1370: + case 1429: + case 2002: + case 2005: return new Exception\ConnectionException($message, $exception); - case '1048': - case '1121': - case '1138': - case '1171': - case '1252': - case '1263': - case '1364': - case '1566': + case 1048: + case 1121: + case 1138: + case 1171: + case 1252: + case 1263: + case 1364: + case 1566: return new Exception\NotNullConstraintViolationException($message, $exception); } diff --git a/lib/Doctrine/DBAL/Driver/AbstractOracleDriver.php b/lib/Doctrine/DBAL/Driver/AbstractOracleDriver.php index e9e1cce5382..00020139565 100644 --- a/lib/Doctrine/DBAL/Driver/AbstractOracleDriver.php +++ b/lib/Doctrine/DBAL/Driver/AbstractOracleDriver.php @@ -21,38 +21,38 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver */ public function convertException($message, DriverException $exception) { - switch ($exception->getErrorCode()) { - case '1': - case '2299': - case '38911': + switch ($exception->getCode()) { + case 1: + case 2299: + case 38911: return new Exception\UniqueConstraintViolationException($message, $exception); - case '904': + case 904: return new Exception\InvalidFieldNameException($message, $exception); - case '918': - case '960': + case 918: + case 960: return new Exception\NonUniqueFieldNameException($message, $exception); - case '923': + case 923: return new Exception\SyntaxErrorException($message, $exception); - case '942': + case 942: return new Exception\TableNotFoundException($message, $exception); - case '955': + case 955: return new Exception\TableExistsException($message, $exception); - case '1017': - case '12545': + case 1017: + case 12545: return new Exception\ConnectionException($message, $exception); - case '1400': + case 1400: return new Exception\NotNullConstraintViolationException($message, $exception); - case '2266': - case '2291': - case '2292': + case 2266: + case 2291: + case 2292: return new Exception\ForeignKeyConstraintViolationException($message, $exception); } diff --git a/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php b/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php index 41a9f058e9a..ea62f8e3603 100644 --- a/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php +++ b/lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php @@ -64,16 +64,13 @@ public function convertException($message, DriverException $exception) case '42P07': return new Exception\TableExistsException($message, $exception); + } - case '7': - // In some case (mainly connection errors) the PDO exception does not provide a SQLSTATE via its code. - // The exception code is always set to 7 here. - // We have to match against the SQLSTATE in the error message in these cases. - if (strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) { - return new Exception\ConnectionException($message, $exception); - } - - break; + // In some case (mainly connection errors) the PDO exception does not provide a SQLSTATE via its code. + // The exception code is always set to 7 here. + // We have to match against the SQLSTATE in the error message in these cases. + if ($exception->getCode() === 7 && strpos($exception->getMessage(), 'SQLSTATE[08006]') !== false) { + return new Exception\ConnectionException($message, $exception); } return new Exception\DriverException($message, $exception); diff --git a/lib/Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php b/lib/Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php index a4c45ccaa23..ebca7b1934e 100644 --- a/lib/Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php +++ b/lib/Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php @@ -25,38 +25,38 @@ abstract class AbstractSQLAnywhereDriver implements Driver, ExceptionConverterDr */ public function convertException($message, DriverException $exception) { - switch ($exception->getErrorCode()) { - case '-306': - case '-307': - case '-684': + switch ($exception->getCode()) { + case -306: + case -307: + case -684: return new Exception\DeadlockException($message, $exception); - case '-210': - case '-1175': - case '-1281': + case -210: + case -1175: + case -1281: return new Exception\LockWaitTimeoutException($message, $exception); - case '-100': - case '-103': - case '-832': + case -100: + case -103: + case -832: return new Exception\ConnectionException($message, $exception); - case '-143': + case -143: return new Exception\InvalidFieldNameException($message, $exception); - case '-193': - case '-196': + case -193: + case -196: return new Exception\UniqueConstraintViolationException($message, $exception); - case '-194': - case '-198': + case -194: + case -198: return new Exception\ForeignKeyConstraintViolationException($message, $exception); - case '-144': + case -144: return new Exception\NonUniqueFieldNameException($message, $exception); - case '-184': - case '-195': + case -184: + case -195: return new Exception\NotNullConstraintViolationException($message, $exception); - case '-131': + case -131: return new Exception\SyntaxErrorException($message, $exception); - case '-110': + case -110: return new Exception\TableExistsException($message, $exception); - case '-141': - case '-1041': + case -141: + case -1041: return new Exception\TableNotFoundException($message, $exception); } diff --git a/lib/Doctrine/DBAL/Driver/DriverException.php b/lib/Doctrine/DBAL/Driver/DriverException.php index c95609712bb..ca7f453ec46 100644 --- a/lib/Doctrine/DBAL/Driver/DriverException.php +++ b/lib/Doctrine/DBAL/Driver/DriverException.php @@ -14,29 +14,10 @@ */ interface DriverException extends Throwable { - /** - * Returns the driver specific error code if available. - * - * Returns null if no driver specific error code is available - * for the error raised by the driver. - * - * @return int|string|null - */ - public function getErrorCode(); - - /** - * Returns the driver error message. - * - * @return string - */ - public function getMessage(); - /** * Returns the SQLSTATE the driver was in at the time the error occurred. * * Returns null if the driver does not provide a SQLSTATE for the error occurred. - * - * @return string|null */ - public function getSQLState(); + public function getSQLState() : ?string; } diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php index ef4dd3546d9..9763a627c14 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Connection.php @@ -24,7 +24,6 @@ use function db2_prepare; use function db2_rollback; use function db2_server_info; -use function db2_stmt_errormsg; class DB2Connection implements Connection, ServerInfoAwareConnection { @@ -50,7 +49,7 @@ public function __construct(array $params, $username, $password, $driverOptions } if ($conn === false) { - throw new DB2Exception(db2_conn_errormsg()); + throw DB2Exception::fromConnectionError(); } $this->conn = $conn; @@ -82,7 +81,7 @@ public function prepare(string $sql) : DriverStatement { $stmt = @db2_prepare($this->conn, $sql); if (! $stmt) { - throw new DB2Exception(db2_stmt_errormsg()); + throw DB2Exception::fromStatementError(); } return new DB2Statement($stmt); @@ -115,7 +114,7 @@ public function exec(string $statement) : int $stmt = @db2_exec($this->conn, $statement); if ($stmt === false) { - throw new DB2Exception(db2_stmt_errormsg()); + throw DB2Exception::fromStatementError(); } return db2_num_rows($stmt); @@ -135,7 +134,7 @@ public function lastInsertId($name = null) public function beginTransaction() : void { if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF)) { - throw new DB2Exception(db2_conn_errormsg($this->conn)); + throw DB2Exception::fromConnectionError($this->conn); } } @@ -145,11 +144,11 @@ public function beginTransaction() : void public function commit() : void { if (! db2_commit($this->conn)) { - throw new DB2Exception(db2_conn_errormsg($this->conn)); + throw DB2Exception::fromConnectionError($this->conn); } if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) { - throw new DB2Exception(db2_conn_errormsg($this->conn)); + throw DB2Exception::fromConnectionError($this->conn); } } @@ -159,11 +158,11 @@ public function commit() : void public function rollBack() : void { if (! db2_rollback($this->conn)) { - throw new DB2Exception(db2_conn_errormsg($this->conn)); + throw DB2Exception::fromConnectionError($this->conn); } if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) { - throw new DB2Exception(db2_conn_errormsg($this->conn)); + throw DB2Exception::fromConnectionError($this->conn); } } diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Exception.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Exception.php index eea9cc1c9e2..bd8d5b298aa 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Exception.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Exception.php @@ -5,7 +5,34 @@ namespace Doctrine\DBAL\Driver\IBMDB2; use Doctrine\DBAL\Driver\AbstractDriverException; +use function db2_conn_error; +use function db2_conn_errormsg; +use function db2_stmt_error; +use function db2_stmt_errormsg; class DB2Exception extends AbstractDriverException { + /** + * @param resource|null $connection + */ + public static function fromConnectionError($connection = null) : self + { + if ($connection !== null) { + return new self(db2_conn_errormsg($connection), db2_conn_error($connection)); + } + + return new self(db2_conn_errormsg(), db2_conn_error()); + } + + /** + * @param resource|null $statement + */ + public static function fromStatementError($statement = null) : self + { + if ($statement !== null) { + return new self(db2_stmt_errormsg($statement), db2_stmt_error($statement)); + } + + return new self(db2_stmt_errormsg(), db2_stmt_error()); + } } diff --git a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php index 0096feff73c..dfe594d386c 100644 --- a/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php +++ b/lib/Doctrine/DBAL/Driver/IBMDB2/DB2Statement.php @@ -137,7 +137,7 @@ private function bind($position, &$variable, int $parameterType, int $dataType) $this->bindParam[$position] =& $variable; if (! db2_bind_param($this->stmt, $position, 'variable', $parameterType, $dataType)) { - throw new DB2Exception(db2_stmt_errormsg()); + throw DB2Exception::fromStatementError($this->stmt); } } @@ -218,7 +218,7 @@ public function execute($params = null) : void $this->lobs = []; if ($retval === false) { - throw new DB2Exception(db2_stmt_errormsg()); + throw DB2Exception::fromStatementError($this->stmt); } $this->result = true; diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php index 0b28b2cae50..b377b79c711 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php @@ -20,8 +20,6 @@ use function floor; use function in_array; use function ini_get; -use function mysqli_errno; -use function mysqli_error; use function mysqli_init; use function mysqli_options; use function restore_error_handler; @@ -70,7 +68,7 @@ public function __construct(array $params, $username, $password, array $driverOp }); try { if (! $this->conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) { - throw new MysqliException($this->conn->connect_error, $this->conn->sqlstate ?? 'HY000', $this->conn->connect_errno); + throw MysqliException::fromConnectionError($this->conn); } } finally { restore_error_handler(); @@ -158,7 +156,7 @@ public function quote(string $input) : string public function exec(string $statement) : int { if ($this->conn->query($statement) === false) { - throw new MysqliException($this->conn->error, $this->conn->sqlstate, $this->conn->errno); + throw MysqliException::fromConnectionError($this->conn); } return $this->conn->affected_rows; @@ -186,7 +184,7 @@ public function beginTransaction() : void public function commit() : void { if (! $this->conn->commit()) { - throw new MysqliException($this->conn->error, $this->conn->sqlstate, $this->conn->errno); + throw MysqliException::fromConnectionError($this->conn); } } @@ -196,7 +194,7 @@ public function commit() : void public function rollBack() : void { if (! $this->conn->rollback()) { - throw new MysqliException($this->conn->error, $this->conn->sqlstate, $this->conn->errno); + throw MysqliException::fromConnectionError($this->conn); } } @@ -255,14 +253,7 @@ private function setDriverOptions(array $driverOptions = []) continue; } - $msg = sprintf($exceptionMsg, 'Failed to set', $option, $value); - $msg .= sprintf(', error: %s (%d)', mysqli_error($this->conn), mysqli_errno($this->conn)); - - throw new MysqliException( - $msg, - $this->conn->sqlstate, - $this->conn->errno - ); + throw MysqliException::fromConnectionError($this->conn); } } diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliException.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliException.php index b779bc40457..3a47d317bcb 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliException.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliException.php @@ -5,10 +5,21 @@ namespace Doctrine\DBAL\Driver\Mysqli; use Doctrine\DBAL\Driver\AbstractDriverException; +use mysqli; +use mysqli_stmt; /** * Exception thrown in case the mysqli driver errors. */ class MysqliException extends AbstractDriverException { + public static function fromConnectionError(mysqli $connection) : self + { + return new self($connection->error, $connection->sqlstate ?: null, $connection->errno); + } + + public static function fromStatementError(mysqli_stmt $statement) : self + { + return new self($statement->error, $statement->sqlstate ?: null, $statement->errno); + } } diff --git a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php index fed098f3c09..156b2dd5835 100644 --- a/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php +++ b/lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php @@ -87,7 +87,7 @@ public function __construct(mysqli $conn, $prepareString) $stmt = $conn->prepare($prepareString); if ($stmt === false) { - throw new MysqliException($this->_conn->error, $this->_conn->sqlstate, $this->_conn->errno); + throw MysqliException::fromConnectionError($this->_conn); } $this->_stmt = $stmt; @@ -139,14 +139,14 @@ public function execute($params = null) : void { if ($params !== null && count($params) > 0) { if (! $this->bindUntypedValues($params)) { - throw new MysqliException($this->_stmt->error, $this->_stmt->errno); + throw MysqliException::fromStatementError($this->_stmt); } } else { $this->bindTypedParameters(); } if (! $this->_stmt->execute()) { - throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); + throw MysqliException::fromStatementError($this->_stmt); } if ($this->_columnNames === null) { @@ -193,7 +193,7 @@ public function execute($params = null) : void } if (! $this->_stmt->bind_result(...$refs)) { - throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); + throw MysqliException::fromStatementError($this->_stmt); } } @@ -232,7 +232,7 @@ private function bindTypedParameters() } if (count($values) > 0 && ! $this->_stmt->bind_param($types, ...$values)) { - throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); + throw MysqliException::fromStatementError($this->_stmt); } $this->sendLongData($streams); @@ -254,7 +254,7 @@ private function sendLongData($streams) } if (! $this->_stmt->send_long_data($paramNr - 1, $chunk)) { - throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); + throw MysqliException::fromStatementError($this->_stmt); } } } @@ -322,7 +322,7 @@ public function fetch($fetchMode = null, ...$args) } if ($values === false) { - throw new MysqliException($this->_stmt->error, $this->_stmt->sqlstate, $this->_stmt->errno); + throw MysqliException::fromStatementError($this->_stmt); } if ($fetchMode === FetchMode::NUMERIC) { diff --git a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Exception.php b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Exception.php index bdb64347a6a..2e7f1693343 100644 --- a/lib/Doctrine/DBAL/Driver/OCI8/OCI8Exception.php +++ b/lib/Doctrine/DBAL/Driver/OCI8/OCI8Exception.php @@ -10,10 +10,8 @@ class OCI8Exception extends AbstractDriverException { /** * @param mixed[]|false $error - * - * @return \Doctrine\DBAL\Driver\OCI8\OCI8Exception */ - public static function fromErrorInfo($error) + public static function fromErrorInfo($error) : self { if ($error === false) { return new self('Database error occurred but no error information was retrieved from the driver.'); diff --git a/lib/Doctrine/DBAL/Driver/PDOException.php b/lib/Doctrine/DBAL/Driver/PDOException.php index 47f12e7834c..e25fcd461ab 100644 --- a/lib/Doctrine/DBAL/Driver/PDOException.php +++ b/lib/Doctrine/DBAL/Driver/PDOException.php @@ -7,48 +7,20 @@ /** * Tiny wrapper for PDOException instances to implement the {@link DriverException} interface. */ -class PDOException extends \PDOException implements DriverException +class PDOException extends AbstractDriverException { - /** - * The driver specific error code. - * - * @var int|string|null - */ - private $errorCode; - - /** - * The SQLSTATE of the driver. - * - * @var string|null - */ - private $sqlState; - /** * @param \PDOException $exception The PDO exception to wrap. */ public function __construct(\PDOException $exception) { - parent::__construct($exception->getMessage(), 0, $exception); - - $this->code = $exception->getCode(); - $this->errorInfo = $exception->errorInfo; - $this->errorCode = $exception->errorInfo[1] ?? $exception->getCode(); - $this->sqlState = $exception->errorInfo[0] ?? $exception->getCode(); - } - - /** - * {@inheritdoc} - */ - public function getErrorCode() - { - return $this->errorCode; - } - - /** - * {@inheritdoc} - */ - public function getSQLState() - { - return $this->sqlState; + if ($exception->errorInfo !== null) { + [$sqlState, $code] = $exception->errorInfo; + } else { + $code = $exception->getCode(); + $sqlState = null; + } + + parent::__construct($exception->getMessage(), $sqlState, $code, $exception); } } diff --git a/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php b/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php index 0320585573a..3aa794c4a27 100644 --- a/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOMySql/Driver.php @@ -7,7 +7,7 @@ use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Driver\AbstractMySQLDriver; use Doctrine\DBAL\Driver\PDOConnection; -use PDOException; +use Doctrine\DBAL\Driver\PDOException; /** * PDO MySql driver. diff --git a/lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php b/lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php index 79dd059a59f..06e726df84d 100644 --- a/lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOOracle/Driver.php @@ -7,7 +7,7 @@ use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Driver\AbstractOracleDriver; use Doctrine\DBAL\Driver\PDOConnection; -use PDOException; +use Doctrine\DBAL\Driver\PDOException; /** * PDO Oracle driver. diff --git a/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php b/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php index c081484c666..f2d8dde4a95 100644 --- a/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOPgSql/Driver.php @@ -7,8 +7,8 @@ use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Driver\AbstractPostgreSQLDriver; use Doctrine\DBAL\Driver\PDOConnection; +use Doctrine\DBAL\Driver\PDOException; use PDO; -use PDOException; use function defined; /** diff --git a/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php b/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php index 6af349aa721..ba86e3c8d92 100644 --- a/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php +++ b/lib/Doctrine/DBAL/Driver/PDOSqlite/Driver.php @@ -7,8 +7,8 @@ use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Driver\AbstractSQLiteDriver; use Doctrine\DBAL\Driver\PDOConnection; +use Doctrine\DBAL\Driver\PDOException; use Doctrine\DBAL\Platforms\SqlitePlatform; -use PDOException; use function array_merge; /** diff --git a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereException.php b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereException.php index 99e405585e3..5613acf9960 100644 --- a/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereException.php +++ b/lib/Doctrine/DBAL/Driver/SQLAnywhere/SQLAnywhereException.php @@ -5,7 +5,6 @@ namespace Doctrine\DBAL\Driver\SQLAnywhere; use Doctrine\DBAL\Driver\AbstractDriverException; -use InvalidArgumentException; use function sasql_error; use function sasql_errorcode; use function sasql_sqlstate; @@ -22,12 +21,8 @@ class SQLAnywhereException extends AbstractDriverException * * @param resource|null $conn The SQL Anywhere connection resource to retrieve the last error from. * @param resource|null $stmt The SQL Anywhere statement resource to retrieve the last error from. - * - * @return SQLAnywhereException - * - * @throws InvalidArgumentException */ - public static function fromSQLAnywhereError($conn = null, $stmt = null) + public static function fromSQLAnywhereError($conn = null, $stmt = null) : self { $state = $conn ? sasql_sqlstate($conn) : sasql_sqlstate(); $code = null; diff --git a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvException.php b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvException.php index cbb4fa49e38..87e982c9d82 100644 --- a/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvException.php +++ b/lib/Doctrine/DBAL/Driver/SQLSrv/SQLSrvException.php @@ -13,14 +13,12 @@ class SQLSrvException extends AbstractDriverException { /** * Helper method to turn sql server errors into exception. - * - * @return \Doctrine\DBAL\Driver\SQLSrv\SQLSrvException */ - public static function fromSqlSrvErrors() + public static function fromSqlSrvErrors() : self { - $message = ''; - $sqlState = null; - $errorCode = null; + $message = ''; + $sqlState = null; + $code = null; foreach ((array) sqlsrv_errors(SQLSRV_ERR_ERRORS) as $error) { $message .= 'SQLSTATE [' . $error['SQLSTATE'] . ', ' . $error['code'] . ']: ' . $error['message'] . "\n"; @@ -29,17 +27,17 @@ public static function fromSqlSrvErrors() $sqlState = $error['SQLSTATE']; } - if ($errorCode !== null) { + if ($code !== null) { continue; } - $errorCode = $error['code']; + $code = $error['code']; } if (! $message) { $message = 'SQL Server error occurred but no error message was retrieved from driver.'; } - return new self(rtrim($message), $sqlState, $errorCode); + return new self(rtrim($message), $sqlState, $code); } } diff --git a/lib/Doctrine/DBAL/Exception/DriverException.php b/lib/Doctrine/DBAL/Exception/DriverException.php index 831ddd08116..ed6ee082a06 100644 --- a/lib/Doctrine/DBAL/Exception/DriverException.php +++ b/lib/Doctrine/DBAL/Exception/DriverException.php @@ -5,58 +5,31 @@ namespace Doctrine\DBAL\Exception; use Doctrine\DBAL\DBALException; -use Exception; +use Doctrine\DBAL\Driver\DriverException as DriverExceptionInterface; +use function assert; /** * Base class for all errors detected in the driver. */ -class DriverException extends DBALException +class DriverException extends DBALException implements DriverExceptionInterface { /** - * The previous DBAL driver exception. - * - * @var \Doctrine\DBAL\Driver\DriverException + * @param string $message The exception message. + * @param DriverExceptionInterface $driverException The DBAL driver exception to chain. */ - private $driverException; - - /** - * @param string $message The exception message. - * @param \Doctrine\DBAL\Driver\DriverException $driverException The DBAL driver exception to chain. - */ - public function __construct($message, \Doctrine\DBAL\Driver\DriverException $driverException) + public function __construct(string $message, DriverExceptionInterface $driverException) { - $exception = null; - - if ($driverException instanceof Exception) { - $exception = $driverException; - } - - parent::__construct($message, 0, $exception); - - $this->driverException = $driverException; + parent::__construct($message, $driverException->getCode(), $driverException); } /** - * Returns the driver specific error code if given. - * - * Returns null if no error code was given by the driver. - * - * @return int|string|null + * {@inheritDoc} */ - public function getErrorCode() + public function getSQLState() : ?string { - return $this->driverException->getErrorCode(); - } + $previous = $this->getPrevious(); + assert($previous instanceof DriverExceptionInterface); - /** - * Returns the SQLSTATE the driver was in at the time the error occurred, if given. - * - * Returns null if no SQLSTATE was given by the driver. - * - * @return string|null - */ - public function getSQLState() - { - return $this->driverException->getSQLState(); + return $previous->getSQLState(); } } diff --git a/lib/Doctrine/DBAL/Schema/OracleSchemaManager.php b/lib/Doctrine/DBAL/Schema/OracleSchemaManager.php index 8acabf36642..4bf88abc1c9 100644 --- a/lib/Doctrine/DBAL/Schema/OracleSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/OracleSchemaManager.php @@ -44,7 +44,7 @@ public function dropDatabase($database) // because of active connections on the database. // To force dropping the database, we first have to close all active connections // on that database and issue the drop database operation again. - if ($exception->getErrorCode() !== 1940) { + if ($exception->getCode() !== 1940) { throw $exception; } diff --git a/lib/Doctrine/DBAL/Schema/SQLServerSchemaManager.php b/lib/Doctrine/DBAL/Schema/SQLServerSchemaManager.php index 127f8587e01..08aa1d1e313 100644 --- a/lib/Doctrine/DBAL/Schema/SQLServerSchemaManager.php +++ b/lib/Doctrine/DBAL/Schema/SQLServerSchemaManager.php @@ -44,7 +44,7 @@ public function dropDatabase($database) // because of active connections on the database. // To force dropping the database, we first have to close all active connections // on that database and issue the drop database operation again. - if ($exception->getErrorCode() !== 3702) { + if ($exception->getCode() !== 3702) { throw $exception; } diff --git a/tests/Doctrine/Tests/DBAL/DBALExceptionTest.php b/tests/Doctrine/Tests/DBAL/DBALExceptionTest.php index e3aeead3e27..de251cdceac 100644 --- a/tests/Doctrine/Tests/DBAL/DBALExceptionTest.php +++ b/tests/Doctrine/Tests/DBAL/DBALExceptionTest.php @@ -42,14 +42,7 @@ public function testAvoidOverWrappingOnDriverException() /** * {@inheritDoc} */ - public function getErrorCode() - { - } - - /** - * {@inheritDoc} - */ - public function getSQLState() + public function getSQLState() : ?string { } }; diff --git a/tests/Doctrine/Tests/DBAL/Driver/AbstractDriverTest.php b/tests/Doctrine/Tests/DBAL/Driver/AbstractDriverTest.php index 47d04e03373..c86a75fab5b 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/AbstractDriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/AbstractDriverTest.php @@ -97,15 +97,7 @@ public function __construct() /** * {@inheritDoc} */ - public function getErrorCode() - { - return 'foo'; - } - - /** - * {@inheritDoc} - */ - public function getSQLState() + public function getSQLState() : ?string { return 'bar'; } @@ -123,7 +115,7 @@ public function getSQLState() self::assertSame($convertedExceptionClassName, get_class($convertedException)); - self::assertSame($driverException->getErrorCode(), $convertedException->getErrorCode()); + self::assertSame($driverException->getCode(), $convertedException->getCode()); self::assertSame($driverException->getSQLState(), $convertedException->getSQLState()); self::assertSame($message, $convertedException->getMessage()); } @@ -259,36 +251,24 @@ private function getExceptionConversions() foreach ($this->getExceptionConversionData() as $convertedExceptionClassName => $errors) { foreach ($errors as $error) { - $driverException = new class ($error[0], $error[1], $error[2]) + $driverException = new class (...$error) extends Exception implements DriverExceptionInterface { - /** @var mixed */ - private $errorCode; - - /** @var mixed */ + /** @var string|null */ private $sqlState; - public function __construct($errorCode, $sqlState, $message) + public function __construct(int $code, ?string $sqlState = null, string $message = '') { - parent::__construct($message ?? ''); + parent::__construct($message, $code); - $this->errorCode = $errorCode; - $this->sqlState = $sqlState; - } - - /** - * {@inheritDoc} - */ - public function getErrorCode() - { - return $this->errorCode; + $this->sqlState = $sqlState; } /** * {@inheritDoc} */ - public function getSQLState() + public function getSQLState() : ?string { return $this->sqlState; } diff --git a/tests/Doctrine/Tests/DBAL/Driver/AbstractMySQLDriverTest.php b/tests/Doctrine/Tests/DBAL/Driver/AbstractMySQLDriverTest.php index 81140b97754..775388bf7bd 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/AbstractMySQLDriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/AbstractMySQLDriverTest.php @@ -89,76 +89,76 @@ protected function getExceptionConversionData() { return [ self::EXCEPTION_CONNECTION => [ - ['1044', null, null], - ['1045', null, null], - ['1046', null, null], - ['1049', null, null], - ['1095', null, null], - ['1142', null, null], - ['1143', null, null], - ['1227', null, null], - ['1370', null, null], - ['2002', null, null], - ['2005', null, null], + [1044], + [1045], + [1046], + [1049], + [1095], + [1142], + [1143], + [1227], + [1370], + [2002], + [2005], ], self::EXCEPTION_FOREIGN_KEY_CONSTRAINT_VIOLATION => [ - ['1216', null, null], - ['1217', null, null], - ['1451', null, null], - ['1452', null, null], + [1216], + [1217], + [1451], + [1452], ], self::EXCEPTION_INVALID_FIELD_NAME => [ - ['1054', null, null], - ['1166', null, null], - ['1611', null, null], + [1054], + [1166], + [1611], ], self::EXCEPTION_NON_UNIQUE_FIELD_NAME => [ - ['1052', null, null], - ['1060', null, null], - ['1110', null, null], + [1052], + [1060], + [1110], ], self::EXCEPTION_NOT_NULL_CONSTRAINT_VIOLATION => [ - ['1048', null, null], - ['1121', null, null], - ['1138', null, null], - ['1171', null, null], - ['1252', null, null], - ['1263', null, null], - ['1364', null, null], - ['1566', null, null], + [1048], + [1121], + [1138], + [1171], + [1252], + [1263], + [1364], + [1566], ], self::EXCEPTION_SYNTAX_ERROR => [ - ['1064', null, null], - ['1149', null, null], - ['1287', null, null], - ['1341', null, null], - ['1342', null, null], - ['1343', null, null], - ['1344', null, null], - ['1382', null, null], - ['1479', null, null], - ['1541', null, null], - ['1554', null, null], - ['1626', null, null], + [1064], + [1149], + [1287], + [1341], + [1342], + [1343], + [1344], + [1382], + [1479], + [1541], + [1554], + [1626], ], self::EXCEPTION_TABLE_EXISTS => [ - ['1050', null, null], + [1050], ], self::EXCEPTION_TABLE_NOT_FOUND => [ - ['1051', null, null], - ['1146', null, null], + [1051], + [1146], ], self::EXCEPTION_UNIQUE_CONSTRAINT_VIOLATION => [ - ['1062', null, null], - ['1557', null, null], - ['1569', null, null], - ['1586', null, null], + [1062], + [1557], + [1569], + [1586], ], self::EXCEPTION_DEADLOCK => [ - ['1213', null, null], + [1213], ], self::EXCEPTION_LOCK_WAIT_TIMEOUT => [ - ['1205', null, null], + [1205], ], ]; } diff --git a/tests/Doctrine/Tests/DBAL/Driver/AbstractOracleDriverTest.php b/tests/Doctrine/Tests/DBAL/Driver/AbstractOracleDriverTest.php index fcf5bd6366e..f6348805102 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/AbstractOracleDriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/AbstractOracleDriverTest.php @@ -66,35 +66,35 @@ protected function getExceptionConversionData() { return [ self::EXCEPTION_CONNECTION => [ - ['1017', null, null], - ['12545', null, null], + [1017], + [12545], ], self::EXCEPTION_FOREIGN_KEY_CONSTRAINT_VIOLATION => [ - ['2292', null, null], + [2292], ], self::EXCEPTION_INVALID_FIELD_NAME => [ - ['904', null, null], + [904], ], self::EXCEPTION_NON_UNIQUE_FIELD_NAME => [ - ['918', null, null], - ['960', null, null], + [918], + [960], ], self::EXCEPTION_NOT_NULL_CONSTRAINT_VIOLATION => [ - ['1400', null, null], + [1400], ], self::EXCEPTION_SYNTAX_ERROR => [ - ['923', null, null], + [923], ], self::EXCEPTION_TABLE_EXISTS => [ - ['955', null, null], + [955], ], self::EXCEPTION_TABLE_NOT_FOUND => [ - ['942', null, null], + [942], ], self::EXCEPTION_UNIQUE_CONSTRAINT_VIOLATION => [ - ['1', null, null], - ['2299', null, null], - ['38911', null, null], + [1], + [2299], + [38911], ], ]; } diff --git a/tests/Doctrine/Tests/DBAL/Driver/AbstractPostgreSQLDriverTest.php b/tests/Doctrine/Tests/DBAL/Driver/AbstractPostgreSQLDriverTest.php index f7f06b1423e..5b515ace2e8 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/AbstractPostgreSQLDriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/AbstractPostgreSQLDriverTest.php @@ -75,35 +75,35 @@ protected function getExceptionConversionData() { return [ self::EXCEPTION_CONNECTION => [ - [null, '7', 'SQLSTATE[08006]'], + [7, null, 'SQLSTATE[08006]'], ], self::EXCEPTION_FOREIGN_KEY_CONSTRAINT_VIOLATION => [ - [null, '23503', null], + [0, '23503'], ], self::EXCEPTION_INVALID_FIELD_NAME => [ - [null, '42703', null], + [0, '42703'], ], self::EXCEPTION_NON_UNIQUE_FIELD_NAME => [ - [null, '42702', null], + [0, '42702'], ], self::EXCEPTION_NOT_NULL_CONSTRAINT_VIOLATION => [ - [null, '23502', null], + [0, '23502'], ], self::EXCEPTION_SYNTAX_ERROR => [ - [null, '42601', null], + [0, '42601'], ], self::EXCEPTION_TABLE_EXISTS => [ - [null, '42P07', null], + [0, '42P07'], ], self::EXCEPTION_TABLE_NOT_FOUND => [ - [null, '42P01', null], + [0, '42P01'], ], self::EXCEPTION_UNIQUE_CONSTRAINT_VIOLATION => [ - [null, '23505', null], + [0, '23505'], ], self::EXCEPTION_DEADLOCK => [ - [null, '40001', null], - [null, '40P01', null], + [0, '40001'], + [0, '40P01'], ], ]; } diff --git a/tests/Doctrine/Tests/DBAL/Driver/AbstractSQLAnywhereDriverTest.php b/tests/Doctrine/Tests/DBAL/Driver/AbstractSQLAnywhereDriverTest.php index f3bdb4ebb23..9615e8beabf 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/AbstractSQLAnywhereDriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/AbstractSQLAnywhereDriverTest.php @@ -43,46 +43,46 @@ protected function getExceptionConversionData() { return [ self::EXCEPTION_CONNECTION => [ - ['-100', null, null], - ['-103', null, null], - ['-832', null, null], + [-100], + [-103], + [-832], ], self::EXCEPTION_FOREIGN_KEY_CONSTRAINT_VIOLATION => [ - ['-198', null, null], + [-198], ], self::EXCEPTION_INVALID_FIELD_NAME => [ - ['-143', null, null], + [-143], ], self::EXCEPTION_NON_UNIQUE_FIELD_NAME => [ - ['-144', null, null], + [-144], ], self::EXCEPTION_NOT_NULL_CONSTRAINT_VIOLATION => [ - ['-184', null, null], - ['-195', null, null], + [-184], + [-195], ], self::EXCEPTION_SYNTAX_ERROR => [ - ['-131', null, null], + [-131], ], self::EXCEPTION_TABLE_EXISTS => [ - ['-110', null, null], + [-110], ], self::EXCEPTION_TABLE_NOT_FOUND => [ - ['-141', null, null], - ['-1041', null, null], + [-141], + [-1041], ], self::EXCEPTION_UNIQUE_CONSTRAINT_VIOLATION => [ - ['-193', null, null], - ['-196', null, null], + [-193], + [-196], ], self::EXCEPTION_DEADLOCK => [ - ['-306', null, null], - ['-307', null, null], - ['-684', null, null], + [-306], + [-307], + [-684], ], self::EXCEPTION_LOCK_WAIT_TIMEOUT => [ - ['-210', null, null], - ['-1175', null, null], - ['-1281', null, null], + [-210], + [-1175], + [-1281], ], ]; } diff --git a/tests/Doctrine/Tests/DBAL/Driver/AbstractSQLiteDriverTest.php b/tests/Doctrine/Tests/DBAL/Driver/AbstractSQLiteDriverTest.php index a53229cd5b7..0dec1909014 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/AbstractSQLiteDriverTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/AbstractSQLiteDriverTest.php @@ -48,36 +48,36 @@ protected function getExceptionConversionData() { return [ self::EXCEPTION_CONNECTION => [ - [null, null, 'unable to open database file'], + [0, null, 'unable to open database file'], ], self::EXCEPTION_INVALID_FIELD_NAME => [ - [null, null, 'has no column named'], + [0, null, 'has no column named'], ], self::EXCEPTION_NON_UNIQUE_FIELD_NAME => [ - [null, null, 'ambiguous column name'], + [0, null, 'ambiguous column name'], ], self::EXCEPTION_NOT_NULL_CONSTRAINT_VIOLATION => [ - [null, null, 'may not be NULL'], + [0, null, 'may not be NULL'], ], self::EXCEPTION_READ_ONLY => [ - [null, null, 'attempt to write a readonly database'], + [0, null, 'attempt to write a readonly database'], ], self::EXCEPTION_SYNTAX_ERROR => [ - [null, null, 'syntax error'], + [0, null, 'syntax error'], ], self::EXCEPTION_TABLE_EXISTS => [ - [null, null, 'already exists'], + [0, null, 'already exists'], ], self::EXCEPTION_TABLE_NOT_FOUND => [ - [null, null, 'no such table:'], + [0, null, 'no such table:'], ], self::EXCEPTION_UNIQUE_CONSTRAINT_VIOLATION => [ - [null, null, 'must be unique'], - [null, null, 'is not unique'], - [null, null, 'are not unique'], + [0, null, 'must be unique'], + [0, null, 'is not unique'], + [0, null, 'are not unique'], ], self::EXCEPTION_LOCK_WAIT_TIMEOUT => [ - [null, null, 'database is locked'], + [0, null, 'database is locked'], ], ]; } diff --git a/tests/Doctrine/Tests/DBAL/Driver/PDOExceptionTest.php b/tests/Doctrine/Tests/DBAL/Driver/PDOExceptionTest.php index 08b69ee253d..f8930233dc0 100644 --- a/tests/Doctrine/Tests/DBAL/Driver/PDOExceptionTest.php +++ b/tests/Doctrine/Tests/DBAL/Driver/PDOExceptionTest.php @@ -15,7 +15,7 @@ class PDOExceptionTest extends DbalTestCase public const MESSAGE = 'PDO Exception'; - public const SQLSTATE = 28000; + public const SQLSTATE = 'HY000'; /** * The PDO exception wrapper under test. @@ -39,7 +39,7 @@ protected function setUp() : void parent::setUp(); - $this->wrappedException = new \PDOException(self::MESSAGE, self::SQLSTATE); + $this->wrappedException = new \PDOException(self::MESSAGE); $this->wrappedException->errorInfo = [self::SQLSTATE, self::ERROR_CODE]; @@ -48,12 +48,7 @@ protected function setUp() : void public function testReturnsCode() { - self::assertSame(self::SQLSTATE, $this->exception->getCode()); - } - - public function testReturnsErrorCode() - { - self::assertSame(self::ERROR_CODE, $this->exception->getErrorCode()); + self::assertSame(self::ERROR_CODE, $this->exception->getCode()); } public function testReturnsMessage() diff --git a/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php b/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php index 31d64b99044..a05ab7a948d 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/WriteTest.php @@ -351,7 +351,7 @@ private function lastInsertId(?string $name = null) try { return $this->connection->lastInsertId($name); } catch (DriverException $e) { - if ($e->getCode() === 'IM001') { + if ($e->getSQLState() === 'IM001') { $this->markTestSkipped($e->getMessage()); }