[DBAL-722] Introduce subclasses for runtime relevant exceptions #458

Merged
merged 3 commits into from Dec 22, 2013

Projects

None yet

4 participants

@beberlei
Doctrine member

No description provided.

@beberlei beberlei [DBAL-722] Introduce subclasses for runtime relevant exceptions (NotN…
…ull, DuplicateKey, ForeignKey Constraint Violation).
ac27abe
@doctrinebot

Hello,

thank you for creating this pull request. I have automatically opened an issue
on our Jira Bug Tracker for you. See the issue link:

http://www.doctrine-project.org/jira/browse/DBAL-723

We use Jira to track the state of pull requests and the versions they got
included in.

@Ocramius Ocramius commented on the diff Dec 22, 2013
lib/Doctrine/DBAL/DBALException.php
@@ -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.
@Ocramius
Ocramius Dec 22, 2013

No return type?

@Ocramius Ocramius commented on the diff Dec 22, 2013
lib/Doctrine/DBAL/DBALException.php
+ /**
+ * 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:
@Ocramius
Ocramius Dec 22, 2013

Can simply skip the default

@beberlei
beberlei Dec 22, 2013

yes, but i feel this is more explicit

@deeky666 deeky666 commented on the diff Dec 22, 2013
lib/Doctrine/DBAL/DBALException.php
@@ -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)
@deeky666
deeky666 Dec 22, 2013

Just for my personal interest. Is there a reason surround that condition with braces? I have seen that a lot of times now but I don't know the purpose.

@beberlei
beberlei Dec 22, 2013

i guess readability which is important for ternary operations (as they are inheritly unreadable)

@deeky666 deeky666 commented on an outdated diff Dec 22, 2013
lib/Doctrine/DBAL/Exception/DuplicateKeyException.php
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the MIT license. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\DBAL\Exception;
+
+use Doctrine\DBAL\DBALException;
+
+/**
+ * Thrown when {@link DBALException::ERROR_DUPLICATE_KEY} is detected in driver
+ */
@deeky666
deeky666 Dec 22, 2013

Don't you want to add the @since tag?

@deeky666
Doctrine member

Looks good. I think we should document this feature, people will like it :)

@beberlei beberlei merged commit 6ce265d into master Dec 22, 2013
@beberlei beberlei deleted the DBAL-722 branch Dec 22, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment