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

api-platform / core / 13202118186

07 Feb 2025 02:44PM UTC coverage: 8.371% (+0.2%) from 8.164%
13202118186

push

github

web-flow
fix: errors retrieval and documentation (#6952)

64 of 235 new or added lines in 22 files covered. (27.23%)

21 existing lines in 11 files now uncovered.

13140 of 156979 relevant lines covered (8.37%)

22.88 hits per line

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

0.0
/src/Laravel/ApiResource/ValidationError.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\Laravel\ApiResource;
15

16
use ApiPlatform\Metadata\ApiProperty;
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 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\WebLink\Link;
26

27
/**
28
 * Thrown when a validation error occurs.
29
 *
30
 * @author Kévin Dunglas <dunglas@gmail.com>
31
 */
32
#[ErrorResource(
33
    uriTemplate: '/validation_errors/{id}',
×
34
    status: 422,
×
35
    openapi: false,
×
NEW
36
    outputFormats: ['jsonapi' => ['application/vnd.api+json'], 'jsonld' => ['application/ld+json'], 'json' => ['application/problem+json', 'application/json']],
×
37
    uriVariables: ['id'],
×
38
    shortName: 'ValidationError',
×
39
    operations: [
×
40
        new ErrorOperation(
×
NEW
41
            routeName: 'api_validation_errors',
×
42
            name: '_api_validation_errors_problem',
×
43
            outputFormats: ['json' => ['application/problem+json']],
×
NEW
44
            normalizationContext: [
×
NEW
45
                'groups' => ['json'],
×
46
                'skip_null_values' => true,
×
NEW
47
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
48
            ],
×
49
        ),
×
50
        new ErrorOperation(
×
51
            name: '_api_validation_errors_hydra',
×
NEW
52
            routeName: 'api_validation_errors',
×
53
            outputFormats: ['jsonld' => ['application/problem+json']],
×
54
            links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
×
55
            normalizationContext: [
×
56
                'groups' => ['jsonld'],
×
57
                'skip_null_values' => true,
×
NEW
58
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
59
            ],
×
60
        ),
×
61
        new ErrorOperation(
×
62
            name: '_api_validation_errors_jsonapi',
×
NEW
63
            routeName: 'api_validation_errors',
×
64
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
×
65
            normalizationContext: [
×
66
                'groups' => ['jsonapi'],
×
67
                'skip_null_values' => true,
×
NEW
68
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
69
            ],
×
70
        ),
×
71
    ],
×
72
    graphQlOperations: []
×
73
)]
×
74
class ValidationError extends RuntimeException implements \Stringable, ProblemExceptionInterface, HttpExceptionInterface, SymfonyHttpExceptionInterface
75
{
76
    private int $status = 422;
77
    private string $id;
78

79
    /**
80
     * @param array<int, array{propertyPath: string, message: string, code?: string}> $violations
81
     */
82
    public function __construct(string $message = '', mixed $code = null, ?\Throwable $previous = null, protected array $violations = [])
83
    {
84
        $this->id = (string) $code;
×
85
        $this->setDetail($message);
×
86
        parent::__construct($message ?: $this->__toString(), 422, $previous);
×
87
    }
88

89
    public function getId(): string
90
    {
91
        return $this->id;
×
92
    }
93

94
    #[SerializedName('description')]
95
    #[Groups(['jsonld', 'json'])]
96
    public function getDescription(): string
97
    {
98
        return $this->detail;
×
99
    }
100

101
    #[Groups(['jsonld', 'json', 'jsonapi'])]
102
    public function getType(): string
103
    {
104
        return '/validation_errors/'.$this->id;
×
105
    }
106

107
    #[Groups(['jsonld', 'json', 'jsonapi'])]
108
    public function getTitle(): ?string
109
    {
110
        return 'Validation Error';
×
111
    }
112

113
    #[Groups(['jsonld', 'json', 'jsonapi'])]
114
    private string $detail;
115

116
    public function getDetail(): ?string
117
    {
118
        return $this->detail;
×
119
    }
120

121
    public function setDetail(string $detail): void
122
    {
123
        $this->detail = $detail;
×
124
    }
125

126
    #[Groups(['jsonld', 'json', 'jsonapi'])]
127
    public function getStatus(): ?int
128
    {
129
        return $this->status;
×
130
    }
131

132
    public function setStatus(int $status): void
133
    {
134
        $this->status = $status;
×
135
    }
136

137
    #[Groups(['jsonld', 'json', 'jsonapi'])]
138
    public function getInstance(): ?string
139
    {
140
        return null;
×
141
    }
142

143
    /**
144
     * @return array<int,array{propertyPath:string,message:string,code?:string}>
145
     */
146
    #[SerializedName('violations')]
147
    #[Groups(['json', 'jsonld', 'jsonapi'])]
148
    #[ApiProperty(
NEW
149
        jsonldContext: ['@type' => 'ConstraintViolationList'],
×
NEW
150
        schema: [
×
NEW
151
            'type' => 'array',
×
NEW
152
            'items' => [
×
NEW
153
                'type' => 'object',
×
NEW
154
                'properties' => [
×
NEW
155
                    'propertyPath' => ['type' => 'string', 'description' => 'The property path of the violation'],
×
NEW
156
                    'message' => ['type' => 'string', 'description' => 'The message associated with the violation'],
×
NEW
157
                ],
×
NEW
158
            ],
×
NEW
159
        ]
×
NEW
160
    )]
×
161
    public function getViolations(): array
162
    {
163
        return $this->violations;
×
164
    }
165

166
    public function getStatusCode(): int
167
    {
168
        return $this->status;
×
169
    }
170

171
    /**
172
     * @return array<string, string>
173
     */
174
    public function getHeaders(): array
175
    {
176
        return [];
×
177
    }
178
}
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

© 2026 Coveralls, Inc