Commit 069280e5 authored by Steve Müller's avatar Steve Müller

Merge pull request #923 from mathroc/avoid-over-nesting-exceptions

Avoid rewrapping Docrine\DBAL\Exception\DriverException with nested drivers
parents bfee907d 8a3a81f9
...@@ -19,7 +19,8 @@ ...@@ -19,7 +19,8 @@
namespace Doctrine\DBAL; namespace Doctrine\DBAL;
use Doctrine\DBAL\Driver\DriverException; use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\ExceptionConverterDriver; use Doctrine\DBAL\Driver\ExceptionConverterDriver;
class DBALException extends \Exception class DBALException extends \Exception
...@@ -112,11 +113,7 @@ class DBALException extends \Exception ...@@ -112,11 +113,7 @@ class DBALException extends \Exception
} }
$msg .= ":\n\n".$driverEx->getMessage(); $msg .= ":\n\n".$driverEx->getMessage();
if ($driver instanceof ExceptionConverterDriver && $driverEx instanceof DriverException) { return static::wrapException($driver, $driverEx, $msg);
return $driver->convertException($msg, $driverEx);
}
return new self($msg, 0, $driverEx);
} }
/** /**
...@@ -127,9 +124,21 @@ class DBALException extends \Exception ...@@ -127,9 +124,21 @@ class DBALException extends \Exception
*/ */
public static function driverException(Driver $driver, \Exception $driverEx) 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); return $driver->convertException($msg, $driverEx);
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
namespace Doctrine\Tests\DBAL; namespace Doctrine\Tests\DBAL;
use Doctrine\DBAL\DBALException; use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception\DriverException;
class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase
{ {
...@@ -12,4 +13,12 @@ class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase ...@@ -12,4 +13,12 @@ class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase
$e = DBALException::driverExceptionDuringQuery($driver, new \Exception, '', array('ABC', chr(128))); $e = DBALException::driverExceptionDuringQuery($driver, new \Exception, '', array('ABC', chr(128)));
$this->assertContains('with params ["ABC", "\x80"]', $e->getMessage()); $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);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment