SerializerErrorRenderer.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\RequestStack;
  13. use Symfony\Component\Serializer\Exception\NotEncodableValueException;
  14. use Symfony\Component\Serializer\SerializerInterface;
  15. /**
  16. * Formats an exception using Serializer for rendering.
  17. *
  18. * @author Nicolas Grekas <p@tchwork.com>
  19. */
  20. class SerializerErrorRenderer implements ErrorRendererInterface
  21. {
  22. private $serializer;
  23. private $format;
  24. private $fallbackErrorRenderer;
  25. private $debug;
  26. /**
  27. * @param string|callable(FlattenException) $format The format as a string or a callable that should return it
  28. * @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
  29. */
  30. public function __construct(SerializerInterface $serializer, $format, ErrorRendererInterface $fallbackErrorRenderer = null, $debug = false)
  31. {
  32. if (!\is_string($format) && !\is_callable($format)) {
  33. throw new \TypeError(sprintf('Argument 2 passed to %s() must be a string or a callable, %s given.', __METHOD__, \is_object($format) ? \get_class($format) : \gettype($format)));
  34. }
  35. if (!\is_bool($debug) && !\is_callable($debug)) {
  36. throw new \TypeError(sprintf('Argument 4 passed to %s() must be a boolean or a callable, %s given.', __METHOD__, \is_object($debug) ? \get_class($debug) : \gettype($debug)));
  37. }
  38. $this->serializer = $serializer;
  39. $this->format = $format;
  40. $this->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
  41. $this->debug = $debug;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function render(\Throwable $exception): FlattenException
  47. {
  48. $flattenException = FlattenException::createFromThrowable($exception);
  49. try {
  50. $format = \is_string($this->format) ? $this->format : ($this->format)($flattenException);
  51. return $flattenException->setAsString($this->serializer->serialize($flattenException, $format, [
  52. 'exception' => $exception,
  53. 'debug' => \is_bool($this->debug) ? $this->debug : ($this->debug)($exception),
  54. ]));
  55. } catch (NotEncodableValueException $e) {
  56. return $this->fallbackErrorRenderer->render($exception);
  57. }
  58. }
  59. public static function getPreferredFormat(RequestStack $requestStack): \Closure
  60. {
  61. return static function () use ($requestStack) {
  62. if (!$request = $requestStack->getCurrentRequest()) {
  63. throw new NotEncodableValueException();
  64. }
  65. return $request->getPreferredFormat();
  66. };
  67. }
  68. }