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

api-platform / core / 13175309672

06 Feb 2025 09:04AM UTC coverage: 7.663% (-0.2%) from 7.841%
13175309672

push

github

web-flow
fix: ensure template files have a tpl file extension (#6826) (#6829)

2 of 5 new or added lines in 4 files covered. (40.0%)

3676 existing lines in 122 files now uncovered.

13073 of 170593 relevant lines covered (7.66%)

27.3 hits per line

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

62.5
/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(
28
    openapi: false,
29
    uriVariables: ['status'],
30
    uriTemplate: '/errors/{status}',
31
    operations: [
32
        new Operation(
33
            name: '_api_errors_problem',
34
            routeName: 'api_errors',
35
            outputFormats: ['json' => ['application/problem+json']],
36
            normalizationContext: [
37
                'groups' => ['jsonproblem'],
38
                'skip_null_values' => true,
39
            ],
40
        ),
41
        new Operation(
42
            name: '_api_errors_hydra',
43
            routeName: 'api_errors',
44
            outputFormats: ['jsonld' => ['application/problem+json']],
45
            normalizationContext: [
46
                'groups' => ['jsonld'],
47
                'skip_null_values' => true,
48
            ],
49
            links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
50
        ),
51
        new Operation(
52
            name: '_api_errors_jsonapi',
53
            routeName: 'api_errors',
54
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
55
            normalizationContext: [
56
                'groups' => ['jsonapi'],
57
                'skip_null_values' => true,
58
            ],
59
        ),
60
        new Operation(
61
            name: '_api_errors',
62
            routeName: 'api_errors'
63
        ),
64
    ],
65
    provider: 'api_platform.state.error_provider',
66
    graphQlOperations: []
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
    ) {
80
        parent::__construct($title, $status, $previous);
209✔
81

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

188
    public function setInstance(?string $instance = null): void
189
    {
UNCOV
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