Skip to content

Commit e33ffa0

Browse files
author
Enno Woortmann
committed
Rework remaining exceptions
1 parent c65652d commit e33ffa0

26 files changed

+114
-118
lines changed

docs/source/combinedSchemas/allOf.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Possible exception (if a string is provided):
3939
.. code-block:: none
4040
4141
Invalid value for example declined by composition constraint.
42-
Requires to match 2 composition elements but matched 0 elements.
42+
Requires to match all composition elements but matched 0 elements.
4343
- Composition element #1: Failed
4444
* Invalid type for example. Requires float, got string
4545
- Composition element #2: Failed

docs/source/complexTypes/array.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ With a schema like this all items must contain a string with at least two charac
5656

5757
.. code-block:: none
5858
59-
Invalid item in array example:
59+
Invalid items in array example:
6060
- invalid item #3
6161
* Invalid type for item of array example. Requires string, got double
6262

docs/source/complexTypes/multiType.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,6 @@ For example if an array **["Hello", 123, "Goodbye"]** is given the validation wi
5454

5555
.. code-block:: none
5656
57-
Invalid item in array example:
57+
Invalid items in array example:
5858
- invalid item #1
5959
* Invalid type for item of array example. Requires string, got integer

docs/source/generic/combinedException.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,30 +60,30 @@ The exception which will be thrown (combined array-exception and combined-schema
6060

6161
.. code-block:: none
6262
63-
Invalid item in array property:
63+
Invalid items in array property:
6464
- invalid item #0
6565
* Invalid value for item of array property declined by composition constraint.
66-
Requires to match 2 composition elements but matched 1 elements.
66+
Requires to match all composition elements but matched 1 elements.
6767
- Composition element #1: Failed
6868
* Invalid type for name. Requires string, got boolean
6969
- Composition element #2: Valid
7070
- invalid item #1
7171
* Invalid value for item of array property declined by composition constraint.
72-
Requires to match 2 composition elements but matched 0 elements.
72+
Requires to match all composition elements but matched 0 elements.
7373
- Composition element #1: Failed
7474
* Value for name must not be shorter than 2
7575
- Composition element #2: Failed
7676
* Invalid type for age. Requires int, got string
7777
- invalid item #2
7878
* Invalid value for item of array property declined by composition constraint.
79-
Requires to match 2 composition elements but matched 0 elements.
79+
Requires to match all composition elements but matched 0 elements.
8080
- Composition element #1: Failed
8181
* Invalid type for item of array property. Requires object, got integer
8282
- Composition element #2: Failed
8383
* Invalid type for item of array property. Requires object, got integer
8484
- invalid item #3
8585
* Invalid value for item of array property declined by composition constraint.
86-
Requires to match 2 composition elements but matched 1 elements.
86+
Requires to match all composition elements but matched 1 elements.
8787
- Composition element #1: Failed
8888
* Missing required value for name
8989
* Invalid type for name. Requires string, got NULL

src/Model/Validator/ConditionalPropertyValidator.php

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

55
namespace PHPModelGenerator\Model\Validator;
66

7+
use PHPModelGenerator\Exception\ComposedValue\ConditionalException;
78
use PHPModelGenerator\Model\Property\CompositionPropertyDecorator;
89
use PHPModelGenerator\Model\Property\PropertyInterface;
910
use PHPModelGenerator\PropertyProcessor\ComposedValue\IfProcessor;
@@ -28,9 +29,10 @@ public function __construct(
2829
array $validatorVariables
2930
) {
3031
parent::__construct(
31-
"Invalid value for {$property->getName()} declined by conditional composition constraint",
3232
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'ConditionalComposedItem.phptpl',
33-
$validatorVariables
33+
$validatorVariables,
34+
ConditionalException::class,
35+
[$property->getName()]
3436
);
3537

3638
$this->composedProcessor = IfProcessor::class;

src/Model/Validator/FilterValidator.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44

55
namespace PHPModelGenerator\Model\Validator;
66

7+
use PHPModelGenerator\Exception\Generic\IncompatibleFilterException;
8+
use PHPModelGenerator\Exception\Generic\InvalidFilterValueException;
79
use PHPModelGenerator\Exception\SchemaException;
810
use PHPModelGenerator\Model\GeneratorConfiguration;
911
use PHPModelGenerator\Model\Property\PropertyInterface;
10-
use PHPModelGenerator\Model\Schema;
1112
use PHPModelGenerator\PropertyProcessor\Filter\FilterInterface;
1213
use PHPModelGenerator\PropertyProcessor\Filter\TransformingFilterInterface;
1314
use PHPModelGenerator\Utils\RenderHelper;
@@ -24,13 +25,14 @@ class FilterValidator extends PropertyTemplateValidator
2425
/**
2526
* FilterValidator constructor.
2627
*
27-
* @param GeneratorConfiguration $generatorConfiguration
28-
* @param FilterInterface $filter
29-
* @param PropertyInterface $property
30-
* @param array $filterOptions
28+
* @param GeneratorConfiguration $generatorConfiguration
29+
* @param FilterInterface $filter
30+
* @param PropertyInterface $property
31+
* @param array $filterOptions
3132
* @param TransformingFilterInterface|null $transformingFilter
3233
*
3334
* @throws SchemaException
35+
* @throws ReflectionException
3436
*/
3537
public function __construct(
3638
GeneratorConfiguration $generatorConfiguration,
@@ -44,11 +46,6 @@ public function __construct(
4446
: $this->validateFilterCompatibilityWithTransformedType($filter, $transformingFilter, $property);
4547

4648
parent::__construct(
47-
sprintf(
48-
'Filter %s is not compatible with property type " . gettype($value) . " for property %s',
49-
$filter->getToken(),
50-
$property->getName()
51-
),
5249
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'Filter.phptpl',
5350
[
5451
'skipTransformedValuesCheck' => false,
@@ -63,13 +60,15 @@ public function __construct(
6360
'filterClass' => $filter->getFilter()[0],
6461
'filterMethod' => $filter->getFilter()[1],
6562
'filterOptions' => var_export($filterOptions, true),
66-
'transferExceptionMessage' => sprintf(
67-
'Invalid value for property %s denied by filter %s: {$e->getMessage()}',
68-
$property->getName(),
69-
$filter->getToken()
63+
'filterValueValidator' => new PropertyValidator(
64+
'',
65+
InvalidFilterValueException::class,
66+
[$property->getName(), $filter->getToken(), '&$filterException']
7067
),
7168
'viewHelper' => new RenderHelper($generatorConfiguration),
72-
]
69+
],
70+
IncompatibleFilterException::class,
71+
[$property->getName(), $filter->getToken()]
7372
);
7473
}
7574

src/Model/Validator/PassThroughTypeCheckValidator.php

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

55
namespace PHPModelGenerator\Model\Validator;
66

7+
use PHPModelGenerator\Exception\Generic\InvalidTypeException;
78
use PHPModelGenerator\Model\Property\PropertyInterface;
89
use ReflectionType;
910

@@ -37,12 +38,8 @@ public function __construct(
3738
ReflectionTypeCheckValidator::fromReflectionType($passThroughType, $property)->getCheck(),
3839
$typeCheckValidator->getCheck()
3940
),
40-
sprintf(
41-
'Invalid type for %s. Requires [%s, %s], got " . gettype($value) . "',
42-
$property->getName(),
43-
$passThroughType->getName(),
44-
$property->getType()
45-
)
41+
InvalidTypeException::class,
42+
[$property->getName(), [$passThroughType->getName(), $property->getType()]]
4643
);
4744
}
4845

src/Model/Validator/PropertyDependencyValidator.php

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

55
namespace PHPModelGenerator\Model\Validator;
66

7+
use PHPModelGenerator\Exception\Dependency\InvalidPropertyDependencyException;
78
use PHPModelGenerator\Model\Property\PropertyInterface;
89

910
/**
@@ -22,11 +23,6 @@ class PropertyDependencyValidator extends PropertyTemplateValidator
2223
public function __construct(PropertyInterface $property, array $dependencies)
2324
{
2425
parent::__construct(
25-
sprintf(
26-
'Missing required attributes which are dependants of %s:' .
27-
'\n - " . join("\n - ", $missingAttributes) . "',
28-
$property->getName()
29-
),
3026
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'PropertyDependency.phptpl',
3127
[
3228
'propertyName' => $property->getName(),
@@ -35,7 +31,9 @@ public function __construct(PropertyInterface $property, array $dependencies)
3531
'',
3632
var_export(array_values($dependencies), true)
3733
),
38-
]
34+
],
35+
InvalidPropertyDependencyException::class,
36+
[$property->getName(), '&$missingAttributes']
3937
);
4038
}
4139

src/Model/Validator/PropertyNamesValidator.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPMicroTemplate\Exception\FileSystemException;
88
use PHPMicroTemplate\Exception\SyntaxErrorException;
99
use PHPMicroTemplate\Exception\UndefinedSymbolException;
10+
use PHPModelGenerator\Exception\Generic\InvalidPropertyNamesException;
1011
use PHPModelGenerator\Exception\SchemaException;
1112
use PHPModelGenerator\Model\Schema;
1213
use PHPModelGenerator\Model\Validator;
@@ -48,16 +49,14 @@ public function __construct(
4849
});
4950

5051
parent::__construct(
51-
$this->getRenderer()->renderTemplate(
52-
DIRECTORY_SEPARATOR . 'Exception' . DIRECTORY_SEPARATOR . 'InvalidPropertiesException.phptpl',
53-
['error' => 'Provided JSON contains properties with invalid names.', 'property' => 'property']
54-
),
5552
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'PropertyNames.phptpl',
5653
[
5754
'nameValidationProperty' => $nameValidationProperty,
5855
'generatorConfiguration' => $schemaProcessor->getGeneratorConfiguration(),
5956
'viewHelper' => new RenderHelper($schemaProcessor->getGeneratorConfiguration()),
60-
]
57+
],
58+
InvalidPropertyNamesException::class,
59+
[$schema->getClassName(), '&$invalidProperties']
6160
);
6261
}
6362

src/Model/Validator/SchemaDependencyValidator.php

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

55
namespace PHPModelGenerator\Model\Validator;
66

7+
use PHPModelGenerator\Exception\Dependency\InvalidSchemaDependencyException;
8+
use PHPModelGenerator\Exception\SchemaException;
79
use PHPModelGenerator\Model\Property\Property;
810
use PHPModelGenerator\Model\Property\PropertyInterface;
911
use PHPModelGenerator\Model\Schema;
@@ -21,15 +23,15 @@ class SchemaDependencyValidator extends PropertyTemplateValidator
2123
/**
2224
* SchemaDependencyValidator constructor.
2325
*
24-
* @param SchemaProcessor $schemaProcessor
26+
* @param SchemaProcessor $schemaProcessor
2527
* @param PropertyInterface $property
26-
* @param Schema $schema
28+
* @param Schema $schema
29+
*
30+
* @throws SchemaException
2731
*/
2832
public function __construct(SchemaProcessor $schemaProcessor, PropertyInterface $property, Schema $schema)
2933
{
3034
parent::__construct(
31-
"Invalid schema which is dependant on {$property->getName()}:\\n - " .
32-
'" . preg_replace("/\n([^\s])/m", "\n - $1", preg_replace("/\n\s/m", "\n ", $dependencyException->getMessage())) . "',
3335
DIRECTORY_SEPARATOR . 'Validator' . DIRECTORY_SEPARATOR . 'SchemaDependency.phptpl',
3436
[
3537
'viewHelper' => new RenderHelper($schemaProcessor->getGeneratorConfiguration()),
@@ -42,7 +44,9 @@ public function __construct(SchemaProcessor $schemaProcessor, PropertyInterface
4244
$schema->getClassName(),
4345
$schemaProcessor->getGeneratorConfiguration()
4446
))
45-
]
47+
],
48+
InvalidSchemaDependencyException::class,
49+
[$property->getName(), '&$dependencyException']
4650
);
4751
}
4852

