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

api-platform / core / 7587980559

19 Jan 2024 06:55PM UTC coverage: 63.81% (+1.8%) from 61.988%
7587980559

push

github

web-flow
chore: components dependencies (#6113)

* chore: components dependencies

* test

23 of 70 new or added lines in 12 files covered. (32.86%)

925 existing lines in 37 files now uncovered.

16957 of 26574 relevant lines covered (63.81%)

31.24 hits per line

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

48.57
/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\JsonLd\ContextBuilderInterface;
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\ConstraintViolationListInterface;
27
use Symfony\Component\WebLink\Link;
28

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

73
    public function __construct(string|ConstraintViolationListInterface $message = '', string|int $code = null, int|\Throwable $previous = null, \Throwable|string $errorTitle = null)
74
    {
75
        $this->errorTitle = $errorTitle;
24✔
76

77
        if ($message instanceof ConstraintViolationListInterface) {
24✔
78
            $this->constraintViolationList = $message;
20✔
79
            parent::__construct($code ?? $this->__toString(), $previous ?? 0, $errorTitle instanceof \Throwable ? $errorTitle : null);
20✔
80

81
            return;
20✔
82
        }
83

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

88
    /**
89
     * @deprecated
90
     */
91
    public function getErrorTitle(): ?string
92
    {
93
        return $this->errorTitle;
12✔
94
    }
95

96
    public function getId(): string
97
    {
NEW
98
        $ids = [];
×
NEW
99
        foreach ($this->getConstraintViolationList() as $violation) {
×
NEW
100
            $ids[] = $violation->getCode();
×
101
        }
102

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

NEW
105
        if (!$id) {
×
NEW
106
            return spl_object_hash($this);
×
107
        }
108

NEW
109
        return $id;
×
110
    }
111

112
    #[SerializedName('hydra:title')]
113
    #[Groups(['jsonld'])]
114
    public function getHydraTitle(): string
115
    {
NEW
116
        return $this->errorTitle ?? 'An error occurred';
×
117
    }
118

119
    #[Groups(['jsonld'])]
120
    #[SerializedName('hydra:description')]
121
    public function getHydraDescription(): string
122
    {
NEW
123
        return $this->detail;
×
124
    }
125

126
    #[Groups(['jsonld', 'json'])]
127
    public function getType(): string
128
    {
NEW
129
        return '/validation_errors/'.$this->getId();
×
130
    }
131

132
    #[Groups(['jsonld', 'json'])]
133
    public function getTitle(): ?string
134
    {
NEW
135
        return $this->errorTitle ?? 'An error occurred';
×
136
    }
137

138
    #[Groups(['jsonld', 'json'])]
139
    private string $detail;
140

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

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

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

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

162
    #[Groups(['jsonld', 'json'])]
163
    public function getInstance(): ?string
164
    {
NEW
165
        return null;
×
166
    }
167

168
    #[SerializedName('violations')]
169
    #[Groups(['json', 'jsonld'])]
170
    public function getConstraintViolationList(): ConstraintViolationListInterface
171
    {
172
        return $this->constraintViolationList;
20✔
173
    }
174

175
    public function __toString(): string
176
    {
177
        $message = '';
24✔
178
        foreach ($this->getConstraintViolationList() as $violation) {
24✔
179
            if ('' !== $message) {
12✔
180
                $message .= "\n";
8✔
181
            }
182
            if ($propertyPath = $violation->getPropertyPath()) {
12✔
183
                $message .= "$propertyPath: ";
12✔
184
            }
185

186
            $message .= $violation->getMessage();
12✔
187
        }
188

189
        return $message;
24✔
190
    }
191

192
    public function getStatusCode(): int
193
    {
NEW
194
        return $this->status;
×
195
    }
196

197
    public function getHeaders(): array
198
    {
NEW
199
        return [];
×
200
    }
201
}
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