|
| 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\ApiResource; |
| 15 | + |
| 16 | +use ApiPlatform\Exception\ProblemExceptionInterface; |
| 17 | +use ApiPlatform\Metadata\ApiProperty; |
| 18 | +use ApiPlatform\Metadata\ErrorResource; |
| 19 | +use ApiPlatform\Metadata\Exception\HttpExceptionInterface; |
| 20 | +use ApiPlatform\Metadata\Get; |
| 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 | + |
| 26 | +#[ErrorResource( |
| 27 | + uriTemplate: '/errors/{status}', |
| 28 | + provider: 'api_platform.state_provider.default_error', |
| 29 | + types: ['hydra:Error'], |
| 30 | + operations: [ |
| 31 | + new Get(name: '_api_errors_hydra', outputFormats: ['jsonld' => ['application/ld+json']], normalizationContext: ['groups' => ['jsonld'], 'skip_null_values' => true]), |
| 32 | + new Get(name: '_api_errors_problem', outputFormats: ['json' => ['application/problem+json']], normalizationContext: ['groups' => ['jsonproblem'], 'skip_null_values' => true]), |
| 33 | + new Get(name: '_api_errors_jsonapi', outputFormats: ['jsonapi' => ['application/vnd.api+json']], normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true], provider: 'api_platform.json_api.state_provider.default_error'), |
| 34 | + ] |
| 35 | +)] |
| 36 | +class Error extends \Exception implements ProblemExceptionInterface, HttpExceptionInterface |
| 37 | +{ |
| 38 | + public function __construct( |
| 39 | + private readonly string $title, |
| 40 | + private readonly string $detail, |
| 41 | + #[ApiProperty(identifier: true)] private readonly int $status, |
| 42 | + #[Groups(['trace'])] |
| 43 | + public readonly array $trace, |
| 44 | + private ?string $instance = null, |
| 45 | + private string $type = 'about:blank', |
| 46 | + private array $headers = [] |
| 47 | + ) { |
| 48 | + parent::__construct(); |
| 49 | + } |
| 50 | + |
| 51 | + #[SerializedName('hydra:title')] |
| 52 | + #[Groups(['jsonld', 'legacy_jsonld'])] |
| 53 | + public function getHydraTitle(): string |
| 54 | + { |
| 55 | + return $this->title; |
| 56 | + } |
| 57 | + |
| 58 | + #[SerializedName('hydra:description')] |
| 59 | + #[Groups(['jsonld', 'legacy_jsonld'])] |
| 60 | + public function getHydraDescription(): string |
| 61 | + { |
| 62 | + return $this->detail; |
| 63 | + } |
| 64 | + |
| 65 | + #[SerializedName('description')] |
| 66 | + #[Groups(['jsonapi', 'legacy_jsonapi'])] |
| 67 | + public function getDescription(): string |
| 68 | + { |
| 69 | + return $this->detail; |
| 70 | + } |
| 71 | + |
| 72 | + public static function createFromException(\Exception|\Throwable $exception, int $status): self |
| 73 | + { |
| 74 | + $headers = ($exception instanceof SymfonyHttpExceptionInterface || $exception instanceof HttpExceptionInterface) ? $exception->getHeaders() : []; |
| 75 | + |
| 76 | + return new self('An error occurred', $exception->getMessage(), $status, $exception->getTrace(), type: '/errors/'.$status, headers: $headers); |
| 77 | + } |
| 78 | + |
| 79 | + #[Ignore] |
| 80 | + public function getHeaders(): array |
| 81 | + { |
| 82 | + return $this->headers; |
| 83 | + } |
| 84 | + |
| 85 | + #[Ignore] |
| 86 | + public function getStatusCode(): int |
| 87 | + { |
| 88 | + return $this->status; |
| 89 | + } |
| 90 | + |
| 91 | + public function setHeaders(array $headers): void |
| 92 | + { |
| 93 | + $this->headers = $headers; |
| 94 | + } |
| 95 | + |
| 96 | + #[Groups(['jsonld', 'jsonproblem'])] |
| 97 | + public function getType(): string |
| 98 | + { |
| 99 | + return $this->type; |
| 100 | + } |
| 101 | + |
| 102 | + #[Groups(['jsonld', 'legacy_jsonproblem', 'jsonproblem', 'jsonapi', 'legacy_jsonapi'])] |
| 103 | + public function getTitle(): ?string |
| 104 | + { |
| 105 | + return $this->title; |
| 106 | + } |
| 107 | + |
| 108 | + public function setType(string $type): void |
| 109 | + { |
| 110 | + $this->type = $type; |
| 111 | + } |
| 112 | + |
| 113 | + #[Groups(['jsonld', 'jsonproblem', 'legacy_jsonproblem'])] |
| 114 | + public function getStatus(): ?int |
| 115 | + { |
| 116 | + return $this->status; |
| 117 | + } |
| 118 | + |
| 119 | + #[Groups(['jsonld', 'jsonproblem', 'legacy_jsonproblem'])] |
| 120 | + public function getDetail(): ?string |
| 121 | + { |
| 122 | + return $this->detail; |
| 123 | + } |
| 124 | + |
| 125 | + #[Groups(['jsonld', 'jsonproblem', 'legacy_jsonproblem'])] |
| 126 | + public function getInstance(): ?string |
| 127 | + { |
| 128 | + return $this->instance; |
| 129 | + } |
| 130 | +} |
0 commit comments