vendor/symfony/error-handler/ErrorRenderer/SerializerErrorRenderer.php line 80

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.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. namespace Symfony\Component\ErrorHandler\ErrorRenderer;
  11. use Symfony\Component\ErrorHandler\Exception\FlattenException;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Serializer\Exception\NotEncodableValueException;
  15. use Symfony\Component\Serializer\SerializerInterface;
  16. /**
  17.  * Formats an exception using Serializer for rendering.
  18.  *
  19.  * @author Nicolas Grekas <p@tchwork.com>
  20.  */
  21. class SerializerErrorRenderer implements ErrorRendererInterface
  22. {
  23.     private $serializer;
  24.     private $format;
  25.     private $fallbackErrorRenderer;
  26.     private $debug;
  27.     /**
  28.      * @param string|callable(FlattenException) $format The format as a string or a callable that should return it
  29.      *                                                  formats not supported by Request::getMimeTypes() should be given as mime types
  30.      * @param bool|callable                     $debug  The debugging mode as a boolean or a callable that should return it
  31.      */
  32.     public function __construct(SerializerInterface $serializer$formatErrorRendererInterface $fallbackErrorRenderer null$debug false)
  33.     {
  34.         if (!\is_string($format) && !\is_callable($format)) {
  35.             throw new \TypeError(sprintf('Argument 2 passed to "%s()" must be a string or a callable, "%s" given.'__METHOD__, \gettype($format)));
  36.         }
  37.         if (!\is_bool($debug) && !\is_callable($debug)) {
  38.             throw new \TypeError(sprintf('Argument 4 passed to "%s()" must be a boolean or a callable, "%s" given.'__METHOD__, \gettype($debug)));
  39.         }
  40.         $this->serializer $serializer;
  41.         $this->format $format;
  42.         $this->fallbackErrorRenderer $fallbackErrorRenderer ?? new HtmlErrorRenderer();
  43.         $this->debug $debug;
  44.     }
  45.     /**
  46.      * {@inheritdoc}
  47.      */
  48.     public function render(\Throwable $exception): FlattenException
  49.     {
  50.         $headers = [];
  51.         $debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
  52.         if ($debug) {
  53.             $headers['X-Debug-Exception'] = rawurlencode($exception->getMessage());
  54.             $headers['X-Debug-Exception-File'] = rawurlencode($exception->getFile()).':'.$exception->getLine();
  55.         }
  56.         $flattenException FlattenException::createFromThrowable($exceptionnull$headers);
  57.         try {
  58.             $format = \is_string($this->format) ? $this->format : ($this->format)($flattenException);
  59.             $headers = [
  60.                 'Content-Type' => Request::getMimeTypes($format)[0] ?? $format,
  61.                 'Vary' => 'Accept',
  62.             ];
  63.             return $flattenException->setAsString($this->serializer->serialize($flattenException$format, [
  64.                 'exception' => $exception,
  65.                 'debug' => $debug,
  66.             ]))
  67.             ->setHeaders($flattenException->getHeaders() + $headers);
  68.         } catch (NotEncodableValueException $e) {
  69.             return $this->fallbackErrorRenderer->render($exception);
  70.         }
  71.     }
  72.     public static function getPreferredFormat(RequestStack $requestStack): \Closure
  73.     {
  74.         return static function () use ($requestStack) {
  75.             if (!$request $requestStack->getCurrentRequest()) {
  76.                 throw new NotEncodableValueException();
  77.             }
  78.             return $request->getPreferredFormat();
  79.         };
  80.     }
  81. }