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

api-platform / core / 6978770879

24 Nov 2023 09:02AM UTC coverage: 37.284% (-0.1%) from 37.409%
6978770879

push

github

soyuka
Merge 3.2

79 of 149 new or added lines in 21 files covered. (53.02%)

16 existing lines in 8 files now uncovered.

10287 of 27591 relevant lines covered (37.28%)

20.53 hits per line

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

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

80
        if (!$originalTrace) {
6✔
NEW
81
            return;
×
82
        }
83

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

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

95
    #[SerializedName('hydra:title')]
96
    #[Groups(['jsonld'])]
97
    public function getHydraTitle(): ?string
98
    {
NEW
99
        return $this->title;
×
100
    }
101

102
    #[SerializedName('hydra:description')]
103
    #[Groups(['jsonld'])]
104
    public function getHydraDescription(): ?string
105
    {
NEW
106
        return $this->detail;
×
107
    }
108

109
    #[SerializedName('description')]
110
    public function getDescription(): ?string
111
    {
NEW
112
        return $this->detail;
×
113
    }
114

115
    public static function createFromException(\Exception|\Throwable $exception, int $status): self
116
    {
117
        $headers = ($exception instanceof SymfonyHttpExceptionInterface || $exception instanceof HttpExceptionInterface) ? $exception->getHeaders() : [];
6✔
118

119
        return new self('An error occurred', $exception->getMessage(), $status, $exception->getTrace(), type: "/errors/$status", headers: $headers, previous: $exception->getPrevious());
6✔
120
    }
121

122
    #[Ignore]
123
    public function getHeaders(): array
124
    {
NEW
125
        return $this->headers;
×
126
    }
127

128
    #[Ignore]
129
    public function getStatusCode(): int
130
    {
NEW
131
        return $this->status;
×
132
    }
133

134
    /**
135
     * @param array<string, string> $headers
136
     */
137
    public function setHeaders(array $headers): void
138
    {
NEW
139
        $this->headers = $headers;
×
140
    }
141

142
    #[Groups(['jsonld', 'jsonproblem'])]
143
    public function getType(): string
144
    {
NEW
145
        return $this->type;
×
146
    }
147

148
    public function setType(string $type): void
149
    {
NEW
150
        $this->type = $type;
×
151
    }
152

153
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
154
    public function getTitle(): ?string
155
    {
NEW
156
        return $this->title;
×
157
    }
158

159
    public function setTitle(string $title = null): void
160
    {
NEW
161
        $this->title = $title;
×
162
    }
163

164
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
165
    public function getStatus(): ?int
166
    {
167
        return $this->status;
3✔
168
    }
169

170
    public function setStatus(int $status): void
171
    {
NEW
172
        $this->status = $status;
×
173
    }
174

175
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
176
    public function getDetail(): ?string
177
    {
NEW
178
        return $this->detail;
×
179
    }
180

181
    public function setDetail(string $detail = null): void
182
    {
NEW
183
        $this->detail = $detail;
×
184
    }
185

186
    #[Groups(['jsonld', 'jsonproblem'])]
187
    public function getInstance(): ?string
188
    {
NEW
189
        return $this->instance;
×
190
    }
191

192
    public function setInstance(string $instance = null): void
193
    {
NEW
194
        $this->instance = $instance;
×
195
    }
196
}
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