Skip to content

Commit e38ed95

Browse files
committed
Merge pull request #3525 from doctrine/exceptions
Extract exception factory methods into specific exceptions
2 parents fb94661 + 0d8b791 commit e38ed95

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'];
@@ -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
$result = true;
@@ -1255,7 +1261,7 @@ private function commitAll() : void
12551261
public function rollBack() : void
12561262
{
12571263
if ($this->transactionNestingLevel === 0) {
1258-
throw ConnectionException::noActiveTransaction();
1264+
throw NoActiveTransaction::new();
12591265
}
12601266

12611267
$connection = $this->getWrappedConnection();
@@ -1299,7 +1305,7 @@ public function rollBack() : void
12991305
public function createSavepoint($savepoint)
13001306
{
13011307
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
1302-
throw ConnectionException::savepointsNotSupported();
1308+
throw SavepointsNotSupported::new();
13031309
}
13041310

13051311
$this->getWrappedConnection()->exec($this->platform->createSavePoint($savepoint));
@@ -1317,7 +1323,7 @@ public function createSavepoint($savepoint)
13171323
public function releaseSavepoint($savepoint)
13181324
{
13191325
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
1320-
throw ConnectionException::savepointsNotSupported();
1326+
throw SavepointsNotSupported::new();
13211327
}
13221328

13231329
if (! $this->platform->supportsReleaseSavepoints()) {
@@ -1339,7 +1345,7 @@ public function releaseSavepoint($savepoint)
13391345
public function rollbackSavepoint($savepoint)
13401346
{
13411347
if (! $this->getDatabasePlatform()->supportsSavepoints()) {
1342-
throw ConnectionException::savepointsNotSupported();
1348+
throw SavepointsNotSupported::new();
13431349
}
13441350

13451351
$this->getWrappedConnection()->exec($this->platform->rollbackSavePoint($savepoint));
@@ -1383,7 +1389,7 @@ public function getSchemaManager()
13831389
public function setRollbackOnly()
13841390
{
13851391
if ($this->transactionNestingLevel === 0) {
1386-
throw ConnectionException::noActiveTransaction();
1392+
throw NoActiveTransaction::new();
13871393
}
13881394
$this->isRollbackOnly = true;
13891395
}
@@ -1398,7 +1404,7 @@ public function setRollbackOnly()
13981404
public function isRollbackOnly()
13991405
{
14001406
if ($this->transactionNestingLevel === 0) {
1401-
throw ConnectionException::noActiveTransaction();
1407+
throw NoActiveTransaction::new();
14021408
}
14031409

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