src/PropertyProcessor/Decorator/Property/ObjectInstantiationDecorator.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,7 @@ public function decorate(string $input, PropertyInterface $property): string
5151
[
5252
'input' => $input,
5353
'className' => $this->className,
54-
'exceptionMessage' => sprintf(
55-
'Invalid type for %s. Requires object, got " . gettype($param) . "',
56-
$property->getName()
57-
),
5854
'generatorConfiguration' => $this->generatorConfiguration,
59-
'viewHelper' => new RenderHelper($this->generatorConfiguration),
6055
]
6156
);
6257
}

src/PropertyProcessor/Filter/FilterProcessor.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,8 @@ private function addTransformedValuePassThrough(
144144
ReflectionTypeCheckValidator::fromReflectionType($filteredType, $property)->getCheck(),
145145
$validator->getCheck()
146146
),
147-
$validator->getExceptionClass()
147+
$validator->getExceptionClass(),
148+
$validator->getExceptionParams()
148149
),
149150
3
150151
);

src/PropertyProcessor/Property/ArrayProcessor.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPMicroTemplate\Exception\FileSystemException;
88
use PHPMicroTemplate\Exception\SyntaxErrorException;
99
use PHPMicroTemplate\Exception\UndefinedSymbolException;
10+
use PHPModelGenerator\Exception\Arrays\AdditionalTupleItemsException;
1011
use PHPModelGenerator\Exception\Arrays\ContainsException;
1112
use PHPModelGenerator\Exception\Arrays\MaxItemsException;
1213
use PHPModelGenerator\Exception\Arrays\MinItemsException;
@@ -199,11 +200,8 @@ private function addAdditionalItemsValidator(PropertyInterface $property, array
199200
$property->addValidator(
200201
new PropertyValidator(
201202
'($amount = count($value)) > ' . $expectedAmount,
202-
sprintf(
203-
'Tuple array %s contains not allowed additional items. Expected %s items, got $amount',
204-
$property->getName(),
205-
$expectedAmount
206-
)
203+
AdditionalTupleItemsException::class,
204+
[$property->getName(), $expectedAmount, '&$amount']
207205
)
208206
);
209207
}

