Skip to content

Commit

Permalink
Merge pull request doctrine#3505 from morozov/driver-exceptions
Browse files Browse the repository at this point in the history
Reworked driver exceptions
  • Loading branch information
Ocramius authored and morozov committed Aug 26, 2019
2 parents 86d5b23 + ba194db commit a58785b
Show file tree
Hide file tree
Showing 32 changed files with 326 additions and 399 deletions.
8 changes: 8 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
32 changes: 9 additions & 23 deletions lib/Doctrine/DBAL/Driver/AbstractDriverException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
Expand Down
106 changes: 53 additions & 53 deletions lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
32 changes: 16 additions & 16 deletions lib/Doctrine/DBAL/Driver/AbstractOracleDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
15 changes: 6 additions & 9 deletions lib/Doctrine/DBAL/Driver/AbstractPostgreSQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
44 changes: 22 additions & 22 deletions lib/Doctrine/DBAL/Driver/AbstractSQLAnywhereDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
21 changes: 1 addition & 20 deletions lib/Doctrine/DBAL/Driver/DriverException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading

0 comments on commit a58785b

Please sign in to comment.