Commit a6bc5a8a authored by Benjamin Eberlei's avatar Benjamin Eberlei

Fix Exception converting into a non-BC change.

parent d2a1d2a0
# Upgrade to 2.5
A new method was added to `Doctrine\DBAL\Driver` interface that you need to implement
if your project contains custom drivers.
```php
/**
* @param \Exception $exception
*
* @return int
*/
public function convertExceptionCode(\Exception $exception);
```
The most basic implementation for this method just returns 0:
```php
/**
* @param \Exception $exception
*
* @return int
*/
public function convertExceptionCode(\Exception $exception)
{
return 0;
}
```
You can see the MySQL, SQLite and PostgreSQL drivers for sample implementations with
the new feature and take a look at `tests/Doctrine/Tests/DBAL/Functional/ExceptionTest.php`
for testing this functionality.
No BC breaks yet.
# Upgrade to 2.4
......
......@@ -19,6 +19,8 @@
namespace Doctrine\DBAL;
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
class DBALException extends \Exception
{
const ERROR_DUPLICATE_KEY = 1;
......@@ -101,7 +103,11 @@ class DBALException extends \Exception
}
$msg .= ":\n\n".$driverEx->getMessage();
return new self($msg, $driver->convertExceptionCode($driverEx), $driverEx);
$code = ($driver instanceof ExceptionConverterDriver)
? $driver->convertExceptionCode($driverEx)
: 0;
return new self($msg, $code, $driverEx);
}
/**
......
......@@ -73,7 +73,6 @@ interface Driver
*/
public function getDatabase(Connection $conn);
/**
* @param \Exception $exception
*
......
<?php
/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/
namespace Doctrine\DBAL\Driver;
use Exception;
/**
* Drivers marked as ExceptionConverters can convert to standardized codes.
*
* @since 2.5
*/
interface ExceptionConverterDriver
{
/**
* @param \Exception $exception
*
* @return int
*/
public function convertExceptionCode(Exception $exception);
}
......@@ -22,11 +22,12 @@ namespace Doctrine\DBAL\Driver\Mysqli;
use Doctrine\DBAL\Driver as DriverInterface;
use Doctrine\DBAL\Driver\Mysqli\MysqliException;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
/**
* @author Kim Hemsø Rasmussen <kimhemsoe@gmail.com>
*/
class Driver implements DriverInterface
class Driver implements DriverInterface, ExceptionConverterDriver
{
/**
* {@inheritdoc}
......
......@@ -21,6 +21,7 @@ namespace Doctrine\DBAL\Driver\PDOMySql;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
use PDOException;
/**
......@@ -28,7 +29,7 @@ use PDOException;
*
* @since 2.0
*/
class Driver implements \Doctrine\DBAL\Driver
class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
{
/**
* {@inheritdoc}
......
......@@ -22,13 +22,14 @@ namespace Doctrine\DBAL\Driver\PDOPgSql;
use Doctrine\DBAL\Platforms;
use Doctrine\DBAL\DBALException;
use PDOException;
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
/**
* Driver that connects through pdo_pgsql.
*
* @since 2.0
*/
class Driver implements \Doctrine\DBAL\Driver
class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
{
/**
* {@inheritdoc}
......
......@@ -20,6 +20,7 @@
namespace Doctrine\DBAL\Driver\PDOSqlite;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
use PDOException;
/**
......@@ -27,7 +28,7 @@ use PDOException;
*
* @since 2.0
*/
class Driver implements \Doctrine\DBAL\Driver
class Driver implements \Doctrine\DBAL\Driver, ExceptionConverterDriver
{
/**
* @var array
......
......@@ -2,6 +2,7 @@
namespace Doctrine\Tests\DBAL\Functional;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ExceptionConverterDriver;
class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
......@@ -9,10 +10,7 @@ class ExceptionTest extends \Doctrine\Tests\DbalFunctionalTestCase
{
parent::setUp();
$supportExceptions = array('pdo_sqlite', 'pdo_mysql', 'pdo_pgsql', 'mysqli');
$params = $this->_conn->getParams();
if (!in_array($params['driver'], $supportExceptions)) {
if ( !($this->_conn->getDriver() instanceof ExceptionConverterDriver)) {
$this->markTestSkipped('Driver does not support special exception handling.');
}
}
......
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