Skip to content

Commit e4cc5a4

Browse files
author
Wout Gevaert
committed
improve error messages by inluding info about user input
1 parent ad4df49 commit e4cc5a4

File tree

3 files changed

+84
-13
lines changed

3 files changed

+84
-13
lines changed

src/Clauses/CallProcedureClause.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
namespace WikibaseSolutions\CypherDSL\Clauses;
2323

2424
use InvalidArgumentException;
25+
use TypeError;
26+
use WikibaseSolutions\CypherDSL\ErrorHandling\ErrorTextHelper;
2527
use WikibaseSolutions\CypherDSL\Types\AnyType;
2628
use WikibaseSolutions\CypherDSL\Variable;
2729

@@ -75,7 +77,13 @@ public function withArguments(array $arguments): self
7577
{
7678
foreach ($arguments as $argument) {
7779
if (!($argument instanceof AnyType)) {
78-
throw new InvalidArgumentException("\$arguments should only consist of AnyType objects");
80+
throw new TypeError(
81+
ErrorTextHelper::getTypeErrorObjectArrayText(
82+
'arguments',
83+
'AnyType',
84+
$argument
85+
)
86+
);
7987
}
8088
}
8189

src/Functions/RawFunction.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace WikibaseSolutions\CypherDSL\Functions;
2323

2424
use InvalidArgumentException;
25+
use TypeError;
2526
use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait;
2627
use WikibaseSolutions\CypherDSL\Traits\ListTypeTrait;
2728
use WikibaseSolutions\CypherDSL\Traits\MapTypeTrait;
@@ -37,6 +38,7 @@
3738
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\StringType;
3839
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\NodeType;
3940
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\PathType;
41+
use WikibaseSolutions\CypherDSL\ErrorHandling\ErrorTextHelper;
4042

4143
/**
4244
* This class represents any function call.
@@ -78,7 +80,13 @@ public function __construct(string $functionName, array $parameters)
7880
{
7981
foreach ($parameters as $parameter) {
8082
if (!($parameter instanceof AnyType)) {
81-
throw new InvalidArgumentException("\$parameters should only consist of AnyType objects");
83+
throw new TypeError(
84+
ErrorTextHelper::getTypeErrorObjectArrayText(
85+
'parameters',
86+
'AnyType',
87+
$parameter
88+
)
89+
);
8290
}
8391
}
8492

src/Query.php

Lines changed: 66 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use WikibaseSolutions\CypherDSL\Clauses\WhereClause;
3939
use WikibaseSolutions\CypherDSL\Clauses\WithClause;
4040
use WikibaseSolutions\CypherDSL\Functions\FunctionCall;
41+
use WikibaseSolutions\CypherDSL\ErrorHandling\ErrorTextHelper;
4142
use WikibaseSolutions\CypherDSL\Literals\Boolean;
4243
use WikibaseSolutions\CypherDSL\Literals\Decimal;
4344
use WikibaseSolutions\CypherDSL\Literals\Literal;
@@ -235,7 +236,9 @@ public function match($patterns): self
235236

236237
foreach ($patterns as $pattern) {
237238
if (!($pattern instanceof StructuralType)) {
238-
throw new TypeError("\$patterns should only consist of StructuralType objects");
239+
throw new TypeError(
240+
ErrorTextHelper::getTypeErrorObjectArrayText('patterns', 'StructuralType', $pattern)
241+
);
239242
}
240243

241244
$matchClause->addPattern($pattern);
@@ -268,7 +271,13 @@ public function returning($expressions, bool $distinct = false): self
268271

269272
foreach ($expressions as $maybeAlias => $expression) {
270273
if (!($expression instanceof AnyType)) {
271-
throw new TypeError("\$expressions should only consist of AnyType objects");
274+
throw new TypeError(
275+
ErrorTextHelper::getTypeErrorObjectArrayText(
276+
'expressions',
277+
'AnyType',
278+
$expression
279+
)
280+
);
272281
}
273282

274283
if ($expression instanceof Node) {
@@ -305,7 +314,13 @@ public function create($patterns): self
305314

306315
foreach ($patterns as $pattern) {
307316
if (!($pattern instanceof StructuralType)) {
308-
throw new TypeError("\$patterns should only consist of StructuralType objects");
317+
throw new TypeError(
318+
ErrorTextHelper::getTypeErrorObjectArrayText(
319+
'patterns',
320+
'StructuralType',
321+
$pattern
322+
)
323+
);
309324
}
310325

311326
$createClause->addPattern($pattern);
@@ -335,7 +350,13 @@ public function delete($nodes): self
335350

336351
foreach ($nodes as $node) {
337352
if (!($node instanceof NodeType)) {
338-
throw new TypeError("\$nodes should exist of only NodeType objects");
353+
throw new TypeError(
354+
ErrorTextHelper::getTypeErrorObjectArrayText(
355+
'nodes',
356+
'NodeType',
357+
$node
358+
)
359+
);
339360
}
340361

341362
$deleteClause->addNode($node);
@@ -366,7 +387,13 @@ public function detachDelete($nodes): self
366387

367388
foreach ($nodes as $node) {
368389
if (!($node instanceof NodeType)) {
369-
throw new TypeError("\$nodes should exist of only NodeType objects");
390+
throw new TypeError(
391+
ErrorTextHelper::getTypeErrorObjectArrayText(
392+
'nodes',
393+
'NodeType',
394+
$node
395+
)
396+
);
370397
}
371398

372399
$deleteClause->addNode($node);
@@ -444,7 +471,13 @@ public function optionalMatch($patterns): self
444471

445472
foreach ($patterns as $pattern) {
446473
if (!($pattern instanceof StructuralType)) {
447-
throw new TypeError("\$patterns should only consist of StructuralType objects");
474+
throw new TypeError(
475+
ErrorTextHelper::getTypeErrorObjectArrayText(
476+
'patterns',
477+
'StructuralType',
478+
$pattern
479+
)
480+
);
448481
}
449482

450483
$optionalMatchClause->addPattern($pattern);
@@ -476,7 +509,13 @@ public function orderBy($properties, bool $descending = false): self
476509

477510
foreach ($properties as $property) {
478511
if (!($property instanceof Property)) {
479-
throw new TypeError("\$properties should only consist of Property objects");
512+
throw new TypeError(
513+
ErrorTextHelper::getTypeErrorObjectArrayText(
514+
'properties',
515+
'PropertyType',
516+
$property
517+
)
518+
);
480519
}
481520

482521
$orderByClause->addProperty($property);
@@ -506,7 +545,13 @@ public function remove($expressions): self
506545

507546
foreach ($expressions as $expression) {
508547
if (!($expression instanceof Property) && !($expression instanceof Label)) {
509-
throw new TypeError("\$expressions should consist of only Property or Label objects");
548+
throw new TypeError(
549+
ErrorTextHelper::getTypeErrorObjectArrayText(
550+
'expressions',
551+
'Property or Label',
552+
$expression
553+
)
554+
);
510555
}
511556

512557
$removeClause->addExpression($expression);
@@ -536,7 +581,11 @@ public function set($expressions): self
536581

537582
foreach ($expressions as $expression) {
538583
if (!($expression instanceof Assignment) && !($expression instanceof Label)) {
539-
throw new TypeError("\$expressions should only consist of Assignment and Label objects");
584+
throw new TypeError(
585+
'expressions',
586+
'Assignment and Label',
587+
$expression
588+
);
540589
}
541590

542591
$setClause->addAssignment($expression);
@@ -586,7 +635,13 @@ public function with($expressions): self
586635

587636
foreach ($expressions as $maybeAlias => $expression) {
588637
if (!($expression instanceof AnyType)) {
589-
throw new TypeError("\$expressions should only consist of AnyType objects");
638+
throw new TypeError(
639+
ErrorTextHelper::getTypeErrorObjectArrayText(
640+
'expressions',
641+
'AnyType',
642+
$expression
643+
)
644+
);
590645
}
591646

592647
if ($expression instanceof Node) {
@@ -678,4 +733,4 @@ public function build(): string
678733
" ", array_filter($builtClauses, fn($clause) => !empty($clause))
679734
);
680735
}
681-
}
736+
}

0 commit comments

Comments
 (0)