Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. SerializerErrorRenderer.php

class SerializerErrorRenderer

Formats an exception using Serializer for rendering.

@author Nicolas Grekas <p@tchwork.com>

Hierarchy

  • class \Symfony\Component\ErrorHandler\ErrorRenderer\SerializerErrorRenderer implements \Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface

Expanded class hierarchy of SerializerErrorRenderer

File

vendor/symfony/error-handler/ErrorRenderer/SerializerErrorRenderer.php, line 25

Namespace

Symfony\Component\ErrorHandler\ErrorRenderer
View source
class SerializerErrorRenderer implements ErrorRendererInterface {
    private string|\Closure $format;
    private ErrorRendererInterface $fallbackErrorRenderer;
    private bool|\Closure $debug;
    
    /**
     * @param string|callable(FlattenException) $format The format as a string or a callable that should return it
     *                                                  formats not supported by Request::getMimeTypes() should be given as mime types
     * @param bool|callable                     $debug  The debugging mode as a boolean or a callable that should return it
     */
    public function __construct(SerializerInterface $serializer, string|callable $format, ?ErrorRendererInterface $fallbackErrorRenderer = null, bool|callable $debug = false) {
        $this->format = \is_string($format) ? $format : $format(...);
        $this->fallbackErrorRenderer = $fallbackErrorRenderer ?? new HtmlErrorRenderer();
        $this->debug = \is_bool($debug) ? $debug : $debug(...);
    }
    public function render(\Throwable $exception) : FlattenException {
        $headers = [
            'Vary' => 'Accept',
        ];
        $debug = \is_bool($this->debug) ? $this->debug : ($this->debug)($exception);
        if ($debug) {
            $headers['X-Debug-Exception'] = rawurlencode(substr($exception->getMessage(), 0, 2000));
            $headers['X-Debug-Exception-File'] = rawurlencode($exception->getFile()) . ':' . $exception->getLine();
        }
        $flattenException = FlattenException::createFromThrowable($exception, null, $headers);
        try {
            $format = \is_string($this->format) ? $this->format : ($this->format)($flattenException);
            $headers['Content-Type'] = Request::getMimeTypes($format)[0] ?? $format;
            $flattenException->setAsString($this->serializer
                ->serialize($flattenException, $format, [
                'exception' => $exception,
                'debug' => $debug,
            ]));
        } catch (NotEncodableValueException) {
            $flattenException = $this->fallbackErrorRenderer
                ->render($exception);
        }
        return $flattenException->setHeaders($flattenException->getHeaders() + $headers);
    }
    public static function getPreferredFormat(RequestStack $requestStack) : \Closure {
        return static function () use ($requestStack) {
            if (!($request = $requestStack->getCurrentRequest())) {
                throw new NotEncodableValueException();
            }
            return $request->getPreferredFormat();
        };
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ErrorRendererInterface::IDE_LINK_FORMATS public constant
SerializerErrorRenderer::$debug private property
SerializerErrorRenderer::$fallbackErrorRenderer private property
SerializerErrorRenderer::$format private property
SerializerErrorRenderer::getPreferredFormat public static function
SerializerErrorRenderer::render public function Renders a Throwable as a FlattenException. Overrides ErrorRendererInterface::render
SerializerErrorRenderer::__construct public function

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal