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

api-platform / core / 13175753759

06 Feb 2025 09:30AM UTC coverage: 0.0% (-7.7%) from 7.663%
13175753759

Pull #6947

github

web-flow
Merge 432a515ad into de2d298e3
Pull Request #6947: fix(metadata): remove temporary fix for ArrayCollection

0 of 1 new or added line in 1 file covered. (0.0%)

11655 existing lines in 385 files now uncovered.

0 of 47351 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/State/ApiResource/Error.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\State\ApiResource;
15

16
use ApiPlatform\Metadata\ApiProperty;
17
use ApiPlatform\Metadata\Error as Operation;
18
use ApiPlatform\Metadata\ErrorResource;
19
use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
20
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
21
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
22
use Symfony\Component\Serializer\Annotation\Groups;
23
use Symfony\Component\Serializer\Annotation\Ignore;
24
use Symfony\Component\Serializer\Annotation\SerializedName;
25
use Symfony\Component\WebLink\Link;
26

27
#[ErrorResource(
UNCOV
28
    openapi: false,
×
UNCOV
29
    uriVariables: ['status'],
×
UNCOV
30
    uriTemplate: '/errors/{status}',
×
UNCOV
31
    operations: [
×
UNCOV
32
        new Operation(
×
UNCOV
33
            name: '_api_errors_problem',
×
UNCOV
34
            routeName: 'api_errors',
×
UNCOV
35
            outputFormats: ['json' => ['application/problem+json']],
×
UNCOV
36
            normalizationContext: [
×
UNCOV
37
                'groups' => ['jsonproblem'],
×
UNCOV
38
                'skip_null_values' => true,
×
UNCOV
39
            ],
×
UNCOV
40
        ),
×
UNCOV
41
        new Operation(
×
UNCOV
42
            name: '_api_errors_hydra',
×
UNCOV
43
            routeName: 'api_errors',
×
UNCOV
44
            outputFormats: ['jsonld' => ['application/problem+json']],
×
UNCOV
45
            normalizationContext: [
×
UNCOV
46
                'groups' => ['jsonld'],
×
UNCOV
47
                'skip_null_values' => true,
×
UNCOV
48
            ],
×
UNCOV
49
            links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
×
UNCOV
50
        ),
×
UNCOV
51
        new Operation(
×
UNCOV
52
            name: '_api_errors_jsonapi',
×
UNCOV
53
            routeName: 'api_errors',
×
UNCOV
54
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
×
UNCOV
55
            normalizationContext: [
×
UNCOV
56
                'groups' => ['jsonapi'],
×
UNCOV
57
                'skip_null_values' => true,
×
UNCOV
58
            ],
×
UNCOV
59
        ),
×
UNCOV
60
        new Operation(
×
UNCOV
61
            name: '_api_errors',
×
UNCOV
62
            routeName: 'api_errors'
×
UNCOV
63
        ),
×
UNCOV
64
    ],
×
UNCOV
65
    provider: 'api_platform.state.error_provider',
×
UNCOV
66
    graphQlOperations: []
×
UNCOV
67
)]
×
68
class Error extends \Exception implements ProblemExceptionInterface, HttpExceptionInterface
69
{
70
    public function __construct(
71
        private string $title,
72
        private string $detail,
73
        #[ApiProperty(identifier: true)] private int $status,
74
        ?array $originalTrace = null,
75
        private ?string $instance = null,
76
        private string $type = 'about:blank',
77
        private array $headers = [],
78
        ?\Throwable $previous = null,
79
    ) {
UNCOV
80
        parent::__construct($title, $status, $previous);
×
81

UNCOV
82
        if (!$originalTrace) {
×
83
            return;
×
84
        }
85

UNCOV
86
        $this->originalTrace = [];
×
UNCOV
87
        foreach ($originalTrace as $i => $t) {
×
UNCOV
88
            unset($t['args']); // we don't want arguments in our JSON traces, especially with xdebug
×
UNCOV
89
            $this->originalTrace[$i] = $t;
×
90
        }
91
    }
92

93
    #[Groups(['jsonapi'])]
94
    public function getId(): string
95
    {
UNCOV
96
        return (string) $this->status;
×
97
    }
98

99
    #[SerializedName('trace')]
100
    #[Groups(['trace'])]
101
    public ?array $originalTrace = null;
102

103
    #[Groups(['jsonld'])]
104
    public function getDescription(): ?string
105
    {
UNCOV
106
        return $this->detail;
×
107
    }
108

109
    public static function createFromException(\Exception|\Throwable $exception, int $status): self
110
    {
UNCOV
111
        $headers = ($exception instanceof SymfonyHttpExceptionInterface || $exception instanceof HttpExceptionInterface) ? $exception->getHeaders() : [];
×
112

UNCOV
113
        return new self('An error occurred', $exception->getMessage(), $status, $exception->getTrace(), type: "/errors/$status", headers: $headers, previous: $exception->getPrevious());
×
114
    }
115

116
    #[Ignore]
117
    #[ApiProperty(readable: false)]
118
    public function getHeaders(): array
119
    {
120
        return $this->headers;
×
121
    }
122

123
    #[Ignore]
124
    #[ApiProperty(readable: false)]
125
    public function getStatusCode(): int
126
    {
127
        return $this->status;
×
128
    }
129

130
    /**
131
     * @param array<string, string> $headers
132
     */
133
    public function setHeaders(array $headers): void
134
    {
135
        $this->headers = $headers;
×
136
    }
137

138
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
139
    public function getType(): string
140
    {
UNCOV
141
        return $this->type;
×
142
    }
143

144
    public function setType(string $type): void
145
    {
146
        $this->type = $type;
×
147
    }
148

149
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
150
    public function getTitle(): ?string
151
    {
UNCOV
152
        return $this->title;
×
153
    }
154

155
    public function setTitle(?string $title = null): void
156
    {
157
        $this->title = $title;
×
158
    }
159

160
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
161
    public function getStatus(): ?int
162
    {
UNCOV
163
        return $this->status;
×
164
    }
165

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

171
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
172
    public function getDetail(): ?string
173
    {
UNCOV
174
        return $this->detail;
×
175
    }
176

177
    public function setDetail(?string $detail = null): void
178
    {
179
        $this->detail = $detail;
×
180
    }
181

182
    #[Groups(['jsonld', 'jsonproblem'])]
183
    public function getInstance(): ?string
184
    {
UNCOV
185
        return $this->instance;
×
186
    }
187

188
    public function setInstance(?string $instance = null): void
189
    {
190
        $this->instance = $instance;
×
191
    }
192
}
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