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

api-platform / core / 13200284839

07 Feb 2025 12:56PM UTC coverage: 0.0% (-8.2%) from 8.164%
13200284839

Pull #6952

github

web-flow
Merge 519fbf8cc into 62377f880
Pull Request #6952: fix: errors retrieval and documentation

0 of 206 new or added lines in 17 files covered. (0.0%)

10757 existing lines in 366 files now uncovered.

0 of 47781 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/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\Validator\Exception;
15

16
use ApiPlatform\Metadata\ApiProperty;
17
use ApiPlatform\Metadata\Error as ErrorOperation;
18
use ApiPlatform\Metadata\ErrorResource;
19
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
20
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
21
use ApiPlatform\Metadata\Exception\RuntimeException;
22
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
23
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
24
use Symfony\Component\Serializer\Annotation\Groups;
25
use Symfony\Component\Serializer\Annotation\SerializedName;
26
use Symfony\Component\Validator\ConstraintViolationList;
27
use Symfony\Component\Validator\ConstraintViolationListInterface;
28
use Symfony\Component\WebLink\Link;
29

30
/**
31
 * Thrown when a validation error occurs.
32
 *
33
 * @author Kévin Dunglas <dunglas@gmail.com>
34
 */
35
#[ErrorResource(
UNCOV
36
    uriTemplate: '/validation_errors/{id}',
×
UNCOV
37
    status: 422,
×
UNCOV
38
    uriVariables: ['id'],
×
NEW
39
    openapi: false,
×
NEW
40
    outputFormats: ['jsonapi' => ['application/vnd.api+json'], 'jsonld' => ['application/ld+json'], 'json' => ['application/problem+json', 'application/json']],
×
UNCOV
41
    provider: 'api_platform.validator.state.error_provider',
×
UNCOV
42
    shortName: 'ConstraintViolation',
×
UNCOV
43
    description: 'Unprocessable entity',
×
UNCOV
44
    operations: [
×
UNCOV
45
        new ErrorOperation(
×
UNCOV
46
            name: '_api_validation_errors_problem',
×
UNCOV
47
            outputFormats: ['json' => ['application/problem+json']],
×
UNCOV
48
            normalizationContext: [
×
UNCOV
49
                'groups' => ['json'],
×
UNCOV
50
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
UNCOV
51
                'skip_null_values' => true,
×
UNCOV
52
            ]
×
UNCOV
53
        ),
×
UNCOV
54
        new ErrorOperation(
×
UNCOV
55
            name: '_api_validation_errors_hydra',
×
UNCOV
56
            outputFormats: ['jsonld' => ['application/problem+json', 'application/ld+json']],
×
UNCOV
57
            links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
×
UNCOV
58
            normalizationContext: [
×
UNCOV
59
                'groups' => ['jsonld'],
×
UNCOV
60
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
UNCOV
61
                'skip_null_values' => true,
×
UNCOV
62
            ]
×
UNCOV
63
        ),
×
UNCOV
64
        new ErrorOperation(
×
UNCOV
65
            name: '_api_validation_errors_jsonapi',
×
UNCOV
66
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
×
UNCOV
67
            normalizationContext: [
×
UNCOV
68
                'disable_json_schema_serializer_groups' => false,
×
UNCOV
69
                'groups' => ['jsonapi'],
×
UNCOV
70
                'skip_null_values' => true,
×
UNCOV
71
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
UNCOV
72
            ]
×
UNCOV
73
        ),
×
UNCOV
74
    ],
×
UNCOV
75
    graphQlOperations: []
