diff --git a/lib/Doctrine/DBAL/DBALException.php b/lib/Doctrine/DBAL/DBALException.php index f7867432e1d..59faff0a8be 100644 --- a/lib/Doctrine/DBAL/DBALException.php +++ b/lib/Doctrine/DBAL/DBALException.php @@ -19,7 +19,8 @@ namespace Doctrine\DBAL; -use Doctrine\DBAL\Driver\DriverException; +use Doctrine\DBAL\Exception; +use Doctrine\DBAL\Driver; use Doctrine\DBAL\Driver\ExceptionConverterDriver; class DBALException extends \Exception @@ -112,11 +113,7 @@ public static function driverExceptionDuringQuery(Driver $driver, \Exception $dr } $msg .= ":\n\n".$driverEx->getMessage(); - if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverException) { - return $driver->convertException($msg, $driverEx); - } - - return new self($msg, 0, $driverEx); + return static::wrapException($driver, $driverEx, $msg); } /** @@ -127,9 +124,21 @@ public static function driverExceptionDuringQuery(Driver $driver, \Exception $dr */ public static function driverException(Driver $driver, \Exception $driverEx) { - $msg = "An exception occurred in driver: " . $driverEx->getMessage(); + return static::wrapException($driver, $driverEx, "An exception occurred in driver: " . $driverEx->getMessage()); + } - if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverException) { + /** + * @param \Doctrine\DBAL\Driver $driver + * @param \Exception $driverEx + * + * @return \Doctrine\DBAL\DBALException + */ + private static function wrapException(Driver $driver, \Exception $driverEx, $msg) + { + if ($driverEx instanceof Exception\DriverException) { + return $driverEx; + } + if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof Driver\DriverException) { return $driver->convertException($msg, $driverEx); } diff --git a/tests/Doctrine/Tests/DBAL/DBALExceptionTest.php b/tests/Doctrine/Tests/DBAL/DBALExceptionTest.php index 199fb91b251..18d7012e2f8 100644 --- a/tests/Doctrine/Tests/DBAL/DBALExceptionTest.php +++ b/tests/Doctrine/Tests/DBAL/DBALExceptionTest.php @@ -3,6 +3,7 @@ namespace Doctrine\Tests\DBAL; use Doctrine\DBAL\DBALException; +use Doctrine\DBAL\Exception\DriverException; class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase { @@ -12,4 +13,12 @@ public function testDriverExceptionDuringQueryAcceptsBinaryData() $e = DBALException::driverExceptionDuringQuery($driver, new \Exception, '', array('ABC', chr(128))); $this->assertContains('with params ["ABC", "\x80"]', $e->getMessage()); } + + public function testAvoidOverWrappingOnDriverException() + { + $driver = $this->getMock('\Doctrine\DBAL\Driver'); + $ex = new DriverException('', $this->getMock('\Doctrine\DBAL\Driver\DriverException')); + $e = DBALException::driverExceptionDuringQuery($driver, $ex, ''); + $this->assertSame($ex, $e); + } }