src/PropertyProcessor/Property/BaseProcessor.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use PHPMicroTemplate\Exception\SyntaxErrorException;
99
use PHPMicroTemplate\Exception\UndefinedSymbolException;
1010
use PHPModelGenerator\Exception\Generic\AdditionalPropertiesException;
11+
use PHPModelGenerator\Exception\Object\MaxPropertiesException;
12+
use PHPModelGenerator\Exception\Object\MinPropertiesException;
1113
use PHPModelGenerator\Exception\SchemaException;
1214
use PHPModelGenerator\Model\Property\Property;
1315
use PHPModelGenerator\Model\Property\PropertyInterface;
@@ -145,11 +147,8 @@ protected function addMaxPropertiesValidator(string $propertyName, array $proper
145147
$this->schema->addBaseValidator(
146148
new PropertyValidator(
147149
sprintf('count($modelData) > %d', $propertyData['maxProperties']),
148-
sprintf(
149-
'Provided object for %s must not contain more than %s properties',
150-
$propertyName,
151-
$propertyData['maxProperties']
152-
)
150+
MaxPropertiesException::class,
151+
[$propertyName, $propertyData['maxProperties']]
153152
)
154153
);
155154
}
@@ -169,11 +168,8 @@ protected function addMinPropertiesValidator(string $propertyName, array $proper
169168
$this->schema->addBaseValidator(
170169
new PropertyValidator(
171170
sprintf('count($modelData) < %d', $propertyData['minProperties']),
172-
sprintf(
173-
'Provided object for %s must not contain less than %s properties',
174-
$propertyName,
175-
$propertyData['minProperties']
176-
)
171+
MinPropertiesException::class,
172+
[$propertyName, $propertyData['minProperties']]
177173
)
178174
);
179175
}

