|
| 1 | +.. index:: |
| 2 | + single: Error |
| 3 | + single: Exception |
| 4 | + single: Components; ErrorCatcher |
| 5 | + |
| 6 | +The ErrorCatcher Component |
| 7 | +========================== |
| 8 | + |
| 9 | + The ErrorCatcher component converts PHP errors and exceptions into other |
| 10 | + formats such as JSON and HTML and renders them. |
| 11 | + |
| 12 | +Installation |
| 13 | +------------ |
| 14 | + |
| 15 | +.. code-block:: terminal |
| 16 | +
|
| 17 | + $ composer require symfony/error-catcher |
| 18 | +
|
| 19 | +.. include:: /components/require_autoload.rst.inc |
| 20 | + |
| 21 | +Usage |
| 22 | +----- |
| 23 | + |
| 24 | +The ErrorCatcher component provides several handlers and renderers to convert |
| 25 | +PHP errors and exceptions into other formats easier to debug when working with |
| 26 | +HTTP applications. |
| 27 | + |
| 28 | +.. TODO: how are these handlers enabled in the app? (Previously: Debug::enable()) |
| 29 | +
|
| 30 | +Handling PHP Errors and Exceptions |
| 31 | +---------------------------------- |
| 32 | + |
| 33 | +Enabling the Error Handler |
| 34 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 35 | + |
| 36 | +The :class:`Symfony\\Component\\ErrorCatcher\\ErrorHandler` class catches PHP |
| 37 | +errors and converts them to exceptions (of class :phpclass:`ErrorException` or |
| 38 | +:class:`Symfony\\Component\\ErrorCatcher\\Exception\\FatalErrorException` for |
| 39 | +PHP fatal errors):: |
| 40 | + |
| 41 | + use Symfony\Component\ErrorCatcher\ErrorHandler; |
| 42 | + |
| 43 | + ErrorHandler::register(); |
| 44 | + |
| 45 | +This error handler is enabled by default in the production environment when the |
| 46 | +application uses the FrameworkBundle because it generates better error logs. |
| 47 | + |
| 48 | +Enabling the Exception Handler |
| 49 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 50 | + |
| 51 | +The :class:`Symfony\\Component\\ErrorCatcher\\ExceptionHandler` class catches |
| 52 | +uncaught PHP exceptions and converts them to a nice PHP response. It is useful |
| 53 | +in :ref:`debug mode <debug-mode>` to replace the default PHP/XDebug output with |
| 54 | +something prettier and more useful:: |
| 55 | + |
| 56 | + use Symfony\Component\ErrorCatcher\ExceptionHandler; |
| 57 | + |
| 58 | + ExceptionHandler::register(); |
| 59 | + |
| 60 | +.. note:: |
| 61 | + |
| 62 | + If the :doc:`HttpFoundation component </components/http_foundation>` is |
| 63 | + available, the handler uses a Symfony Response object; if not, it falls |
| 64 | + back to a regular PHP response. |
| 65 | + |
| 66 | +Rendering PHP Errors and Exceptions |
| 67 | +----------------------------------- |
| 68 | + |
| 69 | +Another feature provided by this component are the "error renderers", which |
| 70 | +converts PHP errors and exceptions into other formats such as JSON and HTML:: |
| 71 | + |
| 72 | + use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRenderer; |
| 73 | + use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer; |
| 74 | + use Symfony\Component\ErrorHandler\ErrorRenderer\JsonErrorRenderer; |
| 75 | + |
| 76 | + $renderers = [ |
| 77 | + new HtmlErrorRenderer(), |
| 78 | + new JsonErrorRenderer(), |
| 79 | + // ... |
| 80 | + ]; |
| 81 | + $errorRenderer = new ErrorRenderer($renderers); |
| 82 | + |
| 83 | + /** @var Symfony\Component\ErrorHandler\Exception\FlattenException */ |
| 84 | + $exception = ...; |
| 85 | + /** @var Symfony\Component\HttpFoundation\Request */ |
| 86 | + $request = ...; |
| 87 | + |
| 88 | + return new Response( |
| 89 | + $errorRenderer->render($exception, $request->getRequestFormat()), |
| 90 | + $exception->getStatusCode(), |
| 91 | + $exception->getHeaders() |
| 92 | + ); |
| 93 | + |
| 94 | +Built-in Error Renderers |
| 95 | +~~~~~~~~~~~~~~~~~~~~~~~~ |
| 96 | + |
| 97 | +This component provides error renderers for the most common needs: |
| 98 | + |
| 99 | + * :class:`Symfony\\Component\\ErrorHandler\\ErrorRenderer\\HtmlErrorRenderer` |
| 100 | + renders errors in HTML format; |
| 101 | + * :class:`Symfony\\Component\\ErrorHandler\\ErrorRenderer\\JsonErrorRenderer` |
| 102 | + renders errors in JsonErrorRenderer format and it's compliant with the |
| 103 | + `RFC 7807`_ standard; |
| 104 | + * :class:`Symfony\\Component\\ErrorHandler\\ErrorRenderer\\XmlErrorRenderer` |
| 105 | + renders errors in XML and Atom formats. It's compliant with the `RFC 7807`_ |
| 106 | + standard; |
| 107 | + * :class:`Symfony\\Component\\ErrorHandler\\ErrorRenderer\\TxtErrorRenderer` |
| 108 | + renders errors in regular text format. |
| 109 | + |
| 110 | +Adding a Custom Error Renderer |
| 111 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 112 | + |
| 113 | +Error renderers are PHP classes that implement the |
| 114 | +:class:`Symfony\\Component\\ErrorHandler\\ErrorRenderer\\ErrorRendererInterface`. |
| 115 | +For example, if you need to render errors in `JSON-LD format`_, create this |
| 116 | +class anywhere in your project:: |
| 117 | + |
| 118 | + namespace App\ErrorHandler\ErrorRenderer; |
| 119 | + |
| 120 | + use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface; |
| 121 | + use Symfony\Component\ErrorHandler\Exception\FlattenException; |
| 122 | + |
| 123 | + class JsonLdErrorRenderer implements ErrorRendererInterface |
| 124 | + { |
| 125 | + private $debug; |
| 126 | + |
| 127 | + public static function getFormat(): string |
| 128 | + { |
| 129 | + return 'jsonld'; |
| 130 | + } |
| 131 | + |
| 132 | + public function __construct(bool $debug = true) |
| 133 | + { |
| 134 | + $this->debug = $debug; |
| 135 | + } |
| 136 | + |
| 137 | + public function render(FlattenException $exception): string |
| 138 | + { |
| 139 | + $content = [ |
| 140 | + '@id' => 'https://example.com', |
| 141 | + '@type' => 'error', |
| 142 | + '@context' => [ |
| 143 | + 'title' => $exception->getTitle(), |
| 144 | + 'code' => $exception->getStatusCode(), |
| 145 | + 'message' => $exception->getMessage(), |
| 146 | + ], |
| 147 | + ]; |
| 148 | + |
| 149 | + if ($this->debug) { |
| 150 | + $content['@context']['exceptions'] = $exception->toArray(); |
| 151 | + } |
| 152 | + |
| 153 | + return (string) json_encode($content); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | +.. tip:: |
| 158 | + |
| 159 | + If the ``getformat()`` method of your error renderer matches one of formats |
| 160 | + supported by the built-in renderers, the built-in renderer is replaced by |
| 161 | + your custom renderer. |
| 162 | + |
| 163 | +To enable the new error renderer in the application, |
| 164 | +:ref:`register it as a service <service-container-creating-service>` and |
| 165 | +:doc:`tag it </service_container/tags>` with the ``error_handler.renderer`` |
| 166 | +tag. |
| 167 | + |
| 168 | +.. configuration-block:: |
| 169 | + |
| 170 | + .. code-block:: yaml |
| 171 | +
|
| 172 | + # config/services.yaml |
| 173 | + services: |
| 174 | + App\ErrorHandler\ErrorRenderer\JsonLdErrorRenderer: |
| 175 | + arguments: ['%kernel.debug%'] |
| 176 | + tags: ['error_handler.renderer'] |
| 177 | +
|
| 178 | + .. code-block:: xml |
| 179 | +
|
| 180 | + <!-- config/services.xml --> |
| 181 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 182 | + <container xmlns="http://symfony.com/schema/dic/services" |
| 183 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 184 | + xsi:schemaLocation="http://symfony.com/schema/dic/services |
| 185 | + https://symfony.com/schema/dic/services/services-1.0.xsd"> |
| 186 | +
|
| 187 | + <services> |
| 188 | + <service id="App\ErrorHandler\ErrorRenderer\JsonLdErrorRenderer"> |
| 189 | + <argument>true</argument> |
| 190 | + <tag name="error_handler.renderer"/> |
| 191 | + </service> |
| 192 | + </services> |
| 193 | + </container> |
| 194 | +
|
| 195 | + .. code-block:: php |
| 196 | +
|
| 197 | + // config/services.php |
| 198 | + use App\ErrorHandler\ErrorRenderer\JsonLdErrorRenderer; |
| 199 | +
|
| 200 | + $container->register(JsonLdErrorRenderer::class) |
| 201 | + ->setArguments([true]); |
| 202 | + ->addTag('error_handler.renderer'); |
| 203 | +
|
| 204 | +.. _`RFC 7807`: https://tools.ietf.org/html/rfc7807 |
| 205 | +.. _`JSON-LD format`: https://en.wikipedia.org/wiki/JSON-LD |
0 commit comments