From ac27abe9898eda0edfe9153cf620d85a16930743 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 22 Dec 2013 20:42:54 +0100 Subject: [PATCH 1/3] [DBAL-722] Introduce subclasses for runtime relevant exceptions (NotNull, DuplicateKey, ForeignKey Constraint Violation). --- lib/Doctrine/DBAL/DBALException.php | 28 ++++++++++++++++-- .../DBAL/Exception/DuplicateKeyException.php | 29 +++++++++++++++++++ ...ForeignKeyConstraintViolationException.php | 29 +++++++++++++++++++ .../DBAL/Exception/NotNullableException.php | 29 +++++++++++++++++++ .../Tests/DBAL/Functional/ExceptionTest.php | 6 ++-- 5 files changed, 116 insertions(+), 5 deletions(-) create mode 100644 lib/Doctrine/DBAL/Exception/DuplicateKeyException.php create mode 100644 lib/Doctrine/DBAL/Exception/ForeignKeyConstraintViolationException.php create mode 100644 lib/Doctrine/DBAL/Exception/NotNullableException.php diff --git a/lib/Doctrine/DBAL/DBALException.php b/lib/Doctrine/DBAL/DBALException.php index 079fdedc31a..15101f356a9 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,31 @@ 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. + */ + 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..ec9896b7ef0 --- /dev/null +++ b/lib/Doctrine/DBAL/Exception/DuplicateKeyException.php @@ -0,0 +1,29 @@ +. + */ + +namespace Doctrine\DBAL\Exception; + +use Doctrine\DBAL\DBALException; + +/** + * Thrown when {@link DBALException::ERROR_DUPLICATE_KEY} is detected in driver + */ +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..7de59296ba1 --- /dev/null +++ b/lib/Doctrine/DBAL/Exception/ForeignKeyConstraintViolationException.php @@ -0,0 +1,29 @@ +. + */ + +namespace Doctrine\DBAL\Exception; + +use Doctrine\DBAL\DBALException; + +/** + * Thrown when {@link DBALException::ERROR_FOREIGN_KEY_CONSTRAINT} is detected in driver + */ +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..1a092ae47f7 --- /dev/null +++ b/lib/Doctrine/DBAL/Exception/NotNullableException.php @@ -0,0 +1,29 @@ +. + */ + +namespace Doctrine\DBAL\Exception; + +use Doctrine\DBAL\DBALException; + +/** + * Thrown when {@link DBALException::ERROR_NOT_NULL} is detected in driver + */ +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)); } From 19e4fd2a25d80448d107be7a491c14263dc2185a Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 22 Dec 2013 20:51:33 +0100 Subject: [PATCH 2/3] [DBAL-722] Add missing doc @return. --- lib/Doctrine/DBAL/DBALException.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/Doctrine/DBAL/DBALException.php b/lib/Doctrine/DBAL/DBALException.php index 15101f356a9..731f9a87652 100644 --- a/lib/Doctrine/DBAL/DBALException.php +++ b/lib/Doctrine/DBAL/DBALException.php @@ -129,6 +129,8 @@ public static function driverException(Driver $driver, \Exception $driverEx) /** * Factory method for subclasses of DBALException based on exception code. + * + * @return \Doctrine\DBAL\DBALException */ private static function createDriverException($msg, $code, $driverEx) { From 21b68939e20ade29947ae64485e3f30a05e63919 Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Sun, 22 Dec 2013 21:02:50 +0100 Subject: [PATCH 3/3] [DBAL-722] add @since tag [ci skip] --- lib/Doctrine/DBAL/Exception/DuplicateKeyException.php | 2 ++ .../DBAL/Exception/ForeignKeyConstraintViolationException.php | 2 ++ lib/Doctrine/DBAL/Exception/NotNullableException.php | 2 ++ 3 files changed, 6 insertions(+) diff --git a/lib/Doctrine/DBAL/Exception/DuplicateKeyException.php b/lib/Doctrine/DBAL/Exception/DuplicateKeyException.php index ec9896b7ef0..7caa0a5f884 100644 --- a/lib/Doctrine/DBAL/Exception/DuplicateKeyException.php +++ b/lib/Doctrine/DBAL/Exception/DuplicateKeyException.php @@ -23,6 +23,8 @@ /** * 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 index 7de59296ba1..ecece925964 100644 --- a/lib/Doctrine/DBAL/Exception/ForeignKeyConstraintViolationException.php +++ b/lib/Doctrine/DBAL/Exception/ForeignKeyConstraintViolationException.php @@ -23,6 +23,8 @@ /** * 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 index 1a092ae47f7..6d4d3fcfa85 100644 --- a/lib/Doctrine/DBAL/Exception/NotNullableException.php +++ b/lib/Doctrine/DBAL/Exception/NotNullableException.php @@ -23,6 +23,8 @@ /** * Thrown when {@link DBALException::ERROR_NOT_NULL} is detected in driver + * + * @since 2.5 */ class NotNullableException extends DBALException {