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

api-platform / core / 13203378522

07 Feb 2025 03:56PM UTC coverage: 8.501% (+0.7%) from 7.837%
13203378522

push

github

soyuka
Merge 4.1

111 of 490 new or added lines in 51 files covered. (22.65%)

5590 existing lines in 163 files now uncovered.

13345 of 156987 relevant lines covered (8.5%)

22.88 hits per line

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

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

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

30
#[ErrorResource(
NEW
31
    uriTemplate: '/errors/{status}{._format}',
×
32
    openapi: false,
×
NEW
33
    uriVariables: ['status'],
×
34
    operations: [
×
35
        new Operation(
×
NEW
36
            errors: [],
×
37
            name: '_api_errors_problem',
×
NEW
38
            routeName: '_api_errors',
×
NEW
39
            outputFormats: ['json' => ['application/problem+json', 'application/json']],
×
NEW
40
            hideHydraOperation: true,
×
41
            normalizationContext: [
×
NEW
42
                SchemaFactory::OPENAPI_DEFINITION_NAME => '',
×
43
                'groups' => ['jsonproblem'],
×
44
                'skip_null_values' => true,
×
NEW
45
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
46
            ],
×
47
        ),
×
48
        new Operation(
×
NEW
49
            errors: [],
×
50
            name: '_api_errors_hydra',
×
NEW
51
            routeName: '_api_errors',
×
NEW
52
            outputFormats: ['jsonld' => ['application/problem+json', 'application/ld+json']],
×
53
            normalizationContext: [
×
NEW
54
                SchemaFactory::OPENAPI_DEFINITION_NAME => '',
×
55
                'groups' => ['jsonld'],
×
56
                'skip_null_values' => true,
×
NEW
57
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
58
            ],
×
NEW
59
            links: [new Link(rel: 'http://www.w3.org/ns/json-ld#error', href: 'http://www.w3.org/ns/hydra/error')],
×
60
        ),
×
61
        new Operation(
×
NEW
62
            errors: [],
×
63
            name: '_api_errors_jsonapi',
×
NEW
64
            routeName: '_api_errors',
×
NEW
65
            hideHydraOperation: true,
×
66
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
×
NEW
67
            normalizationContext: [
×
NEW
68
                SchemaFactory::OPENAPI_DEFINITION_NAME => '',
×
NEW
69
                'disable_json_schema_serializer_groups' => false,
×
NEW
70
                'groups' => ['jsonapi'],
×
NEW
71
                'skip_null_values' => true,
×
NEW
72
                'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
×
NEW
73
            ],
×
NEW
74
        ),
×
NEW
75
        new Operation(
×
NEW
76
            name: '_api_errors',
×
NEW
77
            hideHydraOperation: true,
×
NEW
78
            extraProperties: ['_api_disable_swagger_provider' => true],
×
NEW
79
            outputFormats: ['html' => ['text/html'], 'jsonapi' => ['application/vnd.api+json'], 'jsonld' => ['application/ld+json'], 'json' => ['application/problem+json', 'application/json']],
×
80
        ),
×
81
    ],
×
NEW
82
    outputFormats: ['jsonapi' => ['application/vnd.api+json'], 'jsonld' => ['application/ld+json'], 'json' => ['application/problem+json', 'application/json']],
×
NEW
83
    provider: ErrorProvider::class,
×
NEW
84
    graphQlOperations: [],
×
NEW
85
    description: 'A representation of common errors.',
×
UNCOV
86
)]
×
87
#[ApiProperty(property: 'previous', hydra: false, readable: false)]
88
#[ApiProperty(property: 'traceAsString', hydra: false, readable: false)]
89
#[ApiProperty(property: 'string', hydra: false, readable: false)]
90
class Error extends \Exception implements ProblemExceptionInterface, HttpExceptionInterface, ErrorResourceInterface
91
{
92
    /**
93
     * @var array<int, mixed>
94
     */
95
    private array $originalTrace;
96

97
    /**
98
     * @param array<string, string> $headers
99
     * @param array<int, mixed>     $originalTrace
100
     */
101
    public function __construct(
102
        private readonly string $title,
103
        private readonly string $detail,
104
        #[ApiProperty(identifier: true)] private int $status,
105
        array $originalTrace = [],
106
        private readonly ?string $instance = null,
107
        private string $type = 'about:blank',
108
        private array $headers = [],
109
    ) {
110
        parent::__construct();
×
111

112
        $this->originalTrace = [];
×
113
        foreach ($originalTrace as $i => $t) {
×
114
            unset($t['args']); // we don't want arguments in our JSON traces, especially with xdebug
×
115
            $this->originalTrace[$i] = $t;
×
116
        }
117
    }
118

119
    /**
120
     * @return array<int, mixed>
121
     */
122
    #[SerializedName('trace')]
123
    #[Groups(['trace'])]
124
    public function getOriginalTrace(): array
125
    {
126
        return $this->originalTrace;
×
127
    }
128

129
    #[SerializedName('description')]
130
    public function getDescription(): string
131
    {
132
        return $this->detail;
×
133
    }
134

135
    public static function createFromException(\Exception|\Throwable $exception, int $status): self
136
    {
137
        $headers = ($exception instanceof SymfonyHttpExceptionInterface || $exception instanceof HttpExceptionInterface) ? $exception->getHeaders() : [];
×
138

139
        return new self('An error occurred', $exception->getMessage(), $status, $exception->getTrace(), type: '/errors/'.$status, headers: $headers);
×
140
    }
141

142
    /**
143
     * @return array<string, string>
144
     */
145
    #[Ignore]
146
    public function getHeaders(): array
147
    {
148
        return $this->headers;
×
149
    }
150

151
    #[Ignore]
152
    public function getStatusCode(): int
153
    {
154
        return $this->status;
×
155
    }
156

157
    #[Groups(['jsonapi'])]
158
    public function getId(): string
159
    {
UNCOV
160
        return (string) $this->status;
×
161
    }
162

163
    /**
164
     * @param array<string, string> $headers
165
     */
166
    public function setHeaders(array $headers): void
167
    {
168
        $this->headers = $headers;
×
169
    }
170

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

177
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
178
    public function getTitle(): ?string
179
    {
UNCOV
180
        return $this->title;
×
181
    }
182

183
    public function setType(string $type): void
184
    {
185
        $this->type = $type;
×
186
    }
187

188
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
189
    public function getStatus(): ?int
190
    {
UNCOV
191
        return $this->status;
×
192
    }
193

194
    public function setStatus(int $status): void
195
    {
196
        $this->status = $status;
×
197
    }
198

199
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
200
    public function getDetail(): ?string
201
    {
202
        return $this->detail;
×
203
    }
204

205
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
206
    public function getInstance(): ?string
207
    {
UNCOV
208
        return $this->instance;
×
209
    }
210
}
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

© 2026 Coveralls, Inc