Skip to content

Commit 512ee13

Browse files
authored
Change thrown exceptions to more suitable (#1092)
1 parent c388227 commit 512ee13

14 files changed

Lines changed: 44 additions & 144 deletions

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@
170170
- New #1082: Add `WithQuery` class that holds the information for a single "WITH" query clause (@vjik)
171171
- New #1082: Add `QueryPartsInterface::addWithQuery()` method (@vjik)
172172
- Chg #1082: `QueryPartsInterface::withQuery()` method replace "WITH" clause instead of adding before (@vjik)
173+
- Enh #1092: Change the exceptions thrown to be more appropriate (@Tigrov)
173174

174175
## 1.3.0 March 21, 2024
175176

src/Expression/ExpressionBuilderInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ interface ExpressionBuilderInterface
2626
*
2727
* @param ExpressionInterface $expression The expression to be built.
2828
* @param array $params The binding parameters.
29-
* @throws \InvalidArgumentException If builder can't handle expression passed.
29+
*
3030
* @return string The raw SQL that will not be additionally escaped or quoted.
3131
*
3232
* @psalm-param T $expression

src/QueryBuilder/AbstractDQLQueryBuilder.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
use Yiisoft\Db\Expression\CompositeExpressionBuilder;
99
use Yiisoft\Db\Expression\Value\Param;
1010
use Yiisoft\Db\Expression\Value\Builder\ParamBuilder;
11-
use Yiisoft\Db\Exception\Exception;
1211
use InvalidArgumentException;
13-
use Yiisoft\Db\Exception\InvalidConfigException;
1412
use Yiisoft\Db\Exception\NotSupportedException;
1513
use Yiisoft\Db\Expression\Value\ArrayValue;
1614
use Yiisoft\Db\Expression\Value\Builder\ArrayValueBuilder;
@@ -257,7 +255,7 @@ public function buildJoin(array $joins, array &$params): string
257255
foreach ($joins as $i => $join) {
258256
/** @psalm-suppress DocblockTypeContradiction */
259257
if (!is_array($join) || !isset($join[0], $join[1])) {
260-
throw new Exception(
258+
throw new InvalidArgumentException(
261259
'A join clause must be specified as an array of join type, join table, and optionally join condition.',
262260
);
263261
}
@@ -479,7 +477,7 @@ public function getExpressionBuilder(ExpressionInterface $expression): Expressio
479477
$className = $expression::class;
480478

481479
if (!isset($this->expressionBuilders[$className])) {
482-
throw new InvalidArgumentException(
480+
throw new NotSupportedException(
483481
'Expression of class ' . $className . ' can not be built in ' . static::class,
484482
);
485483
}
@@ -613,8 +611,6 @@ protected function extractAlias(string $table): array|bool
613611
}
614612

615613
/**
616-
* @throws Exception
617-
* @throws InvalidConfigException
618614
* @throws NotSupportedException
619615
*
620616
* @return array The list of table names with quote.

src/QueryBuilder/Condition/Builder/BetweenBuilder.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Yiisoft\Db\QueryBuilder\Condition\Builder;
66

7-
use Yiisoft\Db\Exception\Exception;
8-
use InvalidArgumentException;
9-
use Yiisoft\Db\Exception\InvalidConfigException;
107
use Yiisoft\Db\Exception\NotSupportedException;
118
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
129
use Yiisoft\Db\Expression\ExpressionInterface;
@@ -28,9 +25,6 @@ public function __construct(private readonly QueryBuilderInterface $queryBuilder
2825
*
2926
* @param Between|NotBetween $expression
3027
*
31-
* @throws Exception
32-
* @throws InvalidArgumentException
33-
* @throws InvalidConfigException
3428
* @throws NotSupportedException
3529
*/
3630
public function build(ExpressionInterface $expression, array &$params = []): string
@@ -52,9 +46,6 @@ public function build(ExpressionInterface $expression, array &$params = []): str
5246
/**
5347
* Attaches `$value` to `$params` array and return placeholder.
5448
*
55-
* @throws Exception
56-
* @throws InvalidArgumentException
57-
* @throws InvalidConfigException
5849
* @throws NotSupportedException
5950
*/
6051
protected function createPlaceholder(mixed $value, array &$params): string

src/QueryBuilder/Condition/Builder/CompareBuilder.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44

55
namespace Yiisoft\Db\QueryBuilder\Condition\Builder;
66

7-
use Yiisoft\Db\Exception\Exception;
8-
use Yiisoft\Db\Exception\InvalidConfigException;
97
use Yiisoft\Db\Exception\NotSupportedException;
108
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
119
use Yiisoft\Db\Expression\ExpressionInterface;
@@ -35,8 +33,6 @@ public function __construct(
3533
*
3634
* @param Equals|GreaterThan|GreaterThanOrEqual|LessThan|LessThanOrEqual|NotEquals $expression
3735
*
38-
* @throws Exception
39-
* @throws InvalidConfigException
4036
* @throws NotSupportedException
4137
*/
4238
public function build(ExpressionInterface $expression, array &$params = []): string
@@ -58,9 +54,7 @@ public function build(ExpressionInterface $expression, array &$params = []): str
5854
}
5955

6056
/**
61-
* @throws InvalidConfigException
6257
* @throws NotSupportedException
63-
* @throws Exception
6458
*/
6559
private function prepareColumn(string|ExpressionInterface $column, array &$params): string
6660
{

src/QueryBuilder/Condition/Builder/ExistsBuilder.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44

55
namespace Yiisoft\Db\QueryBuilder\Condition\Builder;
66

7-
use Yiisoft\Db\Exception\Exception;
8-
use InvalidArgumentException;
9-
use Yiisoft\Db\Exception\InvalidConfigException;
107
use Yiisoft\Db\Exception\NotSupportedException;
118
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
129
use Yiisoft\Db\Expression\ExpressionInterface;
@@ -28,9 +25,6 @@ public function __construct(private readonly QueryBuilderInterface $queryBuilder
2825
*
2926
* @param Exists|NotExists $expression
3027
*
31-
* @throws Exception
32-
* @throws InvalidArgumentException
33-
* @throws InvalidConfigException
3428
* @throws NotSupportedException
3529
*/
3630
public function build(ExpressionInterface $expression, array &$params = []): string

src/QueryBuilder/Condition/Builder/InBuilder.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use ArrayAccess;
88
use Iterator;
99
use Traversable;
10-
use Yiisoft\Db\Exception\Exception;
1110
use InvalidArgumentException;
12-
use Yiisoft\Db\Exception\InvalidConfigException;
1311
use Yiisoft\Db\Exception\NotSupportedException;
1412
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
1513
use Yiisoft\Db\Expression\ExpressionInterface;
@@ -42,9 +40,7 @@ public function __construct(protected QueryBuilderInterface $queryBuilder) {}
4240
*
4341
* @param In|NotIn $expression
4442
*
45-
* @throws Exception
4643
* @throws InvalidArgumentException
47-
* @throws InvalidConfigException
4844
* @throws NotSupportedException
4945
*/
5046
public function build(ExpressionInterface $expression, array &$params = []): string
@@ -115,10 +111,7 @@ public function build(ExpressionInterface $expression, array &$params = []): str
115111
/**
116112
* Builds `$values` to use in condition.
117113
*
118-
* @throws Exception
119114
* @throws InvalidArgumentException
120-
* @throws InvalidConfigException
121-
* @throws NotSupportedException
122115
*
123116
* @psalm-return string[]
124117
*/
@@ -144,9 +137,6 @@ protected function buildValues(string $column, iterable $values, array &$params
144137
/**
145138
* Build SQL for composite `IN` condition.
146139
*
147-
* @throws Exception
148-
* @throws InvalidArgumentException
149-
* @throws InvalidConfigException
150140
* @throws NotSupportedException
151141
*
152142
* @psalm-param array<string|ExpressionInterface>|string $columns
@@ -183,8 +173,6 @@ protected function buildSubqueryInCondition(
183173
/**
184174
* Builds an SQL statement for checking the existence of rows with the specified composite column values.
185175
*
186-
* @throws Exception
187-
* @throws InvalidConfigException
188176
* @throws InvalidArgumentException
189177
* @throws NotSupportedException
190178
*

src/QueryBuilder/Condition/Builder/LikeBuilder.php

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
use Traversable;
99
use Yiisoft\Db\Expression\Value\Param;
1010
use Yiisoft\Db\Constant\DataType;
11-
use Yiisoft\Db\Exception\Exception;
12-
use InvalidArgumentException;
13-
use Yiisoft\Db\Exception\InvalidConfigException;
1411
use Yiisoft\Db\Exception\NotSupportedException;
1512
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
1613
use Yiisoft\Db\Expression\ExpressionInterface;
@@ -22,7 +19,6 @@
2219

2320
use function implode;
2421
use function is_string;
25-
use function str_contains;
2622
use function strtr;
2723

2824
/**
@@ -56,9 +52,6 @@ public function __construct(
5652
*
5753
* @param Like|NotLike $expression
5854
*
59-
* @throws Exception
60-
* @throws InvalidArgumentException
61-
* @throws InvalidConfigException
6255
* @throws NotSupportedException
6356
*/
6457
public function build(ExpressionInterface $expression, array &$params = []): string
@@ -102,9 +95,6 @@ public function build(ExpressionInterface $expression, array &$params = []): str
10295
/**
10396
* Prepare column to use in SQL.
10497
*
105-
* @throws Exception
106-
* @throws InvalidArgumentException
107-
* @throws InvalidConfigException
10898
* @throws NotSupportedException
10999
*/
110100
protected function prepareColumn(Like|NotLike $condition, array &$params): string
@@ -115,21 +105,13 @@ protected function prepareColumn(Like|NotLike $condition, array &$params): strin
115105
return $this->queryBuilder->buildExpression($column, $params);
116106
}
117107

118-
if (!str_contains($column, '(')) {
119-
return $this->queryBuilder->getQuoter()->quoteColumnName($column);
120-
}
121-
122-
return $column;
108+
return $this->queryBuilder->getQuoter()->quoteColumnName($column);
123109
}
124110

125111
/**
126112
* Prepare value to use in SQL.
127113
*
128-
* @throws Exception
129-
* @throws InvalidArgumentException
130-
* @throws InvalidConfigException
131114
* @throws NotSupportedException
132-
* @return string
133115
*/
134116
protected function preparePlaceholderName(
135117
string|Stringable|int|ExpressionInterface $value,

src/QueryBuilder/Condition/Builder/LogicalBuilder.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace Yiisoft\Db\QueryBuilder\Condition\Builder;
66

7-
use Yiisoft\Db\Exception\Exception;
87
use InvalidArgumentException;
9-
use Yiisoft\Db\Exception\InvalidConfigException;
108
use Yiisoft\Db\Exception\NotSupportedException;
119
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
1210
use Yiisoft\Db\Expression\ExpressionInterface;
@@ -35,9 +33,7 @@ public function __construct(
3533
*
3634
* @param AndX|OrX $expression
3735
*
38-
* @throws Exception
3936
* @throws InvalidArgumentException
40-
* @throws InvalidConfigException
4137
* @throws NotSupportedException
4238
*/
4339
public function build(ExpressionInterface $expression, array &$params = []): string
@@ -61,9 +57,7 @@ public function build(ExpressionInterface $expression, array &$params = []): str
6157
}
6258

6359
/**
64-
* @throws Exception
6560
* @throws InvalidArgumentException
66-
* @throws InvalidConfigException
6761
* @throws NotSupportedException
6862
*
6963
* @psalm-param array<array|ExpressionInterface|scalar> $expressions

src/QueryBuilder/Condition/Builder/NotBuilder.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44

55
namespace Yiisoft\Db\QueryBuilder\Condition\Builder;
66

7-
use Yiisoft\Db\Exception\Exception;
87
use InvalidArgumentException;
9-
use Yiisoft\Db\Exception\InvalidConfigException;
108
use Yiisoft\Db\Exception\NotSupportedException;
119
use Yiisoft\Db\Expression\ExpressionBuilderInterface;
1210
use Yiisoft\Db\Expression\ExpressionInterface;
@@ -44,9 +42,7 @@ public function __construct(private readonly QueryBuilderInterface $queryBuilder
4442
*
4543
* @param Not $expression
4644
*
47-
* @throws Exception
4845
* @throws InvalidArgumentException
49-
* @throws InvalidConfigException
5046
* @throws NotSupportedException
5147
*/
5248
public function build(ExpressionInterface $expression, array &$params = []): string

0 commit comments

Comments
 (0)