src/Templates/Validator/ArrayItem.phptpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ is_array($value) && (function (&$items) use (&$invalidItems{{ suffix }}) {
2626
} catch (\Exception $e) {
2727
// collect all errors concerning invalid items
2828
isset($invalidItems{{ suffix }}[$index])
29-
? $invalidItems{{ suffix }}[$index][] = $e->getMessage()
30-
: $invalidItems{{ suffix }}[$index] = [$e->getMessage()];
29+
? $invalidItems{{ suffix }}[$index][] = $e
30+
: $invalidItems{{ suffix }}[$index] = [$e];
3131
}
3232
}
3333

src/Templates/Validator/ArrayTuple.phptpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ is_array($value) && (function (&$items) use (&$invalidTuples) {
3333
} catch (\Exception $e) {
3434
// collect all errors concerning invalid tuples
3535
isset($invalidTuples[$index])
36-
? $invalidTuples[$index][] = $e->getMessage()
37-
: $invalidTuples[$index] = [$e->getMessage()];
36+
? $invalidTuples[$index][] = $e
37+
: $invalidTuples[$index] = [$e];
3838
}
3939
{% endforeach %}
4040

src/Templates/Validator/Filter.phptpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
// make sure exceptions from the filter are caught and added to the error handling
66
try {
77
$value = call_user_func_array([\{{ filterClass }}::class, "{{ filterMethod }}"], [$value, {{ filterOptions }}]);
8-
} catch (\Exception $e) {
9-
{{ viewHelper.validationError(transferExceptionMessage) }}
8+
} catch (\Exception $filterException) {
9+
{{ viewHelper.validationError(filterValueValidator) }}
1010
}
1111

1212
return false;

src/Templates/Validator/PropertyNames.phptpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
} catch (\Exception $e) {
2828
// collect all errors concerning invalid property names
2929
isset($invalidProperties[$value])
30-
? $invalidProperties[$value][] = $e->getMessage()
31-
: $invalidProperties[$value] = [$e->getMessage()];
30+
? $invalidProperties[$value][] = $e
31+
: $invalidProperties[$value] = [$e];
3232
}
3333
}
3434

0 commit comments

Comments
 (0)