Deprecated: Constant E_STRICT is deprecated in /home/normanv/www/annuairepro/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/normanv/www/annuairepro/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

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

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 = ['Vary' => 'Accept'];
  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['Content-Type'] = Request::getMimeTypes($format)[0] ?? $format;
  60.             $flattenException->setAsString($this->serializer->serialize($flattenException$format, [
  61.                 'exception' => $exception,
  62.                 'debug' => $debug,
  63.             ]));
  64.         } catch (NotEncodableValueException $e) {
  65.             $flattenException $this->fallbackErrorRenderer->render($exception);
  66.         }
  67.         return $flattenException->setHeaders($flattenException->getHeaders() + $headers);
  68.     }
  69.     public static function getPreferredFormat(RequestStack $requestStack): \Closure
  70.     {
  71.         return static function () use ($requestStack) {
  72.             if (!$request $requestStack->getCurrentRequest()) {
  73.                 throw new NotEncodableValueException();
  74.             }
  75.             return $request->getPreferredFormat();
  76.         };
  77.     }
  78. }