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

api-platform / core / 6867212600

14 Nov 2023 05:15PM UTC coverage: 37.474% (-0.01%) from 37.484%
6867212600

push

github

soyuka
chore: phpstan and deprecations

3 of 3 new or added lines in 2 files covered. (100.0%)

90 existing lines in 7 files now uncovered.

10323 of 27547 relevant lines covered (37.47%)

13.81 hits per line

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

60.0
/src/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\ApiResource;
15

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

28
#[ErrorResource(
29
    types: ['hydra:Error'],
30
    openapi: false,
31
    uriTemplate: '/errors/{status}',
32
    operations: [
33
        new Operation(
34
            name: '_api_errors_problem',
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
            outputFormats: ['jsonld' => ['application/problem+json']],
44
            normalizationContext: [
45
                'groups' => ['jsonld'],
46
                'skip_null_values' => true,
47
            ],
48
            links: [new Link(rel: ContextBuilderInterface::JSONLD_NS.'error', href: 'http://www.w3.org/ns/hydra/error')],
49
        ),
50
        new Operation(
51
            name: '_api_errors_jsonapi',
52
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
53
            normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true],
54
        ),
55
    ],
56
    graphQlOperations: []
57
)]
58
class Error extends \Exception implements ProblemExceptionInterface, HttpExceptionInterface
59
{
60
    public function __construct(
61
        private string $title,
62
        private string $detail,
63
        #[ApiProperty(identifier: true)] private int $status,
64
        array $originalTrace = null,
65
        private ?string $instance = null,
66
        private string $type = 'about:blank',
67
        private array $headers = [],
68
        \Throwable $previous = null
69
    ) {
70
        parent::__construct($title, $status, $previous);
12✔
71

72
        if (!$originalTrace) {
12✔
UNCOV
73
            return;
×
74
        }
75

76
        $this->originalTrace = [];
12✔
77
        foreach ($originalTrace as $i => $t) {
12✔
78
            unset($t['args']); // we don't want arguments in our JSON traces, especially with xdebug
12✔
79
            $this->originalTrace[$i] = $t;
12✔
80
        }
81
    }
82

83
    #[SerializedName('trace')]
84
    #[Groups(['trace'])]
85
    public ?array $originalTrace = null;
86

87
    #[SerializedName('hydra:title')]
88
    #[Groups(['jsonld', 'legacy_jsonld'])]
89
    public function getHydraTitle(): ?string
90
    {
UNCOV
91
        return $this->title;
×
92
    }
93

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

101
    #[SerializedName('description')]
102
    #[Groups(['jsonapi', 'legacy_jsonapi'])]
103
    public function getDescription(): ?string
104
    {
UNCOV
105
        return $this->detail;
×
106
    }
107

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

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

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

121
    #[Ignore]
122
    public function getStatusCode(): int
123
    {
UNCOV
124
        return $this->status;
×
125
    }
126

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

135
    #[Groups(['jsonld', 'jsonproblem'])]
136
    public function getType(): string
137
    {
138
        return $this->type;
2✔
139
    }
140

141
    public function setType(string $type): void
142
    {
UNCOV
143
        $this->type = $type;
×
144
    }
145

146
    #[Groups(['jsonld', 'legacy_jsonproblem', 'jsonproblem', 'jsonapi', 'legacy_jsonapi'])]
147
    public function getTitle(): ?string
148
    {
149
        return $this->title;
2✔
150
    }
151

152
    public function setTitle(string $title = null): void
153
    {
UNCOV
154
        $this->title = $title;
×
155
    }
156

157
    #[Groups(['jsonld', 'jsonproblem', 'legacy_jsonproblem'])]
158
    public function getStatus(): ?int
159
    {
160
        return $this->status;
2✔
161
    }
162

163
    public function setStatus(int $status): void
164
    {
165
        $this->status = $status;
2✔
166
    }
167

168
    #[Groups(['jsonld', 'jsonproblem', 'legacy_jsonproblem'])]
169
    public function getDetail(): ?string
170
    {
171
        return $this->detail;
4✔
172
    }
173

174
    public function setDetail(string $detail = null): void
175
    {
176
        $this->detail = $detail;
2✔
177
    }
178

179
    #[Groups(['jsonld', 'jsonproblem', 'legacy_jsonproblem'])]
180
    public function getInstance(): ?string
181
    {
182
        return $this->instance;
2✔
183
    }
184

185
    public function setInstance(string $instance = null): void
186
    {
UNCOV
187
        $this->instance = $instance;
×
188
    }
189
}
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