• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

api-platform / core / 10903050455

17 Sep 2024 12:29PM UTC coverage: 7.684% (+0.7%) from 6.96%
10903050455

push

github

web-flow
fix: swagger ui with route identifier (#6616)

2 of 6 new or added lines in 6 files covered. (33.33%)

9000 existing lines in 286 files now uncovered.

12668 of 164863 relevant lines covered (7.68%)

22.93 hits per line

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

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

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

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

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

98
    #[SerializedName('hydra:title')]
99
    #[Groups(['jsonld'])]
100
    public function getHydraTitle(): ?string
101
    {
UNCOV
102
        return $this->title;
138✔
103
    }
104

105
    #[SerializedName('hydra:description')]
106
    #[Groups(['jsonld'])]
107
    public function getHydraDescription(): ?string
108
    {
UNCOV
109
        return $this->detail;
138✔
110
    }
111

112
    #[SerializedName('description')]
113
    public function getDescription(): ?string
114
    {
115
        return $this->detail;
×
116
    }
117

118
    public static function createFromException(\Exception|\Throwable $exception, int $status): self
119
    {
UNCOV
120
        $headers = ($exception instanceof SymfonyHttpExceptionInterface || $exception instanceof HttpExceptionInterface) ? $exception->getHeaders() : [];
179✔
121

UNCOV
122
        return new self('An error occurred', $exception->getMessage(), $status, $exception->getTrace(), type: "/errors/$status", headers: $headers, previous: $exception->getPrevious());
179✔
123
    }
124

125
    #[Ignore]
126
    #[ApiProperty(readable: false)]
127
    public function getHeaders(): array
128
    {
129
        return $this->headers;
×
130
    }
131

132
    #[Ignore]
133
    #[ApiProperty(readable: false)]
134
    public function getStatusCode(): int
135
    {
136
        return $this->status;
×
137
    }
138

139
    /**
140
     * @param array<string, string> $headers
141
     */
142
    public function setHeaders(array $headers): void
143
    {
144
        $this->headers = $headers;
×
145
    }
146

147
    #[Groups(['jsonld', 'jsonproblem'])]
148
    public function getType(): string
149
    {
UNCOV
150
        return $this->type;
171✔
151
    }
152

153
    public function setType(string $type): void
154
    {
155
        $this->type = $type;
×
156
    }
157

158
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
159
    public function getTitle(): ?string
160
    {
UNCOV
161
        return $this->title;
179✔
162
    }
163

164
    public function setTitle(?string $title = null): void
165
    {
166
        $this->title = $title;
×
167
    }
168

169
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
170
    public function getStatus(): ?int
171
    {
UNCOV
172
        return $this->status;
179✔
173
    }
174

175
    public function setStatus(int $status): void
176
    {
177
        $this->status = $status;
×
178
    }
179

180
    #[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
181
    public function getDetail(): ?string
182
    {
UNCOV
183
        return $this->detail;
179✔
184
    }
185

186
    public function setDetail(?string $detail = null): void
187
    {
188
        $this->detail = $detail;
×
189
    }
190

191
    #[Groups(['jsonld', 'jsonproblem'])]
192
    public function getInstance(): ?string
193
    {
UNCOV
194
        return $this->instance;
171✔
195
    }
196

197
    public function setInstance(?string $instance = null): void
198
    {
199
        $this->instance = $instance;
×
200
    }
201
}
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