diff --git a/lib/Doctrine/DBAL/DBALException.php b/lib/Doctrine/DBAL/DBALException.php index 079fdedc31a..731f9a87652 100644 --- a/lib/Doctrine/DBAL/DBALException.php +++ b/lib/Doctrine/DBAL/DBALException.php @@ -107,7 +107,7 @@ public static function driverExceptionDuringQuery(Driver $driver, \Exception $dr ? $driver->convertExceptionCode($driverEx) : 0; - return new self($msg, $code, $driverEx); + return self::createDriverException($msg, $code, $driverEx); } /** @@ -120,7 +120,33 @@ public static function driverException(Driver $driver, \Exception $driverEx) { $msg = "An exception occured in driver: " . $driverEx->getMessage(); - return new self($msg, $driver->convertExceptionCode($driverEx), $driverEx); + $code = ($driver instanceof ExceptionConverterDriver) + ? $driver->convertExceptionCode($driverEx) + : 0; + + return self::createDriverException($msg, $code, $driverEx); + } + + /** + * Factory method for subclasses of DBALException based on exception code. + * + * @return \Doctrine\DBAL\DBALException + */ + private static function createDriverException($msg, $code, $driverEx) + { + switch ($code) { + case self::ERROR_NOT_NULL: + return new Exception\NotNullableException($msg, $code, $driverEx); + + case self::ERROR_DUPLICATE_KEY: + return new Exception\DuplicateKeyException($msg, $code, $driverEx); + + case self::ERROR_FOREIGN_KEY_CONSTRAINT: + return new Exception\ForeignKeyConstraintViolationException($msg, $code, $driverEx); + + default: + return new self($msg, $code, $driverEx); + } } /** diff --git a/lib/Doctrine/DBAL/Exception/DuplicateKeyException.php b/lib/Doctrine/DBAL/Exception/DuplicateKeyException.php new file mode 100644 index 00000000000..7caa0a5f884 --- /dev/null +++ b/lib/Doctrine/DBAL/Exception/DuplicateKeyException.php @@ -0,0 +1,31 @@ +. + */ + +namespace Doctrine\DBAL\Exception; + +use Doctrine\DBAL\DBALException; + +/** + * Thrown when {@link DBALException::ERROR_DUPLICATE_KEY} is detected in driver + * + * @since 2.5 + */ +class DuplicateKeyException extends DBALException +{ +} diff --git a/lib/Doctrine/DBAL/Exception/ForeignKeyConstraintViolationException.php b/lib/Doctrine/DBAL/Exception/ForeignKeyConstraintViolationException.php new file mode 100644 index 00000000000..ecece925964 --- /dev/null +++ b/lib/Doctrine/DBAL/Exception/ForeignKeyConstraintViolationException.php @@ -0,0 +1,31 @@ +. + */ + +namespace Doctrine\DBAL\Exception; + +use Doctrine\DBAL\DBALException; + +/** + * Thrown when {@link DBALException::ERROR_FOREIGN_KEY_CONSTRAINT} is detected in driver + * + * @since 2.5 + */ +class ForeignKeyConstraintViolationException extends DBALException +{ +} diff --git a/lib/Doctrine/DBAL/Exception/NotNullableException.php b/lib/Doctrine/DBAL/Exception/NotNullableException.php new file mode 100644 index 00000000000..6d4d3fcfa85 --- /dev/null +++ b/lib/Doctrine/DBAL/Exception/NotNullableException.php @@ -0,0 +1,31 @@ +. + */ + +namespace Doctrine\DBAL\Exception; + +use Doctrine\DBAL\DBALException; + +/** + * Thrown when {@link DBALException::ERROR_NOT_NULL} is detected in driver + * + * @since 2.5 + */ +class NotNullableException extends DBALException +{ +} diff --git a/tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php b/tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php index c4f58d73f94..626d274e4dd 100644 --- a/tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php +++ b/tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php @@ -27,7 +27,7 @@ public function testDuplicateKeyException() $this->_conn->insert("duplicatekey_table", array('id' => 1)); - $this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_DUPLICATE_KEY); + $this->setExpectedException('\Doctrine\DBAL\Exception\DuplicateKeyException', null, DBALException::ERROR_DUPLICATE_KEY); $this->_conn->insert("duplicatekey_table", array('id' => 1)); } @@ -79,7 +79,7 @@ public function testForeignKeyContraintException() $this->_conn->insert("constraint_error_table", array('id' => 1)); $this->_conn->insert("owning_table", array('id' => 1, 'constraint_id' => 1)); - $this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_FOREIGN_KEY_CONSTRAINT); + $this->setExpectedException('\Doctrine\DBAL\Exception\ForeignKeyConstraintViolationException', null, DBALException::ERROR_FOREIGN_KEY_CONSTRAINT); $this->_conn->delete('constraint_error_table', array('id' => 1)); } @@ -96,7 +96,7 @@ public function testNotNullException() $this->_conn->executeQuery($sql); } - $this->setExpectedException('\Doctrine\DBAL\DBALException', null, DBALException::ERROR_NOT_NULL); + $this->setExpectedException('\Doctrine\DBAL\Exception\NotNullableException', null, DBALException::ERROR_NOT_NULL); $this->_conn->insert("notnull_table", array('id' => 1, 'value' => null)); }