Skip to content

Commit 1adeb5c

Browse files
authored
Update Error.php
1 parent deacaad commit 1adeb5c

File tree

1 file changed

+1
-174
lines changed

1 file changed

+1
-174
lines changed

ApiResource/Error.php

Lines changed: 1 addition & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,174 +1 @@
1-
<?php
2-
3-
/*
4-
* This file is part of the API Platform project.
5-
*
6-
* (c) Kévin Dunglas <[email protected]>
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\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-
operations: [
32-
new Operation(
33-
name: '_api_errors_problem',
34-
outputFormats: ['json' => ['application/problem+json']],
35-
normalizationContext: [
36-
'groups' => ['jsonproblem'],
37-
'skip_null_values' => true,
38-
],
39-
uriTemplate: '/errors/{status}'
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-
uriTemplate: '/errors/{status}.jsonld'
50-
),
51-
new Operation(
52-
name: '_api_errors_jsonapi',
53-
outputFormats: ['jsonapi' => ['application/vnd.api+json']],
54-
normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true],
55-
uriTemplate: '/errros/{status}.jsonapi'
56-
),
57-
],
58-
graphQlOperations: []
59-
)]
60-
class Error extends \Exception implements ProblemExceptionInterface, HttpExceptionInterface
61-
{
62-
/**
63-
* @var array<int, mixed>
64-
*/
65-
private array $originalTrace;
66-
67-
/**
68-
* @param array<string, string> $headers
69-
* @param array<int, mixed> $originalTrace
70-
*/
71-
public function __construct(
72-
private readonly string $title,
73-
private readonly string $detail,
74-
#[ApiProperty(identifier: true)] private int $status,
75-
array $originalTrace,
76-
private readonly ?string $instance = null,
77-
private string $type = 'about:blank',
78-
private array $headers = [],
79-
) {
80-
parent::__construct();
81-
82-
$this->originalTrace = [];
83-
foreach ($originalTrace as $i => $t) {
84-
unset($t['args']); // we don't want arguments in our JSON traces, especially with xdebug
85-
$this->originalTrace[$i] = $t;
86-
}
87-
}
88-
89-
/**
90-
* @return array<int, mixed>
91-
*/
92-
#[SerializedName('trace')]
93-
#[Groups(['trace'])]
94-
public function getOriginalTrace(): array
95-
{
96-
return $this->originalTrace;
97-
}
98-
99-
#[SerializedName('description')]
100-
public function getDescription(): string
101-
{
102-
return $this->detail;
103-
}
104-
105-
public static function createFromException(\Exception|\Throwable $exception, int $status): self
106-
{
107-
$headers = ($exception instanceof SymfonyHttpExceptionInterface || $exception instanceof HttpExceptionInterface) ? $exception->getHeaders() : [];
108-
109-
return new self('An error occurred', $exception->getMessage(), $status, $exception->getTrace(), type: '/errors/'.$status, headers: $headers);
110-
}
111-
112-
/**
113-
* @return array<string, string>
114-
*/
115-
#[Ignore]
116-
public function getHeaders(): array
117-
{
118-
return $this->headers;
119-
}
120-
121-
#[Ignore]
122-
public function getStatusCode(): int
123-
{
124-
return $this->status;
125-
}
126-
127-
/**
128-
* @param array<string, string> $headers
129-
*/
130-
public function setHeaders(array $headers): void
131-
{
132-
$this->headers = $headers;
133-
}
134-
135-
#[Groups(['jsonld', 'jsonproblem'])]
136-
public function getType(): string
137-
{
138-
return $this->type;
139-
}
140-
141-
#[Groups(['jsonld', 'jsonproblem', 'jsonapi'])]
142-
public function getTitle(): ?string
143-
{
144-
return $this->title;
145-
}
146-
147-
public function setType(string $type): void
148-
{
149-
$this->type = $type;
150-
}
151-
152-
#[Groups(['jsonld', 'jsonproblem'])]
153-
public function getStatus(): ?int
154-
{
155-
return $this->status;
156-
}
157-
158-
public function setStatus(int $status): void
159-
{
160-
$this->status = $status;
161-
}
162-
163-
#[Groups(['jsonld', 'jsonproblem'])]
164-
public function getDetail(): ?string
165-
{
166-
return $this->detail;
167-
}
168-
169-
#[Groups(['jsonld', 'jsonproblem'])]
170-
public function getInstance(): ?string
171-
{
172-
return $this->instance;
173-
}
174-
}
1+
test

0 commit comments

Comments
 (0)