SerializerErrorRenderer.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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, $format, ErrorRendererInterface $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__, \is_object($format) ? \get_class($format) : \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__, \is_object($debug) ? \get_class($debug) : \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. $flattenException = FlattenException::createFromThrowable($exception);
  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' => \is_bool($this->debug) ? $this->debug : ($this->debug)($exception),
  60. ]))
  61. ->setHeaders($flattenException->getHeaders() + $headers);
  62. } catch (NotEncodableValueException $e) {
  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. }