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

api-platform / core / 10489772975

21 Aug 2024 12:22PM UTC coverage: 7.704%. Remained the same
10489772975

push

github

web-flow
doc: link monorepo to test laravel (#6530)

12476 of 161932 relevant lines covered (7.7%)

23.0 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(
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;
60✔
73

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

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

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

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

106
        return $id;
53✔
107
    }
108

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

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

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

129
    #[Groups(['jsonld', 'json', 'jsonapi'])]
130
    public function getTitle(): ?string
131
    {
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
    {
140
        return $this->detail;
56✔
141
    }
142

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

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

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

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

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

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

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

186
        return $message;
60✔
187
    }
188

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

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