×
UNCOV
76
)]
×
77
#[ApiProperty(property: 'traceAsString', hydra: false)]
78
#[ApiProperty(property: 'string', hydra: false)]
79
class ValidationException extends RuntimeException implements ConstraintViolationListAwareExceptionInterface, \Stringable, ProblemExceptionInterface, HttpExceptionInterface, SymfonyHttpExceptionInterface
80
{
81
    private int $status = 422;
82
    protected ?string $errorTitle = null;
83
    private array|ConstraintViolationListInterface $constraintViolationList = [];
84

85
    public function __construct(ConstraintViolationListInterface $message = new ConstraintViolationList(), string|int|null $code = null, int|\Throwable|null $previous = null, \Throwable|string|null $errorTitle = null)
86
    {
UNCOV
87
        $this->errorTitle = $errorTitle;
×
UNCOV
88
        $this->constraintViolationList = $message;
×
UNCOV
89
        parent::__construct($this->__toString(), $code ?? 0, $previous);
×
90
    }
91

92
    /**
93
     * @deprecated
94
     */
95
    public function getErrorTitle(): ?string
96
    {
97
        return $this->errorTitle;
×
98
    }
99

100
    public function getId(): string
101
    {
UNCOV
102
        $ids = [];
×
UNCOV
103
        foreach ($this->getConstraintViolationList() as $violation) {
×
UNCOV
104
            $ids[] = $violation->getCode();
×
105
        }
106

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

UNCOV
109
        if (!$id) {
×
110
            return spl_object_hash($this);
×
111
        }
112

UNCOV
113
        return $id;
×
114
    }
115

116
    #[Groups(['jsonld'])]
117
    #[ApiProperty(writable: false, initializable: false)]
118
    public function getDescription(): string
119
    {
UNCOV
120
        return $this->detail;
×
121
    }
122

123
    #[Groups(['jsonld', 'json', 'jsonapi'])]
124
    #[ApiProperty(writable: false, initializable: false)]
125
    public function getType(): string
126
    {
UNCOV
127
        return '/validation_errors/'.$this->getId();
×
128
    }
129

130
    #[Groups(['jsonld', 'json', 'jsonapi'])]
131
    #[ApiProperty(writable: false, initializable: false)]
132
    public function getTitle(): ?string
133
    {
UNCOV
134
        return $this->errorTitle ?? 'An error occurred';
×
135
    }
136

137
    #[Groups(['jsonld', 'json', 'jsonapi'])]
138
    #[ApiProperty(writable: false, initializable: false)]
139
    private string $detail;
140

141
    public function getDetail(): ?string
142
    {
UNCOV
143
        return $this->detail;
×
144
    }
145

146
    public function setDetail(string $detail): void
147
    {
UNCOV
148
        $this->detail = $detail;
×
149
    }
150

151
    #[Groups(['jsonld', 'json', 'jsonapi'])]
152
    public function getStatus(): ?int
153
    {
UNCOV
154
        return $this->status;
×
155
    }
156

157
    public function setStatus(int $status): void
158
    {
UNCOV
159
        $this->status = $status;
×
160
    }
161

162
    #[Groups(['jsonld', 'json', 'jsonapi'])]
163
    #[ApiProperty(writable: false, initializable: false)]
164
    public function getInstance(): ?string
165
    {
UNCOV
166
        return null;
×
167
    }
168

169
    #[SerializedName('violations')]
170
    #[Groups(['json', 'jsonld'])]
171
    #[ApiProperty(
UNCOV
172
        jsonldContext: ['@type' => 'ConstraintViolationList'],
×
UNCOV
173
        schema: [
×
UNCOV
174
            'type' => 'array',
×
UNCOV
175
            'items' => [
×
UNCOV
176
                'type' => 'object',
×
UNCOV
177
                'properties' => [
×
UNCOV
178
                    'propertyPath' => ['type' => 'string', 'description' => 'The property path of the violation'],
×
UNCOV
179
                    'message' => ['type' => 'string', 'description' => 'The message associated with the violation'],
×
UNCOV
180
                ],
×
UNCOV
181
            ],
×
UNCOV
182
        ]
×
UNCOV
183
    )]
×
184
    public function getConstraintViolationList(): ConstraintViolationListInterface
185
    {
UNCOV
186
        return $this->constraintViolationList;
×
187
    }
188

189
    public function __toString(): string
190
    {
UNCOV
191
        $message = '';
×
UNCOV
192
        foreach ($this->getConstraintViolationList() as $violation) {
×
UNCOV
193
            if ('' !== $message) {
×
UNCOV
194
                $message .= "\n";
×
195
            }
UNCOV
196
            if ($propertyPath = $violation->getPropertyPath()) {
×
UNCOV
197
                $message .= "$propertyPath: ";
×
198
            }
199

UNCOV
200
            $message .= $violation->getMessage();
×
201
        }
202

UNCOV
203
        return $message;
×
204
    }
205

206
    public function getStatusCode(): int
207
    {
UNCOV
208
        return $this->status;
×
209
    }
210

211
    public function getHeaders(): array
212
    {
UNCOV
213
        return [];
×
214
    }
215
}
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