-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Closed
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,205 @@ | ||
.. index:: | ||
single: Error | ||
single: Exception | ||
single: Components; ErrorCatcher | ||
|
||
The ErrorCatcher Component | ||
========================== | ||
|
||
The ErrorCatcher 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-catcher | ||
|
||
.. include:: /components/require_autoload.rst.inc | ||
|
||
Usage | ||
----- | ||
|
||
The ErrorCatcher component provides several handlers and renderers to convert | ||
PHP errors and exceptions into other formats easier to debug when working with | ||
HTTP applications. | ||
|
||
.. TODO: how are these handlers enabled in the app? (Previously: Debug::enable()) | ||
|
||
Handling PHP Errors and Exceptions | ||
---------------------------------- | ||
|
||
Enabling the Error Handler | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The :class:`Symfony\\Component\\ErrorCatcher\\ErrorHandler` class catches PHP | ||
errors and converts them to exceptions (of class :phpclass:`ErrorException` or | ||
:class:`Symfony\\Component\\ErrorCatcher\\Exception\\FatalErrorException` for | ||
PHP fatal errors):: | ||
|
||
use Symfony\Component\ErrorCatcher\ErrorHandler; | ||
|
||
ErrorHandler::register(); | ||
|
||
This error handler is enabled by default in the production environment when the | ||
application uses the FrameworkBundle because it generates better error logs. | ||
|
||
Enabling the Exception Handler | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The :class:`Symfony\\Component\\ErrorCatcher\\ExceptionHandler` class catches | ||
uncaught PHP exceptions and converts them to a nice PHP response. It is useful | ||
in :ref:`debug mode <debug-mode>` to replace the default PHP/XDebug output with | ||
something prettier and more useful:: | ||
|
||
use Symfony\Component\ErrorCatcher\ExceptionHandler; | ||
|
||
ExceptionHandler::register(); | ||
|
||
.. note:: | ||
|
||
If the :doc:`HttpFoundation component </components/http_foundation>` is | ||
available, the handler uses a Symfony Response object; if not, it falls | ||
back to a regular PHP response. | ||
|
||
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; | ||
javiereguiluz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
$renderers = [ | ||
new HtmlErrorRenderer(), | ||
new JsonErrorRenderer(), | ||
// ... | ||
]; | ||
$errorRenderer = new ErrorRenderer($renderers); | ||
javiereguiluz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** @var Symfony\Component\ErrorHandler\Exception\FlattenException */ | ||
$exception = ...; | ||
/** @var Symfony\Component\HttpFoundation\Request */ | ||
$request = ...; | ||
|
||
return new Response( | ||
$errorRenderer->render($exception, $request->getRequestFormat()), | ||
$exception->getStatusCode(), | ||
$exception->getHeaders() | ||
); | ||
|
||
Built-in Error Renderers | ||
~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
This component provides error renderers for the most common needs: | ||
|
||
* :class:`Symfony\\Component\\ErrorHandler\\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` | ||
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. | ||
|
||
Adding a Custom Error Renderer | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Error renderers are PHP classes that implement the | ||
:class:`Symfony\\Component\\ErrorHandler\\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; | ||
|
||
use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface; | ||
use Symfony\Component\ErrorHandler\Exception\FlattenException; | ||
|
||
class JsonLdErrorRenderer implements ErrorRendererInterface | ||
{ | ||
private $debug; | ||
|
||
public static function getFormat(): string | ||
{ | ||
return 'jsonld'; | ||
} | ||
|
||
public function __construct(bool $debug = true) | ||
{ | ||
$this->debug = $debug; | ||
} | ||
|
||
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 | ||
javiereguiluz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 <service-container-creating-service>` and | ||
:doc:`tag it </service_container/tags>` with the ``error_handler.renderer`` | ||
javiereguiluz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tag. | ||
|
||
.. configuration-block:: | ||
|
||
.. code-block:: yaml | ||
|
||
# config/services.yaml | ||
services: | ||
App\ErrorHandler\ErrorRenderer\JsonLdErrorRenderer: | ||
arguments: ['%kernel.debug%'] | ||
tags: ['error_handler.renderer'] | ||
|
||
.. code-block:: xml | ||
|
||
<!-- config/services.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services | ||
https://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<service id="App\ErrorHandler\ErrorRenderer\JsonLdErrorRenderer"> | ||
<argument>true</argument> | ||
<tag name="error_handler.renderer"/> | ||
</service> | ||
</services> | ||
</container> | ||
|
||
.. code-block:: php | ||
|
||
// config/services.php | ||
use App\ErrorHandler\ErrorRenderer\JsonLdErrorRenderer; | ||
|
||
$container->register(JsonLdErrorRenderer::class) | ||
->setArguments([true]); | ||
->addTag('error_handler.renderer'); | ||
|
||
.. _`RFC 7807`: https://tools.ietf.org/html/rfc7807 | ||
.. _`JSON-LD format`: https://en.wikipedia.org/wiki/JSON-LD |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,8 @@ new kernel. | |
.. index:: | ||
single: Configuration; Debug mode | ||
|
||
.. _debug-mode: | ||
|
||
Debug Mode | ||
~~~~~~~~~~ | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.