diff --git a/components/error_handler.rst b/components/error_handler.rst index f5b1e7b9c11..7452ebce3e5 100644 --- a/components/error_handler.rst +++ b/components/error_handler.rst @@ -67,11 +67,6 @@ to enable this error handler:: ErrorHandler::register(); -.. tip:: - - If you want to get even better exception pages, install the - :doc:`ErrorRenderer component ` too. - Catching PHP Function Errors and Turning Them into Exceptions ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -145,11 +140,6 @@ to enable this exception handler:: ExceptionHandler::register(); -.. tip:: - - If you want to get even better exception pages, install the - :doc:`ErrorRenderer component ` too. - .. _component-debug-class-loader: Class Loading Debugger diff --git a/components/error_renderer.rst b/components/error_renderer.rst deleted file mode 100644 index 607935aa6bb..00000000000 --- a/components/error_renderer.rst +++ /dev/null @@ -1,159 +0,0 @@ -.. index:: - single: Error - single: Exception - single: Components; ErrorRenderer - -The ErrorRenderer Component -=========================== - - The ErrorRenderer component converts PHP errors and exceptions into other - formats such as JSON and HTML and renders them. - -Installation ------------- - -.. code-block:: terminal - - $ composer require symfony/error-renderer - -.. include:: /components/require_autoload.rst.inc - -Usage ------ - -The ErrorRenderer component provides several renderers to convert PHP errors and -exceptions into other formats such as JSON and HTML easier to debug when working -with HTTP applications:: - - use Symfony\Component\ErrorRenderer\ErrorRenderer; - use Symfony\Component\ErrorRenderer\ErrorRenderer\HtmlErrorRenderer; - use Symfony\Component\ErrorRenderer\ErrorRenderer\JsonErrorRenderer; - use Symfony\Component\ErrorRenderer\Exception\FlattenException; - use Symfony\Component\HttpFoundation\Response; - - $renderers = [ - new HtmlErrorRenderer(), - new JsonErrorRenderer(), - // ... - ]; - $errorRenderer = new ErrorRenderer($renderers); - - try { - // ... - } catch (\Throwable $e) { - $e = FlattenException::createFromThrowable($e); - - return new Response($errorRenderer->render($e, 'json'), 500, ['Content-Type' => 'application/json']); - } - -Built-in Error Renderers ------------------------- - -This component provides error renderers for the most common needs: - - * :class:`Symfony\\Component\\ErrorRenderer\\ErrorRenderer\\HtmlErrorRenderer` - renders errors in HTML format; - * :class:`Symfony\\Component\\ErrorRenderer\\ErrorRenderer\\JsonErrorRenderer` - renders errors in JSON format and it's compliant with the `RFC 7807`_ standard; - * :class:`Symfony\\Component\\ErrorRenderer\\ErrorRenderer\\XmlErrorRenderer` - renders errors in XML and Atom formats. It's compliant with the `RFC 7807`_ - standard; - * :class:`Symfony\\Component\\ErrorRenderer\\ErrorRenderer\\TxtErrorRenderer` - renders errors in plain text format. - -Adding a Custom Error Renderer ------------------------------- - -Error renderers are PHP classes that implement the -:class:`Symfony\\Component\\ErrorRenderer\\ErrorRenderer\\ErrorRendererInterface`. -For example, if you need to render errors in `JSON-LD format`_, create this -class anywhere in your project:: - - namespace App\ErrorRenderer; - - use Symfony\Component\ErrorRenderer\ErrorRenderer\ErrorRendererInterface; - use Symfony\Component\ErrorRenderer\Exception\FlattenException; - - class JsonLdErrorRenderer implements ErrorRendererInterface - { - private $debug; - - public function __construct(bool $debug = true) - { - $this->debug = $debug; - } - - public static function getFormat(): string - { - return 'jsonld'; - } - - public function render(FlattenException $exception): string - { - $content = [ - '@id' => 'https://example.com', - '@type' => 'error', - '@context' => [ - 'title' => $exception->getTitle(), - 'code' => $exception->getStatusCode(), - 'message' => $exception->getMessage(), - ], - ]; - - if ($this->debug) { - $content['@context']['exceptions'] = $exception->toArray(); - } - - return (string) json_encode($content); - } - } - -.. tip:: - - If the ``getFormat()`` method of your error renderer matches one of formats - supported by the built-in renderers, the built-in renderer is replaced by - your custom renderer. - -To enable the new error renderer in the application, -:ref:`register it as a service ` and -:doc:`tag it ` with the ``error_renderer.renderer`` -tag. - -.. configuration-block:: - - .. code-block:: yaml - - # config/services.yaml - services: - App\ErrorRenderer\JsonLdErrorRenderer: - arguments: ['%kernel.debug%'] - tags: ['error_renderer.renderer'] - - .. code-block:: xml - - - - - - - - %kernel.debug% - - - - - - .. code-block:: php - - // config/services.php - use App\ErrorRenderer\JsonLdErrorRenderer; - - $container->register(JsonLdErrorRenderer::class) - ->setArguments([$container->getParameter('kernel.debug')]); - ->addTag('error_renderer.renderer'); - -.. _`RFC 7807`: https://tools.ietf.org/html/rfc7807 -.. _`JSON-LD format`: https://en.wikipedia.org/wiki/JSON-LD diff --git a/components/http_kernel.rst b/components/http_kernel.rst index 046ca09a6f6..9ba9226abeb 100644 --- a/components/http_kernel.rst +++ b/components/http_kernel.rst @@ -554,7 +554,7 @@ below for more details). The listener has several goals: 1) The thrown exception is converted into a - :class:`Symfony\\Component\\ErrorRenderer\\Exception\\FlattenException` + :class:`Symfony\\Component\\ErrorHandler\\Exception\\FlattenException` object, which contains all the information about the request, but which can be printed and serialized. diff --git a/controller/error_pages.rst b/controller/error_pages.rst index 482ba6edd95..b1a6e2d3992 100644 --- a/controller/error_pages.rst +++ b/controller/error_pages.rst @@ -247,7 +247,7 @@ the request that will be dispatched to your controller. In addition, your contro will be passed two parameters: ``exception`` - A :class:`\\Symfony\\Component\\ErrorRenderer\\Exception\\FlattenException` + A :class:`\\Symfony\\Component\\ErrorHandler\\Exception\\FlattenException` instance created from the exception being handled. ``logger`` diff --git a/create_framework/http_kernel_httpkernel_class.rst b/create_framework/http_kernel_httpkernel_class.rst index 5845f4219dc..96bda4df692 100644 --- a/create_framework/http_kernel_httpkernel_class.rst +++ b/create_framework/http_kernel_httpkernel_class.rst @@ -69,7 +69,7 @@ Our code is now much more concise and surprisingly more robust and more powerful than ever. For instance, use the built-in ``ExceptionListener`` to make your error management configurable:: - $errorHandler = function (Symfony\Component\ErrorRenderer\Exception\FlattenException $exception) { + $errorHandler = function (Symfony\Component\ErrorHandler\Exception\FlattenException $exception) { $msg = 'Something went wrong! ('.$exception->getMessage().')'; return new Response($msg, $exception->getStatusCode()); @@ -91,7 +91,7 @@ The error controller reads as follows:: // example.com/src/Calendar/Controller/ErrorController.php namespace Calendar\Controller; - use Symfony\Component\ErrorRenderer\Exception\FlattenException; + use Symfony\Component\ErrorHandler\Exception\FlattenException; use Symfony\Component\HttpFoundation\Response; class ErrorController