Skip to content
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

Remove DriverException::getErrorCode() #4113

Merged
merged 1 commit into from
Jun 26, 2020
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
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# 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()` or `::getSQLState()`.
2. The value returned by `Doctrine\DBAL\Driver\PDOException::getSQLState()` no longer falls back to the driver error code.

## BC BREAK: Changes in `OracleSchemaManager::createDatabase()`

The `$database` argument is no longer nullable or optional.
Expand Down
30 changes: 8 additions & 22 deletions src/Driver/AbstractException.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\DBAL\Driver;

use Exception as BaseException;
use Throwable;

/**
* Base implementation of the {@link Exception} interface.
Expand All @@ -15,13 +16,6 @@
*/
abstract class AbstractException extends BaseException implements DriverException
{
/**
* The driver specific error code.
*
* @var int|string|null
*/
private $errorCode;

/**
* The SQLSTATE of the driver.
*
Expand All @@ -30,24 +24,16 @@ abstract class AbstractException extends BaseException implements DriverExceptio
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($message, $sqlState = null, $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;
}

/**
Expand Down
106 changes: 53 additions & 53 deletions src/Driver/AbstractMySQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,79 +41,79 @@ abstract class AbstractMySQLDriver implements ExceptionConverterDriver, VersionA
*/
public function convertException($message, DeprecatedDriverException $exception)
{
switch ($exception->getErrorCode()) {
case '1213':
switch ($exception->getCode()) {
case 1213:
return new DeadlockException($message, $exception);

case '1205':
case 1205:
return new LockWaitTimeoutException($message, $exception);

case '1050':
case 1050:
return new TableExistsException($message, $exception);

case '1051':
case '1146':
case 1051:
case 1146:
return new 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 ForeignKeyConstraintViolationException($message, $exception);

case '1062':
case '1557':
case '1569':
case '1586':
case 1062:
case 1557:
case 1569:
case 1586:
return new UniqueConstraintViolationException($message, $exception);

case '1054':
case '1166':
case '1611':
case 1054:
case 1166:
case 1611:
return new InvalidFieldNameException($message, $exception);

case '1052':
case '1060':
case '1110':
case 1052:
case 1060:
case 1110:
return new 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 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 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 NotNullConstraintViolationException($message, $exception);
}

Expand Down
32 changes: 16 additions & 16 deletions src/Driver/AbstractOracleDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,38 +29,38 @@ abstract class AbstractOracleDriver implements Driver, ExceptionConverterDriver
*/
public function convertException($message, DeprecatedDriverException $exception)
{
switch ($exception->getErrorCode()) {
case '1':
case '2299':
case '38911':
switch ($exception->getCode()) {
case 1:
case 2299:
case 38911:
return new UniqueConstraintViolationException($message, $exception);

case '904':
case 904:
return new InvalidFieldNameException($message, $exception);

case '918':
case '960':
case 918:
case 960:
return new NonUniqueFieldNameException($message, $exception);

case '923':
case 923:
return new SyntaxErrorException($message, $exception);

case '942':
case 942:
return new TableNotFoundException($message, $exception);

case '955':
case 955:
return new TableExistsException($message, $exception);

case '1017':
case '12545':
case 1017:
case 12545:
return new ConnectionException($message, $exception);

case '1400':
case 1400:
return new NotNullConstraintViolationException($message, $exception);

case '2266':
case '2291':
case '2292':
case 2266:
case 2291:
case 2292:
return new ForeignKeyConstraintViolationException($message, $exception);
}

Expand Down
15 changes: 6 additions & 9 deletions src/Driver/AbstractPostgreSQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,13 @@ public function convertException($message, DeprecatedDriverException $exception)

case '42P07':
return new 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 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 ConnectionException($message, $exception);
}

return new DriverException($message, $exception);
Expand Down
10 changes: 0 additions & 10 deletions src/Driver/Exception.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@
*/
interface Exception 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 SQLSTATE the driver was in at the time the error occurred.
*
Expand Down
44 changes: 8 additions & 36 deletions src/Driver/PDOException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,20 @@
*
* @psalm-immutable
*/
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;
}
if ($exception->errorInfo !== null) {
[$sqlState, $code] = $exception->errorInfo;
} else {
$code = $exception->getCode();
$sqlState = null;
}

/**
* {@inheritdoc}
*/
public function getSQLState()
{
return $this->sqlState;
parent::__construct($exception->getMessage(), $sqlState, $code, $exception);
}
}
12 changes: 6 additions & 6 deletions src/Driver/SQLSrv/Exception/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ final class Error extends SQLSrvException
{
public static function new(): self
{
$message = '';
$sqlState = null;
$errorCode = null;
$message = '';
$sqlState = null;
$code = 0;

foreach ((array) sqlsrv_errors(SQLSRV_ERR_ERRORS) as $error) {
$message .= 'SQLSTATE [' . $error['SQLSTATE'] . ', ' . $error['code'] . ']: ' . $error['message'] . "\n";
Expand All @@ -31,17 +31,17 @@ public static function new(): self
$sqlState = $error['SQLSTATE'];
}

if ($errorCode !== null) {
if ($code !== 0) {
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);
}
}
Loading