• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 7196499749

13 Dec 2023 02:17PM UTC coverage: 37.359% (+1.4%) from 36.003%
7196499749

push

github

web-flow
ci: conflict sebastian/comparator (#6032)

* ci: conflict sebastian/comparator

* for lowest

10295 of 27557 relevant lines covered (37.36%)

28.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

16.67
/src/GraphQl/Serializer/Exception/ValidationExceptionNormalizer.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11

12
declare(strict_types=1);
13

14
namespace ApiPlatform\GraphQl\Serializer\Exception;
15

16
use ApiPlatform\Symfony\Validator\Exception\ValidationException;
17
use GraphQL\Error\Error;
18
use GraphQL\Error\FormattedError;
19
use Symfony\Component\HttpFoundation\Response;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
use Symfony\Component\Validator\ConstraintViolation;
22

23
/**
24
 * Normalize validation exceptions.
25
 *
26
 * @author Mahmood Bazdar <mahmood@bazdar.me>
27
 * @author Alan Poulain <contact@alanpoulain.eu>
28
 */
29
final class ValidationExceptionNormalizer implements NormalizerInterface
30
{
31
    public function __construct(private readonly array $exceptionToStatus = [])
32
    {
33
    }
116✔
34

35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function normalize(mixed $object, string $format = null, array $context = []): array
39
    {
40
        /** @var ValidationException */
41
        $validationException = $object->getPrevious();
×
42
        $error = FormattedError::createFromException($object);
×
43
        $error['message'] = $validationException->getMessage();
×
44

45
        $exceptionClass = $validationException::class;
×
46
        $statusCode = Response::HTTP_UNPROCESSABLE_ENTITY;
×
47

48
        foreach ($this->exceptionToStatus as $class => $status) {
×
49
            if (is_a($exceptionClass, $class, true)) {
×
50
                $statusCode = $status;
×
51

52
                break;
×
53
            }
54
        }
55
        $error['extensions']['status'] = $statusCode;
×
56
        // graphql-php < 15
57
        if (\defined(Error::class.'::CATEGORY_INTERNAL')) {
×
58
            $error['extensions']['category'] = 'user';
×
59
        }
60
        $error['extensions']['violations'] = [];
×
61

62
        /** @var ConstraintViolation $violation */
63
        foreach ($validationException->getConstraintViolationList() as $violation) {
×
64
            $error['extensions']['violations'][] = [
×
65
                'path' => $violation->getPropertyPath(),
×
66
                'message' => $violation->getMessage(),
×
67
            ];
×
68
        }
69

70
        return $error;
×
71
    }
72

73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
77
    {
78
        return $data instanceof Error && $data->getPrevious() instanceof ValidationException;
×
79
    }
80

81
    public function getSupportedTypes($format): array
82
    {
83
        return [
96✔
84
            Error::class => false,
96✔
85
        ];
96✔
86
    }
87
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc