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

api-platform / core / 6198903018

15 Sep 2023 01:50PM UTC coverage: 37.037% (+0.04%) from 36.999%
6198903018

push

github

web-flow
fix: exception to status on error resource (#5823)

52 of 52 new or added lines in 12 files covered. (100.0%)

10121 of 27327 relevant lines covered (37.04%)

19.98 hits per line

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

28.57
/src/Symfony/Validator/Exception/ValidationException.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\Exception;
15

16
use ApiPlatform\Metadata\Error as ErrorOperation;
17
use ApiPlatform\Metadata\ErrorResource;
18
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
19
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
20
use ApiPlatform\Validator\Exception\ValidationException as BaseValidationException;
21
use Symfony\Component\Serializer\Annotation\Groups;
22
use Symfony\Component\Serializer\Annotation\SerializedName;
23
use Symfony\Component\Validator\ConstraintViolationListInterface;
24

25
/**
26
 * Thrown when a validation error occurs.
27
 *
28
 * @author Kévin Dunglas <dunglas@gmail.com>
29
 */
30
#[ErrorResource(
31
    uriTemplate: '/validation_errors/{id}',
32
    status: 422,
33
    openapi: false,
34
    uriVariables: ['id'],
35
    shortName: 'ConstraintViolationList',
36
    operations: [
37
        new ErrorOperation(name: '_api_validation_errors_hydra', outputFormats: ['jsonld' => ['application/ld+json']], normalizationContext: ['groups' => ['jsonld'], 'skip_null_values' => true]),
38
        new ErrorOperation(name: '_api_validation_errors_problem', outputFormats: ['jsonproblem' => ['application/problem+json'], 'json' => ['application/problem+json']], normalizationContext: ['groups' => ['json'], 'skip_null_values' => true]),
39
        new ErrorOperation(name: '_api_validation_errors_jsonapi', outputFormats: ['jsonapi' => ['application/vnd.api+json']], normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true]),
40
    ]
41
)]
42
final class ValidationException extends BaseValidationException implements ConstraintViolationListAwareExceptionInterface, \Stringable, ProblemExceptionInterface
43
{
44
    private int $status = 422;
45

46
    public function __construct(private readonly ConstraintViolationListInterface $constraintViolationList, string $message = '', int $code = 0, \Throwable $previous = null, string $errorTitle = null)
47
    {
48
        parent::__construct($message ?: $this->__toString(), $code, $previous, $errorTitle);
15✔
49
    }
50

51
    public function getConstraintViolationList(): ConstraintViolationListInterface
52
    {
53
        return $this->constraintViolationList;
9✔
54
    }
55

56
    public function getId(): string
57
    {
58
        $ids = [];
×
59
        foreach ($this->getConstraintViolationList() as $violation) {
×
60
            $ids[] = $violation->getCode();
×
61
        }
62

63
        $id = 1 < \count($ids) ? CompositeIdentifierParser::stringify(identifiers: $ids) : ($ids[0] ?? null);
×
64

65
        if (!$id) {
×
66
            return spl_object_hash($this);
×
67
        }
68

69
        return $id;
×
70
    }
71

72
    public function __toString(): string
73
    {
74
        $message = '';
15✔
75
        foreach ($this->constraintViolationList as $violation) {
15✔
76
            if ('' !== $message) {
6✔
77
                $message .= "\n";
3✔
78
            }
79
            if ($propertyPath = $violation->getPropertyPath()) {
6✔
80
                $message .= "$propertyPath: ";
6✔
81
            }
82

83
            $message .= $violation->getMessage();
6✔
84
        }
85

86
        return $message;
15✔
87
    }
88

89
    #[SerializedName('hydra:title')]
90
    #[Groups(['jsonld', 'legacy_jsonld'])]
91
    public function getHydraTitle(): string
92
    {
93
        return $this->errorTitle ?? 'An error occurred';
×
94
    }
95

96
    #[SerializedName('hydra:description')]
97
    #[Groups(['jsonld', 'legacy_jsonld'])]
98
    public function getHydraDescription(): string
99
    {
100
        return $this->__toString();
×
101
    }
102

103
    #[Groups(['jsonld', 'json', 'legacy_jsonproblem', 'legacy_json'])]
104
    public function getType(): string
105
    {
106
        return '/validation_errors/'.$this->getId();
×
107
    }
108

109
    #[Groups(['jsonld', 'json', 'legacy_jsonproblem', 'legacy_json'])]
110
    public function getTitle(): ?string
111
    {
112
        return $this->errorTitle ?? 'An error occurred';
×
113
    }
114

115
    #[Groups(['jsonld', 'json', 'legacy_jsonproblem', 'legacy_json'])]
116
    public function getDetail(): ?string
117
    {
118
        return $this->__toString();
×
119
    }
120

121
    #[Groups(['jsonld', 'json', 'legacy_jsonproblem', 'legacy_json'])]
122
    public function getStatus(): ?int
123
    {
124
        return $this->status;
×
125
    }
126

127
    public function setStatus(int $status): void
128
    {
129
        $this->status = $status;
×
130
    }
131

132
    #[Groups(['jsonld', 'json'])]
133
    public function getInstance(): ?string
134
    {
135
        return null;
×
136
    }
137

138
    #[SerializedName('violations')]
139
    #[Groups(['json', 'jsonld', 'legacy_jsonld', 'legacy_jsonproblem', 'legacy_json'])]
140
    public function getViolations(): iterable
141
    {
142
        foreach ($this->getConstraintViolationList() as $violation) {
×
143
            $propertyPath = $violation->getPropertyPath();
×
144
            $violationData = [
×
145
                'propertyPath' => $propertyPath,
×
146
                'message' => $violation->getMessage(),
×
147
                'code' => $violation->getCode(),
×
148
            ];
×
149

150
            if ($hint = $violation->getParameters()['hint'] ?? false) {
×
151
                $violationData['hint'] = $hint;
×
152
            }
153

154
            yield $violationData;
×
155
        }
156
    }
157
}
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