Skip to content

Commit

Permalink
Merge pull request #3525 from doctrine/exceptions
Browse files Browse the repository at this point in the history
Extract exception factory methods into specific exceptions
  • Loading branch information
morozov committed May 23, 2019
2 parents d007efe + 91159d1 commit 5494b34
Show file tree
Hide file tree
Showing 113 changed files with 1,451 additions and 811 deletions.
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/Cache/ArrayStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
namespace Doctrine\DBAL\Cache;

use ArrayIterator;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Exception\InvalidColumnIndex;
use Doctrine\DBAL\FetchMode;
use InvalidArgumentException;
use IteratorAggregate;
Expand Down Expand Up @@ -149,7 +149,7 @@ public function fetchColumn($columnIndex = 0)
}

if (! array_key_exists($columnIndex, $row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row));
throw InvalidColumnIndex::new($columnIndex, count($row));
}

return $row[$columnIndex];
Expand Down
15 changes: 0 additions & 15 deletions lib/Doctrine/DBAL/Cache/CacheException.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,4 @@

class CacheException extends DBALException
{
/**
* @return \Doctrine\DBAL\Cache\CacheException
*/
public static function noCacheKey()
{
return new self('No cache key was set.');
}

/**
* @return \Doctrine\DBAL\Cache\CacheException
*/
public static function noResultDriverConfigured()
{
return new self('Trying to cache a query but no result driver is configured.');
}
}
15 changes: 15 additions & 0 deletions lib/Doctrine/DBAL/Cache/Exception/NoCacheKey.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Cache\Exception;

use Doctrine\DBAL\Cache\CacheException;

final class NoCacheKey extends CacheException
{
public static function new() : self
{
return new self('No cache key was set.');
}
}
15 changes: 15 additions & 0 deletions lib/Doctrine/DBAL/Cache/Exception/NoResultDriverConfigured.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Cache\Exception;

use Doctrine\DBAL\Cache\CacheException;

final class NoResultDriverConfigured extends CacheException
{
public static function new() : self
{
return new self('Trying to cache a query but no result driver is configured.');
}
}
3 changes: 2 additions & 1 deletion lib/Doctrine/DBAL/Cache/QueryCacheProfile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Doctrine\DBAL\Cache;

use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\Cache\Exception\NoCacheKey;
use function hash;
use function serialize;
use function sha1;
Expand Down Expand Up @@ -60,7 +61,7 @@ public function getLifetime()
public function getCacheKey()
{
if ($this->cacheKey === null) {
throw CacheException::noCacheKey();
throw NoCacheKey::new();
}

return $this->cacheKey;
Expand Down
4 changes: 2 additions & 2 deletions lib/Doctrine/DBAL/Cache/ResultCacheStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

use ArrayIterator;
use Doctrine\Common\Cache\Cache;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Exception\InvalidColumnIndex;
use Doctrine\DBAL\FetchMode;
use InvalidArgumentException;
use IteratorAggregate;
Expand Down Expand Up @@ -184,7 +184,7 @@ public function fetchColumn($columnIndex = 0)
}

if (! array_key_exists($columnIndex, $row)) {
throw DBALException::invalidColumnIndex($columnIndex, count($row));
throw InvalidColumnIndex::new($columnIndex, count($row));
}

