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

api-platform / core / 11182119487

04 Oct 2024 02:50PM UTC coverage: 7.837% (+0.4%) from 7.441%
11182119487

push

github

soyuka
Merge 4.0

0 of 266 new or added lines in 25 files covered. (0.0%)

9900 existing lines in 360 files now uncovered.

12939 of 165109 relevant lines covered (7.84%)

27.02 hits per line

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

91.18
/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;
96✔
73

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

UNCOV
78
            return;
96✔
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 = [];
89✔
UNCOV
96
        foreach ($this->getConstraintViolationList() as $violation) {
89✔
UNCOV
97
            $ids[] = $violation->getCode();
86✔
98
        }
99

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

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

UNCOV
106
        return $id;
86✔
107
    }
108

109
    #[Groups(['jsonld'])]
110
    public function getDescription(): string
111
    {
UNCOV
112
        return $this->detail;
51✔
113
    }
114

115
    #[Groups(['jsonld', 'json', 'jsonapi'])]
116
    public function getType(): string
117
    {
UNCOV
118
        return '/validation_errors/'.$this->getId();
89✔
119
    }
120

121
    #[Groups(['jsonld', 'json', 'jsonapi'])]
122
    public function getTitle(): ?string
123
    {
UNCOV
124
        return $this->errorTitle ?? 'An error occurred';
89✔
125
    }
126

127
    #[Groups(['jsonld', 'json', 'jsonapi'])]
128
    private string $detail;
129

130
    public function getDetail(): ?string
131
    {
UNCOV
132
        return $this->detail;
89✔
133
    }
134

135
    public function setDetail(string $detail): void
136
    {
UNCOV
137
        $this->detail = $detail;
89✔
138
    }
139

140
    #[Groups(['jsonld', 'json', 'jsonapi'])]
141
    public function getStatus(): ?int
142
    {
UNCOV
143
        return $this->status;
89✔
144
    }
145

146
    public function setStatus(int $status): void
147
    {
UNCOV
148
        $this->status = $status;
90✔
149
    }
150

151
    #[Groups(['jsonld', 'json', 'jsonapi'])]
152
    public function getInstance(): ?string
153
    {
UNCOV
154
        return null;
89✔
155
    }
156

157
    #[SerializedName('violations')]
158
    #[Groups(['json', 'jsonld'])]
159
    public function getConstraintViolationList(): ConstraintViolationListInterface
160
    {
UNCOV
161
        return $this->constraintViolationList;
96✔
162
    }
163

164
    public function __toString(): string
165
    {
UNCOV
166
        $message = '';
96✔
UNCOV
167
        foreach ($this->getConstraintViolationList() as $violation) {
96✔
UNCOV
168
            if ('' !== $message) {
93✔
UNCOV
169
                $message .= "\n";
16✔
170
            }
UNCOV
171
            if ($propertyPath = $violation->getPropertyPath()) {
93✔
UNCOV
172
                $message .= "$propertyPath: ";
93✔
173
            }
174

UNCOV
175
            $message .= $violation->getMessage();
93✔
176
        }
177

UNCOV
178
        return $message;
96✔
179
    }
180

181
    public function getStatusCode(): int
182
    {
UNCOV
183
        return $this->status;
90✔
184
    }
185

186
    public function getHeaders(): array
187
    {
UNCOV
188
        return [];
90✔
189
    }
190
}
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