Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Documented the ErrorRenderer component #11864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixes
  • Loading branch information
javiereguiluz committed Jul 3, 2019
commit 74472737cba1f3aab767e4931bb310e06f672892
47 changes: 23 additions & 24 deletions 47 components/error_catcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,24 +69,24 @@ Rendering PHP Errors and Exceptions
Another feature provided by this component are the "error renderers", which
converts PHP errors and exceptions into other formats such as JSON and HTML::

use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRenderer;
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorHandler\ErrorRenderer\JsonErrorRenderer;
use Symfony\Component\ErrorCatcher\ErrorRenderer\ErrorRenderer;
use Symfony\Component\ErrorCatcher\ErrorRenderer\HtmlErrorRenderer;
use Symfony\Component\ErrorCatcher\ErrorRenderer\JsonErrorRenderer;

$renderers = [
new HtmlErrorRenderer(),
new JsonErrorRenderer(),
// ...
];
$errorRenderer = new ErrorRenderer($renderers);
$errorFormatter = new ErrorFormatter($renderers);

/** @var Symfony\Component\ErrorHandler\Exception\FlattenException */
/** @var Symfony\Component\ErrorCatcher\Exception\FlattenException */
$exception = ...;
/** @var Symfony\Component\HttpFoundation\Request */
$request = ...;

return new Response(
$errorRenderer->render($exception, $request->getRequestFormat()),
$errorFormatter->render($exception, $request->getRequestFormat()),
$exception->getStatusCode(),
$exception->getHeaders()
);
Expand All @@ -96,29 +96,28 @@ Built-in Error Renderers

This component provides error renderers for the most common needs:

* :class:`Symfony\\Component\\ErrorHandler\\ErrorRenderer\\HtmlErrorRenderer`
* :class:`Symfony\\Component\\ErrorCatcher\\ErrorRenderer\\HtmlErrorRenderer`
renders errors in HTML format;
* :class:`Symfony\\Component\\ErrorHandler\\ErrorRenderer\\JsonErrorRenderer`
renders errors in JsonErrorRenderer format and it's compliant with the
`RFC 7807`_ standard;
* :class:`Symfony\\Component\\ErrorHandler\\ErrorRenderer\\XmlErrorRenderer`
* :class:`Symfony\\Component\\ErrorCatcher\\ErrorRenderer\\JsonErrorRenderer`
renders errors in JSON format and it's compliant with the `RFC 7807`_ standard;
* :class:`Symfony\\Component\\ErrorCatcher\\ErrorRenderer\\XmlErrorRenderer`
renders errors in XML and Atom formats. It's compliant with the `RFC 7807`_
standard;
* :class:`Symfony\\Component\\ErrorHandler\\ErrorRenderer\\TxtErrorRenderer`
renders errors in regular text format.
* :class:`Symfony\\Component\\ErrorCatcher\\ErrorRenderer\\TxtErrorRenderer`
renders errors in plain text format.

Adding a Custom Error Renderer
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Error renderers are PHP classes that implement the
:class:`Symfony\\Component\\ErrorHandler\\ErrorRenderer\\ErrorRendererInterface`.
:class:`Symfony\\Component\\ErrorCatcher\\ErrorRenderer\\ErrorRendererInterface`.
For example, if you need to render errors in `JSON-LD format`_, create this
class anywhere in your project::

namespace App\ErrorHandler\ErrorRenderer;
namespace App\ErrorCatcher;

use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface;
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\ErrorCatcher\ErrorRenderer\ErrorRendererInterface;
use Symfony\Component\ErrorCatcher\Exception\FlattenException;

class JsonLdErrorRenderer implements ErrorRendererInterface
{
Expand Down Expand Up @@ -162,7 +161,7 @@ class anywhere in your project::

To enable the new error renderer in the application,
:ref:`register it as a service <service-container-creating-service>` and
:doc:`tag it </service_container/tags>` with the ``error_handler.renderer``
:doc:`tag it </service_container/tags>` with the ``error_catcher.renderer``
tag.

.. configuration-block::
Expand All @@ -171,9 +170,9 @@ tag.

# config/services.yaml
services:
App\ErrorHandler\ErrorRenderer\JsonLdErrorRenderer:
App\ErrorCatcher\JsonLdErrorRenderer:
arguments: ['%kernel.debug%']
tags: ['error_handler.renderer']
tags: ['error_catcher.renderer']

.. code-block:: xml

Expand All @@ -185,21 +184,21 @@ tag.
https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="App\ErrorHandler\ErrorRenderer\JsonLdErrorRenderer">
<service id="App\ErrorCatcher\JsonLdErrorRenderer">
<argument>true</argument>
<tag name="error_handler.renderer"/>
<tag name="error_catcher.renderer"/>
</service>
</services>
</container>

.. code-block:: php

// config/services.php
use App\ErrorHandler\ErrorRenderer\JsonLdErrorRenderer;
use App\ErrorCatcher\JsonLdErrorRenderer;

$container->register(JsonLdErrorRenderer::class)
->setArguments([true]);
->addTag('error_handler.renderer');
->addTag('error_catcher.renderer');

.. _`RFC 7807`: https://tools.ietf.org/html/rfc7807
.. _`JSON-LD format`: https://en.wikipedia.org/wiki/JSON-LD
Morty Proxy This is a proxified and sanitized view of the page, visit original site.