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

api-platform / core / 6665644319

27 Oct 2023 09:57AM UTC coverage: 37.409% (+0.1%) from 37.305%
6665644319

push

github

soyuka
Merge 3.2

125 of 125 new or added lines in 32 files covered. (100.0%)

10319 of 27584 relevant lines covered (37.41%)

20.57 hits per line

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

68.18
/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 readonly string $title,
62
        private readonly 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
    ) {
69
        parent::__construct();
12✔
70

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

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

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

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

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

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

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

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

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

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

126
    public function setHeaders(array $headers): void
127
    {
128
        $this->headers = $headers;
×
129
    }
130

131
    #[Groups(['jsonld', 'jsonproblem'])]
132
    public function getType(): string
133
    {
134
        return $this->type;
3✔
135
    }
136

137
    #[Groups(['jsonld', 'legacy_jsonproblem', 'jsonproblem', 'jsonapi', 'legacy_jsonapi'])]
138
    public function getTitle(): ?string
139
    {
140
        return $this->title;
3✔
141
    }
142

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

148
    #[Groups(['jsonld', 'jsonproblem', 'legacy_jsonproblem'])]
149
    public function getStatus(): ?int
150
    {
151
        return $this->status;
3✔
152
    }
153

154
    public function setStatus(int $status): void
155
    {
156
        $this->status = $status;
3✔
157
    }
158

159
    #[Groups(['jsonld', 'jsonproblem', 'legacy_jsonproblem'])]
160
    public function getDetail(): ?string
161
    {
162
        return $this->detail;
3✔
163
    }
164

165
    #[Groups(['jsonld', 'jsonproblem', 'legacy_jsonproblem'])]
166
    public function getInstance(): ?string
167
    {
168
        return $this->instance;
3✔
169
    }
170
}
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