Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

DBAL-371: Forward the SQLSTATE error code to DBALException #221

Closed
wants to merge 10 commits into
from
@@ -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) {
@@ -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();
@Baachi

Baachi Dec 14, 2012

Newlines in an exception message? Really?

@mnapoli

mnapoli Dec 14, 2012

Contributor

erm, this is an exact copy of the same method in DBALException: https://github.com/doctrine/dbal/blob/master/lib/Doctrine/DBAL/DBALException.php#L39

+
+ 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) {
@@ -0,0 +1,41 @@
+<?php
+
+namespace Doctrine\Tests\DBAL\Functional\Ticket;
+
+use Doctrine\DBAL\DBALException;
+use Doctrine\DBAL\Driver\DriverException;
+
+/**
+ * @group DBAL-371
+ */
+class DBAL371Test extends \Doctrine\Tests\DbalFunctionalTestCase
+{
+ protected function setUp()
+ {
+ parent::setUp();
+
+ if ($this->_conn->getSchemaManager()->tablesExist('dbal371')) {
+ $this->_conn->getSchemaManager()->dropTable('dbal371');
+ }
+ $table = new \Doctrine\DBAL\Schema\Table('dbal371');
+ $table->addColumn('id', 'integer');
+ $table->setPrimaryKey(array('id'));
+
+ $this->_conn->getSchemaManager()->createTable($table);
+ }
+
+ public function testExceptionCode()
+ {
+ $stmt = $this->_conn->prepare('INSERT INTO dbal371 VALUES (1)');
+ $stmt->execute();
+ $exception = false;
+ try {
+ $stmt->execute();
+ } catch(DBALException $e) {
+ $exception = true;
+ $this->assertInstanceOf('Doctrine\DBAL\Driver\DriverException', $e);
+ $this->assertGreaterThan(0, $e->getCode());
+ }
+ $this->assertTrue($exception);
+ }
+}