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

api-platform / core / 10729306835

05 Sep 2024 10:46PM UTC coverage: 7.655% (-0.01%) from 7.665%
10729306835

push

github

web-flow
Merge pull request #6586 from soyuka/merge-342

Merge 3.4

0 of 54 new or added lines in 12 files covered. (0.0%)

8760 existing lines in 277 files now uncovered.

12505 of 163357 relevant lines covered (7.66%)

22.84 hits per line

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

91.43
/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\Error as ErrorOperation;
17
use ApiPlatform\Metadata\ErrorResource;
18
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
19
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
20
use ApiPlatform\Metadata\Exception\RuntimeException;
21
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
22
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
23
use Symfony\Component\Serializer\Annotation\Groups;
24
use Symfony\Component\Serializer\Annotation\SerializedName;
25
use Symfony\Component\Validator\ConstraintViolationListInterface;
26
use Symfony\Component\WebLink\Link;
27

28
/**
29
 * Thrown when a validation error occurs.
30
 *
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
#[ErrorResource(
UNCOV
34
    uriTemplate: '/validation_errors/{id}',
UNCOV
35
    status: 422,
UNCOV
36
    openapi: false,
UNCOV
37
    uriVariables: ['id'],
UNCOV
38
    provider: 'api_platform.validator.state.error_provider',
UNCOV
39
    shortName: 'ConstraintViolationList',
UNCOV
40
    operations: [
UNCOV
41
        new ErrorOperation(
UNCOV
42
            name: '_api_validation_errors_problem',
UNCOV
43
            outputFormats: ['json' => ['application/problem+json']],
UNCOV
44
            normalizationContext: ['groups' => ['json'],
UNCOV
45
                'skip_null_values' => true,
UNCOV
46
            ]),
UNCOV
47
        new ErrorOperation(
UNCOV
48
            name: '_api_validation_errors_hydra',
UNCOV
49
            outputFormats: ['jsonld' => ['application/problem+json']],
UNCOV
50
            links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
UNCOV
51
            normalizationContext: [
UNCOV
52
                'groups' => ['jsonld'],
UNCOV
53
                'skip_null_values' => true,
UNCOV
54
            ]
UNCOV
55
        ),
UNCOV
56
        new ErrorOperation(
UNCOV
57
            name: '_api_validation_errors_jsonapi',
UNCOV
58
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
UNCOV
59
            normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true]
UNCOV
60
        ),
UNCOV
61
    ],
UNCOV
62
    graphQlOperations: []
UNCOV
63
)]
64
class ValidationException extends RuntimeException implements ConstraintViolationListAwareExceptionInterface, \Stringable, ProblemExceptionInterface, HttpExceptionInterface, SymfonyHttpExceptionInterface
65
{
66
    private int $status = 422;
67
    protected ?string $errorTitle = null;
68
    private ConstraintViolationListInterface $constraintViolationList;
69

70
    public function __construct(string|ConstraintViolationListInterface $message = '', string|int|null $code = null, int|\Throwable|null $previous = null, \Throwable|string|null $errorTitle = null)
71
    {
UNCOV
72
        $this->errorTitle = $errorTitle;
60✔
73

UNCOV
74
        if ($message instanceof ConstraintViolationListInterface) {
60✔
UNCOV
75
            $this->constraintViolationList = $message;
60✔
UNCOV
76
            parent::__construct($code ?? $this->__toString(), $previous ?? 0, $errorTitle instanceof \Throwable ? $errorTitle : null);
60✔
77

UNCOV
78
            return;
60✔
79
        }
80

81
        trigger_deprecation('api_platform/core', '3.3', \sprintf('The "%s" exception will have a "%s" first argument in 4.x.', self::class, ConstraintViolationListInterface::class));
×
82
        parent::__construct($message ?: $this->__toString(), $code ?? 0, $previous);
×
83
    }
84

85
    /**
86
     * @deprecated
87
     */
88
    public function getErrorTitle(): ?string
89
    {
90
        return $this->errorTitle;
×
91
    }
92

93
    public function getId(): string
94
    {
UNCOV
95
        $ids = [];
56✔
UNCOV
96
        foreach ($this->getConstraintViolationList() as $violation) {
56✔
UNCOV
97
            $ids[] = $violation->getCode();
53✔
98
        }
99

UNCOV
100
        $id = 1 < \count($ids) ? CompositeIdentifierParser::stringify(identifiers: $ids) : ($ids[0] ?? null);
56✔
101

UNCOV
102
        if (!$id) {
56✔
103
            return spl_object_hash($this);
3✔
104
        }
105

UNCOV
106
        return $id;
53✔
107
    }
108

109
    #[SerializedName('hydra:title')]
110
    #[Groups(['jsonld'])]
111
    public function getHydraTitle(): string
112
    {
UNCOV
113
        return $this->errorTitle ?? 'An error occurred';
18✔
114
    }
115

116
    #[Groups(['jsonld'])]
117
    #[SerializedName('hydra:description')]
118
    public function getHydraDescription(): string
119
    {
UNCOV
120
        return $this->detail;
18✔
121
    }
122

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

129
    #[Groups(['jsonld', 'json', 'jsonapi'])]
130
    public function getTitle(): ?string
131
    {
UNCOV
132
        return $this->errorTitle ?? 'An error occurred';
56✔
133
    }
134

135
    #[Groups(['jsonld', 'json', 'jsonapi'])]
136
    private string $detail;
137

138
    public function getDetail(): ?string
139
    {
UNCOV
140
        return $this->detail;
56✔
141
    }
142

143
    public function setDetail(string $detail): void
144
    {
UNCOV
145
        $this->detail = $detail;
56✔
146
    }
147

148
    #[Groups(['jsonld', 'json', 'jsonapi'])]
149
    public function getStatus(): ?int
150
    {
UNCOV
151
        return $this->status;
56✔
152
    }
153

154
    public function setStatus(int $status): void
155
    {
UNCOV
156
        $this->status = $status;
57✔
157
    }
158

159
    #[Groups(['jsonld', 'json', 'jsonapi'])]
160
    public function getInstance(): ?string
161
    {
UNCOV
162
        return null;
56✔
163
    }
164

165
    #[SerializedName('violations')]
166
    #[Groups(['json', 'jsonld'])]
167
    public function getConstraintViolationList(): ConstraintViolationListInterface
168
    {
UNCOV
169
        return $this->constraintViolationList;
60✔
170
    }
171

172
    public function __toString(): string
173
    {
UNCOV
174
        $message = '';
60✔
UNCOV
175
        foreach ($this->getConstraintViolationList() as $violation) {
60✔
UNCOV
176
            if ('' !== $message) {
57✔
UNCOV
177
                $message .= "\n";
7✔
178
            }
UNCOV
179
            if ($propertyPath = $violation->getPropertyPath()) {
57✔
UNCOV
180
                $message .= "$propertyPath: ";
57✔
181
            }
182

UNCOV
183
            $message .= $violation->getMessage();
57✔
184
        }
185

UNCOV
186
        return $message;
60✔
187
    }
188

189
    public function getStatusCode(): int
190
    {
UNCOV
191
        return $this->status;
57✔
192
    }
193

194
    public function getHeaders(): array
195
    {
UNCOV
196
        return [];
57✔
197
    }
198
}
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