-
Notifications
You must be signed in to change notification settings - Fork 0
/
ConstraintViolationErrorHandler.php
64 lines (53 loc) · 2.5 KB
/
ConstraintViolationErrorHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
/*
* Copyright (c) Fusonic GmbH. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*/
declare(strict_types=1);
namespace Fusonic\HttpKernelBundle\ErrorHandler;
use Fusonic\HttpKernelBundle\ConstraintViolation\ArgumentCountConstraintViolation;
use Fusonic\HttpKernelBundle\ConstraintViolation\InvalidEnumConstraintViolation;
use Fusonic\HttpKernelBundle\ConstraintViolation\MissingConstructorArgumentsConstraintViolation;
use Fusonic\HttpKernelBundle\ConstraintViolation\NotNormalizableValueConstraintViolation;
use Fusonic\HttpKernelBundle\ConstraintViolation\TypeConstraintViolation;
use Fusonic\HttpKernelBundle\Exception\ConstraintViolationException;
use Fusonic\HttpKernelBundle\Exception\InvalidEnumException;
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Validator\ConstraintViolationListInterface;
class ConstraintViolationErrorHandler implements ErrorHandlerInterface
{
/**
* @param array<string, mixed> $data
* @param class-string $className
*/
public function handleDenormalizeError(\Throwable $ex, array $data, string $className): \Throwable
{
if ($ex instanceof InvalidEnumException) {
return ConstraintViolationException::fromConstraintViolation(
new InvalidEnumConstraintViolation($ex->enumClass, $ex->data, $ex->propertyPath)
);
}
if ($ex instanceof NotNormalizableValueException) {
return ConstraintViolationException::fromConstraintViolation(
new NotNormalizableValueConstraintViolation($ex, $data, $className)
);
}
if ($ex instanceof MissingConstructorArgumentsException) {
return ConstraintViolationException::fromConstraintViolation(
new MissingConstructorArgumentsConstraintViolation($ex)
);
}
if ($ex instanceof \ArgumentCountError) {
return ConstraintViolationException::fromConstraintViolation(new ArgumentCountConstraintViolation($ex));
}
if ($ex instanceof \TypeError) {
return ConstraintViolationException::fromConstraintViolation(new TypeConstraintViolation($ex));
}
return $ex;
}
public function handleConstraintViolations(ConstraintViolationListInterface $list): void
{
throw new ConstraintViolationException($list);
}
}