Skip to content

Commit 5ee9443

Browse files
committed
Merge pull request #3525 from doctrine/exceptions
Extract exception factory methods into specific exceptions
2 parents dd69a29 + 661a922 commit 5ee9443

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

+2-2
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

-15
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
}
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+
}
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

+2-1
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

+2-2
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

+20-14
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'];
@@ -641,7 +648,7 @@ private function addIdentifierCondition(
641648
public function delete($tableExpression, array $identifier, array $types = [])
642649
{
643650
if (empty($identifier)) {
644-
throw InvalidArgumentException::fromEmptyCriteria();
651+
throw EmptyCriteriaNotAllowed::new();
645652
}
646653

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

916923
if ($resultCache === null) {
917-
throw CacheException::noResultDriverConfigured();
924+
throw NoResultDriverConfigured::new();
918925
}
919926

920927
$connectionParams = $this->getParams();
@@ -1124,11 +1131,11 @@ public function transactional(Closure $func)
11241131
public function setNestTransactionsWithSavepoints($nestTransactionsWithSavepoints)
11251132
{
11261133
if ($this->transactionNestingLevel > 0) {
1127-
throw ConnectionException::mayNotAlterNestedTransactionWithSavepointsInTransaction();
1134+
throw MayNotAlterNestedTransactionWithSavepointsInTransaction::new();
11281135
}
11291136

11301137
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
1131-
throw ConnectionException::savepointsNotSupported();
1138+
throw SavepointsNotSupported::new();
11321139
}
11331140

11341141
$this->nestTransactionsWithSavepoints = (bool) $nestTransactionsWithSavepoints;
@@ -1190,11 +1197,10 @@ public function beginTransaction() : void
11901197
public function commit() : void
11911198
{
11921199
if ($this->transactionNestingLevel === 0) {
1193-
throw ConnectionException::noActiveTransaction();
1200+
throw NoActiveTransaction::new();
11941201
}
1195-
11961202
if ($this->isRollbackOnly) {
1197-
throw ConnectionException::commitFailedRollbackOnly();
1203+
throw CommitFailedRollbackOnly::new();
11981204
}
11991205

12001206
$connection = $this->getWrappedConnection();
@@ -1253,7 +1259,7 @@ private function commitAll() : void
12531259
public function rollBack() : void
12541260
{
12551261
if ($this->transactionNestingLevel === 0) {
1256-
throw ConnectionException::noActiveTransaction();
1262+
throw NoActiveTransaction::new();
12571263
}
12581264

12591265
$connection = $this->getWrappedConnection();
@@ -1297,7 +1303,7 @@ public function rollBack() : void
12971303
public function createSavepoint($savepoint)
12981304
{
12991305
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
1300-
throw ConnectionException::savepointsNotSupported();
1306+
throw SavepointsNotSupported::new();
13011307
}
13021308

13031309
$this->getWrappedConnection()->exec($this->platform->createSavePoint($savepoint));
@@ -1315,7 +1321,7 @@ public function createSavepoint($savepoint)
13151321
public function releaseSavepoint($savepoint)
13161322
{
13171323
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
1318-
throw ConnectionException::savepointsNotSupported();
1324+
throw SavepointsNotSupported::new();
13191325
}
13201326

13211327
if (! $this->platform->supportsReleaseSavepoints()) {
@@ -1337,7 +1343,7 @@ public function releaseSavepoint($savepoint)
13371343
public function rollbackSavepoint($savepoint)
13381344
{
13391345
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
1340-
throw ConnectionException::savepointsNotSupported();
1346+
throw SavepointsNotSupported::new();
13411347
}
13421348

13431349
$this->getWrappedConnection()->exec($this->platform->rollbackSavePoint($savepoint));
@@ -1382,7 +1388,7 @@ public function getSchemaManager()
13821388
public function setRollbackOnly()
13831389
{
13841390
if ($this->transactionNestingLevel === 0) {
1385-
throw ConnectionException::noActiveTransaction();
1391+
throw NoActiveTransaction::new();
13861392
}
13871393
$this->isRollbackOnly = true;
13881394
}
@@ -1397,7 +1403,7 @@ public function setRollbackOnly()
13971403
public function isRollbackOnly()
13981404
{
13991405
if ($this->transactionNestingLevel === 0) {
1400-
throw ConnectionException::noActiveTransaction();
1406+
throw NoActiveTransaction::new();
14011407
}
14021408

14031409
return $this->isRollbackOnly;

lib/Doctrine/DBAL/ConnectionException.php

-31
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)