Permalink
Browse files

DBAL-371 Added \Doctrine\DBAL\Driver\DriverException with exception code

  • Loading branch information...
mnapoli committed Dec 14, 2012
1 parent f0a9b8b commit 8ca572b0d7a1346a8a818da23fd59b47310f1b81
@@ -24,6 +24,7 @@
Doctrine\DBAL\Driver\Connection as DriverConnection,
Doctrine\Common\EventManager,
Doctrine\DBAL\DBALException,
+ Doctrine\DBAL\Driver\DriverException,
Doctrine\DBAL\Cache\ResultCacheStatement,
Doctrine\DBAL\Cache\QueryCacheProfile,
Doctrine\DBAL\Cache\ArrayStatement,
@@ -597,7 +598,7 @@ public function prepare($statement)
try {
$stmt = new Statement($statement, $this);
} catch (\Exception $ex) {
- throw DBALException::driverExceptionDuringQuery($ex, $statement);
+ throw DriverException::driverExceptionDuringQuery($ex, $statement);
}
$stmt->setFetchMode($this->_defaultFetchMode);
@@ -646,7 +647,7 @@ public function executeQuery($query, array $params = array(), $types = array(),
$stmt = $this->_conn->query($query);
}
} catch (\Exception $ex) {
- throw DBALException::driverExceptionDuringQuery($ex, $query, $this->resolveParams($params, $types));
+ throw DriverException::driverExceptionDuringQuery($ex, $query, $this->resolveParams($params, $types));
}
$stmt->setFetchMode($this->_defaultFetchMode);
@@ -741,7 +742,7 @@ public function query()
try {
$statement = call_user_func_array(array($this->_conn, 'query'), $args);
} catch (\Exception $ex) {
- throw DBALException::driverExceptionDuringQuery($ex, func_get_arg(0));
+ throw DriverException::driverExceptionDuringQuery($ex, func_get_arg(0));
}
$statement->setFetchMode($this->_defaultFetchMode);
@@ -790,7 +791,7 @@ public function executeUpdate($query, array $params = array(), array $types = ar
$result = $this->_conn->exec($query);
}
} catch (\Exception $ex) {
- throw DBALException::driverExceptionDuringQuery($ex, $query, $this->resolveParams($params, $types));
+ throw DriverException::driverExceptionDuringQuery($ex, $query, $this->resolveParams($params, $types));
}
if ($logger) {
@@ -818,7 +819,7 @@ public function exec($statement)
try {
$result = $this->_conn->exec($statement);
} catch (\Exception $ex) {
- throw DBALException::driverExceptionDuringQuery($ex, $statement);
+ throw DriverException::driverExceptionDuringQuery($ex, $statement);
}
if ($logger) {
@@ -44,7 +44,7 @@ public static function driverExceptionDuringQuery(\Exception $driverEx, $sql, ar
}
$msg .= ":\n\n".$driverEx->getMessage();
- return new self($msg, (int) $driverEx->getCode(), $driverEx);
+ return new self($msg, 0, $driverEx);
}
public static function invalidWrapperClass($wrapperClass)
@@ -0,0 +1,19 @@
+<?php
+
+namespace Doctrine\DBAL\Driver;
+
+use Doctrine\DBAL\DBALException;
+
+class DriverException extends DBALException
+{
+ public static function driverExceptionDuringQuery(\Exception $driverEx, $sql, array $params = array())
+ {
+ $msg = "An exception occurred while executing '".$sql."'";
+ if ($params) {
+ $msg .= " with params ".json_encode($params);
+ }
+ $msg .= ":\n\n".$driverEx->getMessage();
+
+ return new self($msg, (int) $driverEx->getCode(), $driverEx);
+ }
+}
@@ -23,7 +23,8 @@
use PDO,
Doctrine\DBAL\Types\Type,
- Doctrine\DBAL\Driver\Statement as DriverStatement;
+ Doctrine\DBAL\Driver\Statement as DriverStatement,
+ Doctrine\DBAL\Driver\DriverException;
/**
* A thin wrapper around a Doctrine\DBAL\Driver\Statement that adds support
@@ -141,7 +142,7 @@ public function execute($params = null)
try {
$stmt = $this->stmt->execute($params);
} catch (\Exception $ex) {
- throw DBALException::driverExceptionDuringQuery($ex, $this->sql, $this->conn->resolveParams($this->params, $this->types));
+ throw DriverException::driverExceptionDuringQuery($ex, $this->sql, $this->conn->resolveParams($this->params, $this->types));
}
if ($logger) {
@@ -3,6 +3,7 @@
namespace Doctrine\Tests\DBAL\Functional\Ticket;
use Doctrine\DBAL\DBALException;
+use Doctrine\DBAL\Driver\DriverException;
/**
* @group DBAL-371
@@ -24,6 +25,17 @@ protected function setUp()
}
}
+ /**
+ * @expectedException \Doctrine\DBAL\Driver\DriverException
+ */
+ public function testException()
+ {
+ $stmt = $this->_conn->prepare('INSERT INTO DBAL371 VALUES (1)');
+ $this->_conn->beginTransaction();
+ $stmt->execute();
+ $stmt->execute();
+ }
+
public function testExceptionCode()
{
$stmt = $this->_conn->prepare('INSERT INTO DBAL371 VALUES (1)');

0 comments on commit 8ca572b

Please sign in to comment.