SerializerErrorRenderer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 $serializer, string|callable $format, ?ErrorRendererInterface $fallbackErrorRenderer = null, bool|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. public function render(\Throwable $exception): FlattenException
  40. {
  41. $headers = ['Vary' => 'Accept'];
  42. $debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
  43. if ($debug) {
  44. $headers['X-Debug-Exception'] = rawurlencode(substr($exception->getMessage(), 0, 2000));
  45. $headers['X-Debug-Exception-File'] = rawurlencode($exception->getFile()).':'.$exception->getLine();
  46. }
  47. $flattenException = FlattenException::createFromThrowable($exception, null, $headers);
  48. try {
  49. $format = \is_string($this->format) ? $this->format : ($this->format)($flattenException);
  50. $headers['Content-Type'] = Request::getMimeTypes($format)[0] ?? $format;
  51. $flattenException->setAsString($this->serializer->serialize($flattenException, $format, [
  52. 'exception' => $exception,
  53. 'debug' => $debug,
  54. ]));
  55. } catch (NotEncodableValueException) {
  56. $flattenException = $this->fallbackErrorRenderer->render($exception);
  57. }
  58. return $flattenException->setHeaders($flattenException->getHeaders() + $headers);
  59. }
  60. public static function getPreferredFormat(RequestStack $requestStack): \Closure
  61. {
  62. return static function () use ($requestStack) {
  63. if (!$request = $requestStack->getCurrentRequest()) {
  64. throw new NotEncodableValueException();
  65. }
  66. return $request->getPreferredFormat();
  67. };
  68. }
  69. }