return $row[$columnIndex];
Expand Down
34 changes: 20 additions & 14 deletions lib/Doctrine/DBAL/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Cache\ArrayStatement;
use Doctrine\DBAL\Cache\CacheException;
use Doctrine\DBAL\Cache\Exception\NoResultDriverConfigured;
use Doctrine\DBAL\Cache\QueryCacheProfile;
use Doctrine\DBAL\Cache\ResultCacheStatement;
use Doctrine\DBAL\Driver\Connection as DriverConnection;
Expand All @@ -16,7 +17,13 @@
use Doctrine\DBAL\Driver\ResultStatement;
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
use Doctrine\DBAL\Driver\Statement as DriverStatement;
use Doctrine\DBAL\Exception\CommitFailedRollbackOnly;
use Doctrine\DBAL\Exception\EmptyCriteriaNotAllowed;
use Doctrine\DBAL\Exception\InvalidArgumentException;
use Doctrine\DBAL\Exception\InvalidPlatformType;
use Doctrine\DBAL\Exception\MayNotAlterNestedTransactionWithSavepointsInTransaction;
use Doctrine\DBAL\Exception\NoActiveTransaction;
use Doctrine\DBAL\Exception\SavepointsNotSupported;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
use Doctrine\DBAL\Query\QueryBuilder;
Expand Down Expand Up @@ -198,7 +205,7 @@ public function __construct(

if (isset($params['platform'])) {
if (! $params['platform'] instanceof Platforms\AbstractPlatform) {
throw DBALException::invalidPlatformType($params['platform']);
throw InvalidPlatformType::new($params['platform']);
}

$this->platform = $params['platform'];
Expand Down Expand Up @@ -641,7 +648,7 @@ private function addIdentifierCondition(
public function delete($tableExpression, array $identifier, array $types = [])
{
if (empty($identifier)) {
throw InvalidArgumentException::fromEmptyCriteria();
throw EmptyCriteriaNotAllowed::new();
}

$columns = $values = $conditions = [];
Expand Down Expand Up @@ -914,7 +921,7 @@ public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qc
$resultCache = $qcp->getResultCacheDriver() ?? $this->_config->getResultCacheImpl();

if ($resultCache === null) {
throw CacheException::noResultDriverConfigured();
throw NoResultDriverConfigured::new();
}

$connectionParams = $this->getParams();
Expand Down Expand Up @@ -1124,11 +1131,11 @@ public function transactional(Closure $func)
public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
{
if ($this->transactionNestingLevel > 0) {
throw ConnectionException::mayNotAlterNestedTransactionWithSavepointsInTransaction();
throw MayNotAlterNestedTransactionWithSavepointsInTransaction::new();
}

if (! $this->getDatabasePlatform()->supportsSavepoints()) {
throw ConnectionException::savepointsNotSupported();
throw SavepointsNotSupported::new();
}

$this->nestTransactionsWithSavepoints = (bool) $nestTransactionsWithSavepoints;
Expand Down Expand Up @@ -1190,11 +1197,10 @@ public function beginTransaction() : void
public function commit() : void
{
if ($this->transactionNestingLevel === 0) {
throw ConnectionException::noActiveTransaction();
throw NoActiveTransaction::new();
}

if ($this->isRollbackOnly) {
throw ConnectionException::commitFailedRollbackOnly();
throw CommitFailedRollbackOnly::new();
}

$connection = $this->getWrappedConnection();
Expand Down Expand Up @@ -1253,7 +1259,7 @@ private function commitAll() : void
public function rollBack() : void
{
if ($this->transactionNestingLevel === 0) {
throw ConnectionException::noActiveTransaction();
throw NoActiveTransaction::new();
}

$connection = $this->getWrappedConnection();
Expand Down Expand Up @@ -1297,7 +1303,7 @@ public function rollBack() : void
public function createSavepoint($savepoint)
{
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
throw ConnectionException::savepointsNotSupported();
throw SavepointsNotSupported::new();
}

$this->getWrappedConnection()->exec($this->platform->createSavePoint($savepoint));
Expand All @@ -1315,7 +1321,7 @@ public function createSavepoint($savepoint)
public function releaseSavepoint($savepoint)
{
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
throw ConnectionException::savepointsNotSupported();
throw SavepointsNotSupported::new();
}

if (! $this->platform->supportsReleaseSavepoints()) {
Expand All @@ -1337,7 +1343,7 @@ public function releaseSavepoint($savepoint)
public function rollbackSavepoint($savepoint)
{
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
throw ConnectionException::savepointsNotSupported();
throw SavepointsNotSupported::new();
}

$this->getWrappedConnection()->exec($this->platform->rollbackSavePoint($savepoint));
Expand Down Expand Up @@ -1381,7 +1387,7 @@ public function getSchemaManager()
public function setRollbackOnly()
{
if ($this->transactionNestingLevel === 0) {
throw ConnectionException::noActiveTransaction();
throw NoActiveTransaction::new();
}
$this->isRollbackOnly = true;
}
Expand All @@ -1396,7 +1402,7 @@ public function setRollbackOnly()
public function isRollbackOnly()
{
if ($this->transactionNestingLevel === 0) {
throw ConnectionException::noActiveTransaction();
throw NoActiveTransaction::new();
}

return $this->isRollbackOnly;
Expand Down
31 changes: 0 additions & 31 deletions lib/Doctrine/DBAL/ConnectionException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,4 @@

class ConnectionException extends DBALException
{
/**
* @return \Doctrine\DBAL\ConnectionException
*/
public static function commitFailedRollbackOnly()
{
return new self('Transaction commit failed because the transaction has been marked for rollback only.');
}

/**
* @return \Doctrine\DBAL\ConnectionException
*/
public static function noActiveTransaction()
{
return new self('There is no active transaction.');
}

/**
* @return \Doctrine\DBAL\ConnectionException
*/
public static function savepointsNotSupported()
{
return new self('Savepoints are not supported by this driver.');
}

/**
* @return \Doctrine\DBAL\ConnectionException
*/
public static function mayNotAlterNestedTransactionWithSavepointsInTransaction()
{
return new self('May not alter the nested transaction with savepoints behavior while a transaction is open.');
}
}
Loading

0 comments on commit 5494b34

Please sign in to comment.