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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

84.62
/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\Metadata\Exception\RuntimeException;
17
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
18
use GraphQL\Error\Error;
19
use GraphQL\Error\FormattedError;
20
use Symfony\Component\HttpFoundation\Response;
21
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
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
    }
1,160✔
34

35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function normalize(mixed $object, ?string $format = null, array $context = []): array
39
    {
UNCOV
40
        $validationException = $object->getPrevious();
1✔
UNCOV
41
        if (!$validationException instanceof ConstraintViolationListAwareExceptionInterface) {
1✔
42
            throw new RuntimeException(\sprintf('Object is not a "%s".', ConstraintViolationListAwareExceptionInterface::class));
×
43
        }
44

UNCOV
45
        $error = FormattedError::createFromException($object);
1✔
UNCOV
46
        $error['message'] = $validationException->getMessage();
1✔
47

UNCOV
48
        $exceptionClass = $validationException::class;
1✔
UNCOV
49
        $statusCode = Response::HTTP_UNPROCESSABLE_ENTITY;
1✔
50

UNCOV
51
        foreach ($this->exceptionToStatus as $class => $status) {
1✔
UNCOV
52
            if (is_a($exceptionClass, $class, true)) {
1✔
53
                $statusCode = $status;
×
54

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

UNCOV
65
        foreach ($validationException->getConstraintViolationList() as $violation) {
1✔
UNCOV
66
            $error['extensions']['violations'][] = [
1✔
UNCOV
67
                'path' => $violation->getPropertyPath(),
1✔
UNCOV
68
                'message' => $violation->getMessage(),
1✔
UNCOV
69
            ];
1✔
70
        }
71

UNCOV
72
        return $error;
1✔
73
    }
74

75
    /**
76
     * {@inheritdoc}
77
     */
78
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
79
    {
80
        return $data instanceof Error && ($data->getPrevious() instanceof ConstraintViolationListAwareExceptionInterface);
23✔
81
    }
82

83
    public function getSupportedTypes($format): array
84
    {
85
        return [
1,086✔
86
            Error::class => false,
1,086✔
87
        ];
1,086✔
88
    }
89
}
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

© 2025 Coveralls, Inc