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

api-platform / core / 13200284839

07 Feb 2025 12:56PM UTC coverage: 0.0% (-8.2%) from 8.164%
13200284839

Pull #6952

github

web-flow
Merge 519fbf8cc into 62377f880
Pull Request #6952: fix: errors retrieval and documentation

0 of 206 new or added lines in 17 files covered. (0.0%)

10757 existing lines in 366 files now uncovered.

0 of 47781 relevant lines covered (0.0%)

0.0 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(
×
41
            name: '_api_validation_errors_problem',
×
42
            outputFormats: ['json' => ['application/problem+json']],
×
NEW
43
            normalizationContext: [
×
NEW
44
                'groups' => ['json'],
×
45
                'skip_null_values' => true,
×
NEW
46
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
47
            ],
×
48
        ),
×
49
        new ErrorOperation(
×
50
            name: '_api_validation_errors_hydra',
×
51
            outputFormats: ['jsonld' => ['application/problem+json']],
×
52
            links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
×
53
            normalizationContext: [
×
54
                'groups' => ['jsonld'],
×
55
                'skip_null_values' => true,
×
NEW
56
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
57
            ],
×
58
        ),
×
59
        new ErrorOperation(
×
60
            name: '_api_validation_errors_jsonapi',
×
61
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
×
62
            normalizationContext: [
×
63
                'groups' => ['jsonapi'],
×
64
                'skip_null_values' => true,
×
NEW
65
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
66
            ],
×
67
        ),
×
68
    ],
×
69
    graphQlOperations: []
×
70
)]
×
71
class ValidationError extends RuntimeException implements \Stringable, ProblemExceptionInterface, HttpExceptionInterface, SymfonyHttpExceptionInterface
72
{
73
    private int $status = 422;
74
    private string $id;
75

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

86
    public function getId(): string
87
    {
88
        return $this->id;
×
89
    }
90

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

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

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

110
    #[Groups(['jsonld', 'json', 'jsonapi'])]
111
    private string $detail;
112

113
    public function getDetail(): ?string
114
    {
115
        return $this->detail;
×
116
    }
117

118
    public function setDetail(string $detail): void
119
    {
120
        $this->detail = $detail;
×
121
    }
122

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

129
    public function setStatus(int $status): void
130
    {
131
        $this->status = $status;
×
132
    }
133

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

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

163
    public function getStatusCode(): int
164
    {
165
        return $this->status;
×
166
    }
167

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