SerializerErrorRenderer.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 string|\Closure $format;
  24. private ErrorRendererInterface $fallbackErrorRenderer;
  25. private bool|\Closure $debug;
  26. /**
  27. * @param string|callable(FlattenException) $format The format as a string or a callable that should return it
  28. * formats not supported by Request::getMimeTypes() should be given as mime types
  29. * @param bool|callable $debug The debugging mode as a boolean or a callable that should return it
  30. */
  31. public function __construct(
  32. private SerializerInterface $serializer,
  33. string|callable $format,
  34. ?ErrorRendererInterface $fallbackErrorRenderer = null,
  35. bool|callable $debug = false,
  36. ) {
  37. $this->format = \is_string($format) ? $format : $format(...);
  38. $this->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
  39. $this->debug = \is_bool($debug) ? $debug : $debug(...);
  40. }
  41. public function render(\Throwable $exception): FlattenException
  42. {
  43. $headers = ['Vary' => 'Accept'];
  44. $debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
  45. if ($debug) {
  46. $headers['X-Debug-Exception'] = rawurlencode(substr($exception->getMessage(), 0, 2000));
  47. $headers['X-Debug-Exception-File'] = rawurlencode($exception->getFile()).':'.$exception->getLine();
  48. }
  49. $flattenException = FlattenException::createFromThrowable($exception, null, $headers);
  50. try {
  51. $format = \is_string($this->format) ? $this->format : ($this->format)($flattenException);
  52. $headers['Content-Type'] = Request::getMimeTypes($format)[0] ?? $format;
  53. $flattenException->setAsString($this->serializer->serialize($flattenException, $format, [
  54. 'exception' => $exception,
  55. 'debug' => $debug,
  56. ]));
  57. } catch (NotEncodableValueException) {
  58. $flattenException = $this->fallbackErrorRenderer->render($exception);
  59. }
  60. return $flattenException->setHeaders($flattenException->getHeaders() + $headers);
  61. }
  62. public static function getPreferredFormat(RequestStack $requestStack): \Closure
  63. {
  64. return static function () use ($requestStack) {
  65. if (!$request = $requestStack->getCurrentRequest()) {
  66. throw new NotEncodableValueException();
  67. }
  68. return $request->getPreferredFormat();
  69. };
  70. }
  71. }