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

api-platform / core / 11271860720

10 Oct 2024 10:04AM UTC coverage: 7.84% (+0.007%) from 7.833%
11271860720

push

github

soyuka
Merge 3.4

0 of 34 new or added lines in 5 files covered. (0.0%)

11160 existing lines in 381 files now uncovered.

12955 of 165237 relevant lines covered (7.84%)

27.06 hits per line

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

60.87
/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);
203✔
81

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

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

93
    #[SerializedName('trace')]
94
    #[Groups(['trace'])]
95
    public ?array $originalTrace = null;
96

97
    #[Groups(['jsonld'])]
98
    public function getDescription(): ?string
99
    {
UNCOV
100
        return $this->detail;
153✔
101
    }
102

103
    public static function createFromException(\Exception|\Throwable $exception, int $status): self
104
    {
UNCOV
105
        $headers = ($exception instanceof SymfonyHttpExceptionInterface || $exception instanceof HttpExceptionInterface) ? $exception->getHeaders() : [];
203✔
106

UNCOV
107
        return new self('An error occurred', $exception->getMessage(), $status, $exception->getTrace(), type: "/errors/$status", headers: $headers, previous: $exception->getPrevious());
203✔
108
    }
109

110
    #[Ignore]
111
    #[ApiProperty(readable: false)]
112
    public function getHeaders(): array
113
    {
114
        return $this->headers;
×
115
    }
116

117
    #[Ignore]
118
    #[ApiProperty(readable: false)]
119
    public function getStatusCode(): int
120
    {
121
        return $this->status;
×
122
    }
123

124
    /**
125
     * @param array<string, string> $headers
126
     */
127
    public function setHeaders(array $headers): void
128
    {
129
        $this->headers = $headers;
×
130
    }
131

132
    #[Groups(['jsonld', 'jsonproblem'])]
133
    public function getType(): string
134
    {
UNCOV
135
        return $this->type;
192✔
136
    }
137

138
    public function setType(string $type): void
139
    {
140
        $this->type = $type;
×
141
    }
142

143
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
144
    public function getTitle(): ?string
145
    {
UNCOV
146
        return $this->title;
203✔
147
    }
148

149
    public function setTitle(?string $title = null): void
150
    {
151
        $this->title = $title;
×
152
    }
153

154
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
155
    public function getStatus(): ?int
156
    {
UNCOV
157
        return $this->status;
203✔
158
    }
159

160
    public function setStatus(int $status): void
161
    {
162
        $this->status = $status;
×
163
    }
164

165
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
166
    public function getDetail(): ?string
167
    {
UNCOV
168
        return $this->detail;
203✔
169
    }
170

171
    public function setDetail(?string $detail = null): void
172
    {
173
        $this->detail = $detail;
×
174
    }
175

176
    #[Groups(['jsonld', 'jsonproblem'])]
177
    public function getInstance(): ?string
178
    {
UNCOV
179
        return $this->instance;
192✔
180
    }
181

182
    public function setInstance(?string $instance = null): void
183
    {
184
        $this->instance = $instance;
×
185
    }
186
}
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