vendor/api-platform/core/src/Action/ExceptionAction.php line 42

Open in your IDE?
  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\Core\Action;
  12. use ApiPlatform\Core\Util\ErrorFormatGuesser;
  13. use Symfony\Component\Debug\Exception\FlattenException;
  14. use Symfony\Component\HttpFoundation\Request;
  15. use Symfony\Component\HttpFoundation\Response;
  16. use Symfony\Component\Serializer\SerializerInterface;
  17. /**
  18.  * Renders a normalized exception for a given {@see \Symfony\Component\Debug\Exception\FlattenException}.
  19.  *
  20.  * Usage:
  21.  *
  22.  *     $exceptionAction = new ExceptionAction(
  23.  *         new Serializer(),
  24.  *         [
  25.  *             'jsonproblem' => ['application/problem+json'],
  26.  *             'jsonld' => ['application/ld+json'],
  27.  *         ],
  28.  *         [
  29.  *             ExceptionInterface::class => Response::HTTP_BAD_REQUEST,
  30.  *             InvalidArgumentException::class => Response::HTTP_BAD_REQUEST,
  31.  *         ]
  32.  *     );
  33.  *
  34.  * @author Baptiste Meyer <baptiste.meyer@gmail.com>
  35.  * @author Kévin Dunglas <dunglas@gmail.com>
  36.  */
  37. final class ExceptionAction
  38. {
  39.     private $serializer;
  40.     private $errorFormats;
  41.     private $exceptionToStatus;
  42.     /**
  43.      * @param array $errorFormats      A list of enabled formats, the first one will be the default
  44.      * @param array $exceptionToStatus A list of exceptions mapped to their HTTP status code
  45.      */
  46.     public function __construct(SerializerInterface $serializer, array $errorFormats, array $exceptionToStatus = [])
  47.     {
  48.         $this->serializer $serializer;
  49.         $this->errorFormats $errorFormats;
  50.         $this->exceptionToStatus $exceptionToStatus;
  51.     }
  52.     /**
  53.      * Converts an exception to a JSON response.
  54.      */
  55.     public function __invoke(FlattenException $exceptionRequest $request): Response
  56.     {
  57.         $exceptionClass $exception->getClass();
  58.         $statusCode $exception->getStatusCode();
  59.         foreach ($this->exceptionToStatus as $class => $status) {
  60.             if (is_a($exceptionClass$classtrue)) {
  61.                 $statusCode $status;
  62.                 break;
  63.             }
  64.         }
  65.         $headers $exception->getHeaders();
  66.         $format ErrorFormatGuesser::guessErrorFormat($request$this->errorFormats);
  67.         $headers['Content-Type'] = sprintf('%s; charset=utf-8'$format['value'][0]);
  68.         $headers['X-Content-Type-Options'] = 'nosniff';
  69.         $headers['X-Frame-Options'] = 'deny';
  70.         return new Response($this->serializer->serialize($exception$format['key'], ['statusCode' => $statusCode]), $statusCode$headers);
  71.     }
  72. }