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

api-platform / core / 15283185369

27 May 2025 06:43PM UTC coverage: 0.0% (-26.4%) from 26.397%
15283185369

Pull #7180

github

web-flow
Merge 9a6703d44 into c022b441b
Pull Request #7180: feat(deps): allow Elasticsearch v9

0 of 51334 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\JsonSchema\SchemaFactory;
17
use ApiPlatform\Metadata\ApiProperty;
18
use ApiPlatform\Metadata\Error as ErrorOperation;
19
use ApiPlatform\Metadata\ErrorResource;
20
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
21
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
22
use ApiPlatform\Metadata\Exception\RuntimeException;
23
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
24
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
25
use Symfony\Component\Serializer\Annotation\Groups;
26
use Symfony\Component\Serializer\Annotation\SerializedName;
27
use Symfony\Component\Validator\ConstraintViolationList;
28
use Symfony\Component\Validator\ConstraintViolationListInterface;
29
use Symfony\Component\WebLink\Link;
30

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

89
    public function __construct(string|ConstraintViolationListInterface $message = new ConstraintViolationList(), string|int|null $code = null, int|\Throwable|null $previous = null, \Throwable|string|null $errorTitle = null)
90
    {
91
        $this->errorTitle = $errorTitle;
×
92

93
        if ($message instanceof ConstraintViolationListInterface) {
×
94
            $this->constraintViolationList = $message;
×
95
            parent::__construct($this->__toString(), $code ?? 0, $previous);
×
96

97
            return;
×
98
        }
99

100
        trigger_deprecation('api_platform/core', '5.0', \sprintf('The "%s" exception will have a "%s" first argument in 5.x.', self::class, ConstraintViolationListInterface::class));
×
101
        parent::__construct($message ?: $this->__toString(), $code ?? 0, $previous);
×
102
    }
103

104
    /**
105
     * @deprecated
106
     */
107
    public function getErrorTitle(): ?string
108
    {
109
        return $this->errorTitle;
×
110
    }
111

112
    public function getId(): string
113
    {
114
        $ids = [];
×
115
        foreach ($this->getConstraintViolationList() as $violation) {
×
116
            $ids[] = $violation->getCode();
×
117
        }
118

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

121
        if (!$id) {
×
122
            return spl_object_hash($this);
×
123
        }
124

125
        return $id;
×
126
    }
127

128
    #[Groups(['jsonld'])]
129
    #[ApiProperty(writable: false, initializable: false)]
130
    public function getDescription(): string
131
    {
132
        return $this->detail;
×
133
    }
134

135
    #[Groups(['jsonld', 'json', 'jsonapi'])]
136
    #[ApiProperty(writable: false, initializable: false)]
137
    public function getType(): string
138
    {
139
        return '/validation_errors/'.$this->getId();
×
140
    }
141

142
    #[Groups(['jsonld', 'json', 'jsonapi'])]
143
    #[ApiProperty(writable: false, initializable: false)]
144
    public function getTitle(): ?string
145
    {
146
        return $this->errorTitle ?? 'An error occurred';
×
147
    }
148

149
    #[Groups(['jsonld', 'json', 'jsonapi'])]
150
    #[ApiProperty(writable: false, initializable: false)]
151
    private string $detail;
152

153
    public function getDetail(): ?string
154
    {
155
        return $this->detail;
×
156
    }
157

158
    public function setDetail(string $detail): void
159
    {
160
        $this->detail = $detail;
×
161
    }
162

163
    #[Groups(['jsonld', 'json', 'jsonapi'])]
164
    public function getStatus(): ?int
165
    {
166
        return $this->status;
×
167
    }
168

169
    public function setStatus(int $status): void
170
    {
171
        $this->status = $status;
×
172
    }
173

174
    #[Groups(['jsonld', 'json', 'jsonapi'])]
175
    #[ApiProperty(writable: false, initializable: false)]
176
    public function getInstance(): ?string
177
    {
178
        return null;
×
179
    }
180

181
    #[SerializedName('violations')]
182
    #[Groups(['json', 'jsonld'])]
183
    #[ApiProperty(
184
        jsonldContext: ['@type' => 'ConstraintViolationList'],
×
185
        schema: [
×
186
            'type' => 'array',
×
187
            'items' => [
×
188
                'type' => 'object',
×
189
                'properties' => [
×
190
                    'propertyPath' => ['type' => 'string', 'description' => 'The property path of the violation'],
×
191
                    'message' => ['type' => 'string', 'description' => 'The message associated with the violation'],
×
192
                ],
×
193
            ],
×
194
        ]
×
195
    )]
×
196
    public function getConstraintViolationList(): ConstraintViolationListInterface
197
    {
198
        return $this->constraintViolationList;
×
199
    }
200

201
    public function __toString(): string
202
    {
203
        $message = '';
×
204
        foreach ($this->getConstraintViolationList() as $violation) {
×
205
            if ('' !== $message) {
×
206
                $message .= "\n";
×
207
            }
208
            if ($propertyPath = $violation->getPropertyPath()) {
×
209
                $message .= "$propertyPath: ";
×
210
            }
211

212
            $message .= $violation->getMessage();
×
213
        }
214

215
        return $message;
×
216
    }
217

218
    public function getStatusCode(): int
219
    {
220
        return $this->status;
×
221
    }
222

223
    public function getHeaders(): array
224
    {
225
        return [];
×
226
    }
227
}
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