Skip to content

Commit e46c09b

Browse files
authored
Merge pull request #3525 from doctrine/exceptions
Extract exception factory methods into specific exceptions
2 parents 55bd22c + 68e48e8 commit e46c09b

File tree

113 files changed

+1451
-811
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

113 files changed

+1451
-811
lines changed

lib/Doctrine/DBAL/Cache/ArrayStatement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
namespace Doctrine\DBAL\Cache;
66

77
use ArrayIterator;
8-
use Doctrine\DBAL\DBALException;
98
use Doctrine\DBAL\Driver\ResultStatement;
9+
use Doctrine\DBAL\Exception\InvalidColumnIndex;
1010
use Doctrine\DBAL\FetchMode;
1111
use InvalidArgumentException;
1212
use IteratorAggregate;
@@ -149,7 +149,7 @@ public function fetchColumn($columnIndex = 0)
149149
}
150150

151151
if (! array_key_exists($columnIndex, $row)) {
152-
throw DBALException::invalidColumnIndex($columnIndex, count($row));
152+
throw InvalidColumnIndex::new($columnIndex, count($row));
153153
}
154154

155155
return $row[$columnIndex];

lib/Doctrine/DBAL/Cache/CacheException.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,4 @@
88

99
class CacheException extends DBALException
1010
{
11-
/**
12-
* @return \Doctrine\DBAL\Cache\CacheException
13-
*/
14-
public static function noCacheKey()
15-
{
16-
return new self('No cache key was set.');
17-
}
18-
19-
/**
20-
* @return \Doctrine\DBAL\Cache\CacheException
21-
*/
22-
public static function noResultDriverConfigured()
23-
{
24-
return new self('Trying to cache a query but no result driver is configured.');
25-
}
2611
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Cache\Exception;
6+
7+
use Doctrine\DBAL\Cache\CacheException;
8+
9+
final class NoCacheKey extends CacheException
10+
{
11+
public static function new() : self
12+
{
13+
return new self('No cache key was set.');
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Cache\Exception;
6+
7+
use Doctrine\DBAL\Cache\CacheException;
8+
9+
final class NoResultDriverConfigured extends CacheException
10+
{
11+
public static function new() : self
12+
{
13+
return new self('Trying to cache a query but no result driver is configured.');
14+
}
15+
}

lib/Doctrine/DBAL/Cache/QueryCacheProfile.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Doctrine\DBAL\Cache;
66

77
use Doctrine\Common\Cache\Cache;
8+
use Doctrine\DBAL\Cache\Exception\NoCacheKey;
89
use function hash;
910
use function serialize;
1011
use function sha1;
@@ -60,7 +61,7 @@ public function getLifetime()
6061
public function getCacheKey()
6162
{
6263
if ($this->cacheKey === null) {
63-
throw CacheException::noCacheKey();
64+
throw NoCacheKey::new();
6465
}
6566

6667
return $this->cacheKey;

lib/Doctrine/DBAL/Cache/ResultCacheStatement.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
use ArrayIterator;
88
use Doctrine\Common\Cache\Cache;
9-
use Doctrine\DBAL\DBALException;
109
use Doctrine\DBAL\Driver\ResultStatement;
10+
use Doctrine\DBAL\Exception\InvalidColumnIndex;
1111
use Doctrine\DBAL\FetchMode;
1212
use InvalidArgumentException;
1313
use IteratorAggregate;
@@ -184,7 +184,7 @@ public function fetchColumn($columnIndex = 0)
184184
}
185185

186186
if (! array_key_exists($columnIndex, $row)) {
187-
throw DBALException::invalidColumnIndex($columnIndex, count($row));
187+
throw InvalidColumnIndex::new($columnIndex, count($row));
188188
}
189189

190190
return $row[$columnIndex];

lib/Doctrine/DBAL/Connection.php

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Doctrine\Common\EventManager;
99
use Doctrine\DBAL\Cache\ArrayStatement;
1010
use Doctrine\DBAL\Cache\CacheException;
11+
use Doctrine\DBAL\Cache\Exception\NoResultDriverConfigured;
1112
use Doctrine\DBAL\Cache\QueryCacheProfile;
1213
use Doctrine\DBAL\Cache\ResultCacheStatement;
1314
use Doctrine\DBAL\Driver\Connection as DriverConnection;
@@ -16,7 +17,13 @@
1617
use Doctrine\DBAL\Driver\ResultStatement;
1718
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
1819
use Doctrine\DBAL\Driver\Statement as DriverStatement;
20+
use Doctrine\DBAL\Exception\CommitFailedRollbackOnly;
21+
use Doctrine\DBAL\Exception\EmptyCriteriaNotAllowed;
1922
use Doctrine\DBAL\Exception\InvalidArgumentException;
23+
use Doctrine\DBAL\Exception\InvalidPlatformType;
24+
use Doctrine\DBAL\Exception\MayNotAlterNestedTransactionWithSavepointsInTransaction;
25+
use Doctrine\DBAL\Exception\NoActiveTransaction;
26+
use Doctrine\DBAL\Exception\SavepointsNotSupported;
2027
use Doctrine\DBAL\Platforms\AbstractPlatform;
2128
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
2229
use Doctrine\DBAL\Query\QueryBuilder;
@@ -198,7 +205,7 @@ public function __construct(
198205

199206
if (isset($params['platform'])) {
200207
if (! $params['platform'] instanceof Platforms\AbstractPlatform) {
201-
throw DBALException::invalidPlatformType($params['platform']);
208+
throw InvalidPlatformType::new($params['platform']);
202209
}
203210

204211
$this->platform = $params['platform'];
@@ -642,7 +649,7 @@ private function addIdentifierCondition(
642649
public function delete($tableExpression, array $identifier, array $types = [])
643650
{
644651
if (empty($identifier)) {
645-
throw InvalidArgumentException::fromEmptyCriteria();
652+
throw EmptyCriteriaNotAllowed::new();
646653
}
647654

648655
$columns = $values = $conditions = [];
@@ -915,7 +922,7 @@ public function executeCacheQuery($query, $params, $types, QueryCacheProfile $qc
915922
$resultCache = $qcp->getResultCacheDriver() ?? $this->_config->getResultCacheImpl();
916923

917924
if ($resultCache === null) {
918-
throw CacheException::noResultDriverConfigured();
925+
throw NoResultDriverConfigured::new();
919926
}
920927

921928
[$cacheKey, $realKey] = $qcp->generateCacheKeys($query, $params, $types, $this->getParams());
@@ -1122,11 +1129,11 @@ public function transactional(Closure $func)
11221129
public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
11231130
{
11241131
if ($this->transactionNestingLevel > 0) {
1125-
throw ConnectionException::mayNotAlterNestedTransactionWithSavepointsInTransaction();
1132+
throw MayNotAlterNestedTransactionWithSavepointsInTransaction::new();
11261133
}
11271134

11281135
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
1129-
throw ConnectionException::savepointsNotSupported();
1136+
throw SavepointsNotSupported::new();
11301137
}
11311138

11321139
$this->nestTransactionsWithSavepoints = (bool) $nestTransactionsWithSavepoints;
@@ -1188,11 +1195,10 @@ public function beginTransaction() : void
11881195
public function commit() : void
11891196
{
11901197
if ($this->transactionNestingLevel === 0) {
1191-
throw ConnectionException::noActiveTransaction();
1198+
throw NoActiveTransaction::new();
11921199
}
1193-
11941200
if ($this->isRollbackOnly) {
1195-
throw ConnectionException::commitFailedRollbackOnly();
1201+
throw CommitFailedRollbackOnly::new();
11961202
}
11971203

11981204
$connection = $this->getWrappedConnection();
@@ -1251,7 +1257,7 @@ private function commitAll() : void
12511257
public function rollBack() : void
12521258
{
12531259
if ($this->transactionNestingLevel === 0) {
1254-
throw ConnectionException::noActiveTransaction();
1260+
throw NoActiveTransaction::new();
12551261
}
12561262

12571263
$connection = $this->getWrappedConnection();
@@ -1295,7 +1301,7 @@ public function rollBack() : void
12951301
public function createSavepoint($savepoint)
12961302
{
12971303
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
1298-
throw ConnectionException::savepointsNotSupported();
1304+
throw SavepointsNotSupported::new();
12991305
}
13001306

13011307
$this->getWrappedConnection()->exec($this->platform->createSavePoint($savepoint));
@@ -1313,7 +1319,7 @@ public function createSavepoint($savepoint)
13131319
public function releaseSavepoint($savepoint)
13141320
{
13151321
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
1316-
throw ConnectionException::savepointsNotSupported();
1322+
throw SavepointsNotSupported::new();
13171323
}
13181324

13191325
if (! $this->platform->supportsReleaseSavepoints()) {
@@ -1335,7 +1341,7 @@ public function releaseSavepoint($savepoint)
13351341
public function rollbackSavepoint($savepoint)
13361342
{
13371343
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
1338-
throw ConnectionException::savepointsNotSupported();
1344+
throw SavepointsNotSupported::new();
13391345
}
13401346

13411347
$this->getWrappedConnection()->exec($this->platform->rollbackSavePoint($savepoint));
@@ -1380,7 +1386,7 @@ public function getSchemaManager()
13801386
public function setRollbackOnly()
13811387
{
13821388
if ($this->transactionNestingLevel === 0) {
1383-
throw ConnectionException::noActiveTransaction();
1389+
throw NoActiveTransaction::new();
13841390
}
13851391
$this->isRollbackOnly = true;
13861392
}
@@ -1395,7 +1401,7 @@ public function setRollbackOnly()
13951401
public function isRollbackOnly()
13961402
{
13971403
if ($this->transactionNestingLevel === 0) {
1398-
throw ConnectionException::noActiveTransaction();
1404+
throw NoActiveTransaction::new();
13991405
}
14001406

14011407
return $this->isRollbackOnly;

lib/Doctrine/DBAL/ConnectionException.php

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,4 @@
66

77
class ConnectionException extends DBALException
88
{
9-
/**
10-
* @return \Doctrine\DBAL\ConnectionException
11-
*/
12-
public static function commitFailedRollbackOnly()
13-
{
14-
return new self('Transaction commit failed because the transaction has been marked for rollback only.');
15-
}
16-
17-
/**
18-
* @return \Doctrine\DBAL\ConnectionException
19-
*/
20-
public static function noActiveTransaction()
21-
{
22-
return new self('There is no active transaction.');
23-
}
24-
25-
/**
26-
* @return \Doctrine\DBAL\ConnectionException
27-
*/
28-
public static function savepointsNotSupported()
29-
{
30-
return new self('Savepoints are not supported by this driver.');
31-
}
32-
33-
/**
34-
* @return \Doctrine\DBAL\ConnectionException
35-
*/
36-
public static function mayNotAlterNestedTransactionWithSavepointsInTransaction()
37-
{
38-
return new self('May not alter the nested transaction with savepoints behavior while a transaction is open.');
39-
}
409
}

0 commit comments

Comments
 (0)