vendor/api-platform/core/src/State/ApiResource/Error.php line 159

  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\State\ApiResource;
  12. use ApiPlatform\JsonLd\ContextBuilderInterface;
  13. use ApiPlatform\Metadata\ApiProperty;
  14. use ApiPlatform\Metadata\Error as Operation;
  15. use ApiPlatform\Metadata\ErrorResource;
  16. use ApiPlatform\Metadata\Exception\HttpExceptionInterface;
  17. use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
  18. use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface as SymfonyHttpExceptionInterface;
  19. use Symfony\Component\Serializer\Annotation\Groups;
  20. use Symfony\Component\Serializer\Annotation\Ignore;
  21. use Symfony\Component\Serializer\Annotation\SerializedName;
  22. use Symfony\Component\WebLink\Link;
  23. #[ErrorResource(
  24.     types: ['hydra:Error'],
  25.     openapifalse,
  26.     uriVariables: ['status'],
  27.     uriTemplate'/errors/{status}',
  28.     operations: [
  29.         new Operation(
  30.             name'_api_errors_problem',
  31.             outputFormats: ['json' => ['application/problem+json']],
  32.             normalizationContext: [
  33.                 'groups' => ['jsonproblem'],
  34.                 'skip_null_values' => true,
  35.                 'rfc_7807_compliant_errors' => true,
  36.             ],
  37.         ),
  38.         new Operation(
  39.             name'_api_errors_hydra',
  40.             outputFormats: ['jsonld' => ['application/problem+json']],
  41.             normalizationContext: [
  42.                 'groups' => ['jsonld'],
  43.                 'skip_null_values' => true,
  44.                 'rfc_7807_compliant_errors' => true,
  45.             ],
  46.             links: [new Link(relContextBuilderInterface::JSONLD_NS.'error'href'http://www.w3.org/ns/hydra/error')],
  47.         ),
  48.         new Operation(
  49.             name'_api_errors_jsonapi',
  50.             outputFormats: ['jsonapi' => ['application/vnd.api+json']],
  51.             normalizationContext: [
  52.                 'groups' => ['jsonapi'],
  53.                 'skip_null_values' => true,
  54.                 'rfc_7807_compliant_errors' => true,
  55.             ],
  56.         ),
  57.     ],
  58.     provider'api_platform.state.error_provider',
  59.     graphQlOperations: []
  60. )]
  61. class Error extends \Exception implements ProblemExceptionInterfaceHttpExceptionInterface
  62. {
  63.     public function __construct(
  64.         private string $title,
  65.         private string $detail,
  66.         #[ApiProperty(identifiertrue)] private int $status,
  67.         array $originalTrace null,
  68.         private ?string $instance null,
  69.         private string $type 'about:blank',
  70.         private array $headers = [],
  71.         \Throwable $previous null
  72.     ) {
  73.         parent::__construct($title$status$previous);
  74.         if (!$originalTrace) {
  75.             return;
  76.         }
  77.         $this->originalTrace = [];
  78.         foreach ($originalTrace as $i => $t) {
  79.             unset($t['args']); // we don't want arguments in our JSON traces, especially with xdebug
  80.             $this->originalTrace[$i] = $t;
  81.         }
  82.     }
  83.     #[SerializedName('trace')]
  84.     #[Groups(['trace'])]
  85.     public ?array $originalTrace null;
  86.     #[SerializedName('hydra:title')]
  87.     #[Groups(['jsonld'])]
  88.     public function getHydraTitle(): ?string
  89.     {
  90.         return $this->title;
  91.     }
  92.     #[SerializedName('hydra:description')]
  93.     #[Groups(['jsonld'])]
  94.     public function getHydraDescription(): ?string
  95.     {
  96.         return $this->detail;
  97.     }
  98.     #[SerializedName('description')]
  99.     public function getDescription(): ?string
  100.     {
  101.         return $this->detail;
  102.     }
  103.     public static function createFromException(\Exception|\Throwable $exceptionint $status): self
  104.     {
  105.         $headers = ($exception instanceof SymfonyHttpExceptionInterface || $exception instanceof HttpExceptionInterface) ? $exception->getHeaders() : [];
  106.         return new self('An error occurred'$exception->getMessage(), $status$exception->getTrace(), type"/errors/$status"headers$headersprevious$exception->getPrevious());
  107.     }
  108.     #[Ignore]
  109.     public function getHeaders(): array
  110.     {
  111.         return $this->headers;
  112.     }
  113.     #[Ignore]
  114.     public function getStatusCode(): int
  115.     {
  116.         return $this->status;
  117.     }
  118.     /**
  119.      * @param array<string, string> $headers
  120.      */
  121.     public function setHeaders(array $headers): void
  122.     {
  123.         $this->headers $headers;
  124.     }
  125.     #[Groups(['jsonld''jsonproblem'])]
  126.     public function getType(): string
  127.     {
  128.         return $this->type;
  129.     }
  130.     public function setType(string $type): void
  131.     {
  132.         $this->type $type;
  133.     }
  134.     #[Groups(['jsonld''jsonproblem''jsonapi'])]
  135.     public function getTitle(): ?string
  136.     {
  137.         return $this->title;
  138.     }
  139.     public function setTitle(string $title null): void
  140.     {
  141.         $this->title $title;
  142.     }
  143.     #[Groups(['jsonld''jsonproblem''jsonapi'])]
  144.     public function getStatus(): ?int
  145.     {
  146.         return $this->status;
  147.     }
  148.     public function setStatus(int $status): void
  149.     {
  150.         $this->status $status;
  151.     }
  152.     #[Groups(['jsonld''jsonproblem''jsonapi'])]
  153.     public function getDetail(): ?string
  154.     {
  155.         return $this->detail;
  156.     }
  157.     public function setDetail(string $detail null): void
  158.     {
  159.         $this->detail $detail;
  160.     }
  161.     #[Groups(['jsonld''jsonproblem'])]
  162.     public function getInstance(): ?string
  163.     {
  164.         return $this->instance;
  165.     }
  166.     public function setInstance(string $instance null): void
  167.     {
  168.         $this->instance $instance;
  169.     }
  170. }