Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DBAL-1302 Avoid rewrapping Docrine\DBAL\Exception\DriverException with nested drivers #923

Merged
merged 1 commit into from
Dec 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions lib/Doctrine/DBAL/DBALException.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

Expand Down
9 changes: 9 additions & 0 deletions tests/Doctrine/Tests/DBAL/DBALExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Doctrine\Tests\DBAL;

use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception\DriverException;

class DBALExceptionTest extends \Doctrine\Tests\DbalTestCase
{
Expand All @@ -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);
}
}