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

api-platform / core / 10845872733

12 Sep 2024 02:08PM UTC coverage: 69.746%. Remained the same
10845872733

push

github

soyuka
fix(state): correct deserializer_type type

3163 of 4535 relevant lines covered (69.75%)

73.99 hits per line

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

100.0
/src/Symfony/Validator/EventListener/ValidationExceptionListener.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\Symfony\Validator\EventListener;
15

16
use ApiPlatform\Exception\FilterValidationException;
17
use ApiPlatform\ParameterValidator\Exception\ValidationException as ParameterValidationException;
18
use ApiPlatform\Symfony\EventListener\ExceptionListener;
19
use ApiPlatform\Symfony\Validator\Exception\ConstraintViolationListAwareExceptionInterface as SymfonyConstraintViolationListAwareExceptionInterface;
20
use ApiPlatform\Util\ErrorFormatGuesser;
21
use ApiPlatform\Validator\Exception\ConstraintViolationListAwareExceptionInterface;
22
use ApiPlatform\Validator\Exception\ValidationException;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
25
use Symfony\Component\Serializer\SerializerInterface;
26

27
/**
28
 * Handles validation errors.
29
 * TODO: remove this class.
30
 *
31
 * @deprecated
32
 *
33
 * @author Kévin Dunglas <dunglas@gmail.com>
34
 */
35
final class ValidationExceptionListener
36
{
37
    public function __construct(private readonly SerializerInterface $serializer, private readonly array $errorFormats, private readonly array $exceptionToStatus = [], private readonly ?ExceptionListener $exceptionListener = null)
38
    {
39
    }
134✔
40

41
    /**
42
     * Returns a list of violations normalized in the Hydra format.
43
     */
44
    public function onKernelException(ExceptionEvent $event): void
45
    {
46
        // API Platform 3.2 handles every exception through the exception listener so we just skip this one
47
        if ($this->exceptionListener) {
37✔
48
            return;
17✔
49
        }
50

51
        trigger_deprecation('api-platform', '3.2', \sprintf('The class "%s" is deprecated and will be removed in 4.x.', __CLASS__));
20✔
52

53
        $exception = $event->getThrowable();
20✔
54
        $hasConstraintViolationList = ($exception instanceof ConstraintViolationListAwareExceptionInterface || $exception instanceof SymfonyConstraintViolationListAwareExceptionInterface);
20✔
55
        if (!$hasConstraintViolationList && !$exception instanceof FilterValidationException && !$exception instanceof ParameterValidationException) {
20✔
56
            return;
4✔
57
        }
58

59
        $exceptionClass = $exception::class;
16✔
60
        $statusCode = Response::HTTP_UNPROCESSABLE_ENTITY;
16✔
61

62
        foreach ($this->exceptionToStatus as $class => $status) {
16✔
63
            if (is_a($exceptionClass, $class, true)) {
4✔
64
                $statusCode = $status;
4✔
65

66
                break;
4✔
67
            }
68
        }
69

70
        $format = ErrorFormatGuesser::guessErrorFormat($event->getRequest(), $this->errorFormats);
16✔
71

72
        $context = [];
16✔
73
        if ($exception instanceof ValidationException && ($errorTitle = $exception->getErrorTitle())) {
16✔
74
            $context['title'] = $errorTitle;
4✔
75
        }
76

77
        $event->setResponse(new Response(
16✔
78
            $this->serializer->serialize($hasConstraintViolationList ? $exception->getConstraintViolationList() : $exception, $format['key'], $context),
16✔
79
            $statusCode,
16✔
80
            [
16✔
81
                'Content-Type' => \sprintf('%s; charset=utf-8', $format['value'][0]),
16✔
82
                'X-Content-Type-Options' => 'nosniff',
16✔
83
                'X-Frame-Options' => 'deny',
16✔
84
            ]
16✔
85
        ));
16✔
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

© 2025 Coveralls, Inc