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

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 SerializerInterface $serializer;
  24.     private string|\Closure $format;
  25.     private ErrorRendererInterface $fallbackErrorRenderer;
  26.     private bool|\Closure $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 $serializerstring|callable $formatErrorRendererInterface $fallbackErrorRenderer nullbool|callable $debug false)
  33.     {
  34.         $this->serializer $serializer;
  35.         $this->format \is_string($format) ? $format $format(...);
  36.         $this->fallbackErrorRenderer $fallbackErrorRenderer ?? new HtmlErrorRenderer();
  37.         $this->debug \is_bool($debug) ? $debug $debug(...);
  38.     }
  39.     /**
  40.      * {@inheritdoc}
  41.      */
  42.     public function render(\Throwable $exception): FlattenException
  43.     {
  44.         $headers = [];
  45.         $debug \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
  46.         if ($debug) {
  47.             $headers['X-Debug-Exception'] = rawurlencode($exception->getMessage());
  48.             $headers['X-Debug-Exception-File'] = rawurlencode($exception->getFile()).':'.$exception->getLine();
  49.         }
  50.         $flattenException FlattenException::createFromThrowable($exceptionnull$headers);
  51.         try {
  52.             $format \is_string($this->format) ? $this->format : ($this->format)($flattenException);
  53.             $headers = [
  54.                 'Content-Type' => Request::getMimeTypes($format)[0] ?? $format,
  55.                 'Vary' => 'Accept',
  56.             ];
  57.             return $flattenException->setAsString($this->serializer->serialize($flattenException$format, [
  58.                 'exception' => $exception,
  59.                 'debug' => $debug,
  60.             ]))
  61.             ->setHeaders($flattenException->getHeaders() + $headers);
  62.         } catch (NotEncodableValueException) {
  63.             return $this->fallbackErrorRenderer->render($exception);
  64.         }
  65.     }
  66.     public static function getPreferredFormat(RequestStack $requestStack): \Closure
  67.     {
  68.         return static function () use ($requestStack) {
  69.             if (!$request $requestStack->getCurrentRequest()) {
  70.                 throw new NotEncodableValueException();
  71.             }
  72.             return $request->getPreferredFormat();
  73.         };
  74.     }
  75. }