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

api-platform / core / 11176269767

04 Oct 2024 08:02AM UTC coverage: 7.725% (+0.3%) from 7.441%
11176269767

push

github

soyuka
chore: remove useless require-dev

12744 of 164973 relevant lines covered (7.72%)

27.04 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(
34
    uriTemplate: '/validation_errors/{id}',
35
    status: 422,
36
    openapi: false,
37
    uriVariables: ['id'],
38
    provider: 'api_platform.validator.state.error_provider',
39
    shortName: 'ConstraintViolationList',
40
    operations: [
41
        new ErrorOperation(
42
            name: '_api_validation_errors_problem',
43
            outputFormats: ['json' => ['application/problem+json']],
44
            normalizationContext: ['groups' => ['json'],
45
                'skip_null_values' => true,
46
            ]),
47
        new ErrorOperation(
48
            name: '_api_validation_errors_hydra',
49
            outputFormats: ['jsonld' => ['application/problem+json']],
50
            links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
51
            normalizationContext: [
52
                'groups' => ['jsonld'],
53
                'skip_null_values' => true,
54
            ]
55
        ),
56
        new ErrorOperation(
57
            name: '_api_validation_errors_jsonapi',
58
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
59
            normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true]
60
        ),
61
    ],
62
    graphQlOperations: []
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
    {
72
        $this->errorTitle = $errorTitle;
96✔
73

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

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

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

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

106
        return $id;
86✔
107
    }
108

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

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

121
    #[Groups(['jsonld', 'json', 'jsonapi'])]
122
    public function getTitle(): ?string
123
    {
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
    {
132
        return $this->detail;
89✔
133
    }
134

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

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

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

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

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

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

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

178
        return $message;
96✔
179
    }
180

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

186
    public function getHeaders(): array
187
    {
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