|
| 1 | +.. index:: |
| 2 | + single: Debug |
| 3 | + single: Error |
| 4 | + single: Exception |
| 5 | + single: Components; ErrorHandler |
| 6 | + |
| 7 | +The ErrorHandler Component |
| 8 | +========================== |
| 9 | + |
| 10 | + The ErrorHandler component provides tools to manage errors and ease debugging PHP code. |
| 11 | + |
| 12 | +Installation |
| 13 | +------------ |
| 14 | + |
| 15 | +.. code-block:: terminal |
| 16 | +
|
| 17 | + $ composer require symfony/error-handler |
| 18 | +
|
| 19 | +.. include:: /components/require_autoload.rst.inc |
| 20 | + |
| 21 | +Usage |
| 22 | +----- |
| 23 | + |
| 24 | +The ErrorHandler component provides several tools to help you debug PHP code. |
| 25 | +Enable all of them by calling this method:: |
| 26 | + |
| 27 | + use Symfony\Component\ErrorHandler\Debug; |
| 28 | + |
| 29 | + Debug::enable(); |
| 30 | + |
| 31 | +The :method:`Symfony\\Component\\ErrorHandler\\Debug::enable` method registers an |
| 32 | +error handler, an exception handler and |
| 33 | +:ref:`a special class loader <component-debug-class-loader>`. |
| 34 | + |
| 35 | +Read the following sections for more information about the different available |
| 36 | +tools. |
| 37 | + |
| 38 | +.. caution:: |
| 39 | + |
| 40 | + You should never enable the debug tools, except for the error handler, in a |
| 41 | + production environment as they might disclose sensitive information to the user. |
| 42 | + |
| 43 | +Handling PHP Errors and Exceptions |
| 44 | +---------------------------------- |
| 45 | + |
| 46 | +Enabling the Error Handler |
| 47 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 48 | + |
| 49 | +The :class:`Symfony\\Component\\ErrorHandler\\ErrorHandler` class catches PHP |
| 50 | +errors and converts them to exceptions (of class :phpclass:`ErrorException` or |
| 51 | +:class:`Symfony\\Component\\ErrorHandler\\Exception\\FatalErrorException` for |
| 52 | +PHP fatal errors):: |
| 53 | + |
| 54 | + use Symfony\Component\ErrorHandler\ErrorHandler; |
| 55 | + |
| 56 | + ErrorHandler::register(); |
| 57 | + |
| 58 | +This error handler is enabled by default in the production environment when the |
| 59 | +application uses the FrameworkBundle because it generates better error logs. |
| 60 | + |
| 61 | +Enabling the Exception Handler |
| 62 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 63 | + |
| 64 | +The :class:`Symfony\\Component\\ErrorHandler\\ExceptionHandler` class catches |
| 65 | +uncaught PHP exceptions and converts them to a nice PHP response. It is useful |
| 66 | +in :ref:`debug mode <debug-mode>` to replace the default PHP/XDebug output with |
| 67 | +something prettier and more useful:: |
| 68 | + |
| 69 | + use Symfony\Component\ErrorHandler\ExceptionHandler; |
| 70 | + |
| 71 | + ExceptionHandler::register(); |
| 72 | + |
| 73 | +.. note:: |
| 74 | + |
| 75 | + If the :doc:`HttpFoundation component </components/http_foundation>` is |
| 76 | + available, the handler uses a Symfony Response object; if not, it falls |
| 77 | + back to a regular PHP response. |
| 78 | + |
| 79 | +Catches PHP errors and turn them into exceptions |
| 80 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 81 | + |
| 82 | +Most PHP core functions were written before exception handling was introduced to the |
| 83 | +language and most of this functions do not throw an exception on failure. Instead, |
| 84 | +they return ``false`` in case of error. |
| 85 | + |
| 86 | +Let's take the following code example:: |
| 87 | + |
| 88 | + $data = json_decode(file_get_contents($filename), true); |
| 89 | + $data['read_at'] = date($datetimeFormat); |
| 90 | + file_put_contents($filename, json_encode($data)); |
| 91 | + |
| 92 | +All these functions ``file_get_contents``, ``json_decode``, ``date``, ``json_encode`` |
| 93 | +and ``file_put_contents`` will return ``false`` or ``null`` on error, having to |
| 94 | +deal with those failures manually:: |
| 95 | + |
| 96 | + $content = @file_get_contents($filename); |
| 97 | + if (false === $content) { |
| 98 | + throw new \RuntimeException('Could not load file.'); |
| 99 | + } |
| 100 | + $data = @json_decode($content, true); |
| 101 | + if (null === $data) { |
| 102 | + throw new \RuntimeException('File does not contain valid JSON.'); |
| 103 | + } |
| 104 | + $datetime = @date($datetimeFormat); |
| 105 | + if (false === $datetime) { |
| 106 | + throw new \RuntimeException('Invalid datetime format.'); |
| 107 | + } |
| 108 | + // ... |
| 109 | + |
| 110 | +.. note:: |
| 111 | + |
| 112 | + Since PHP 7.3 `json_decode`_ function will accept a new ``JSON_THROW_ON_ERROR`` option |
| 113 | + that will let ``json_decode`` throw an exception instead of returning ``null`` on error. |
| 114 | + However, it is not enabled by default, so you will need to explicitly configure it. |
| 115 | + |
| 116 | +To simplify this behavior the :class:`Symfony\\Component\\ErrorHandler\\ErrorHandler` class |
| 117 | +provides a :method:`Symfony\\Component\\ErrorHandler\\ErrorHandler::call` method that will |
| 118 | +automatically throw an exception when such a failure occurs. This method will accept a ``callable`` |
| 119 | +parameter and then the arguments needed to call it, returning back the result:: |
| 120 | + |
| 121 | + $content = ErrorHandler::call('file_get_contents', $filename); |
| 122 | + |
| 123 | +This way, you could use a ``\Closure`` function to wrap a portion of code and be sure that it |
| 124 | +breaks even if the `@-silencing operator`_ is used:: |
| 125 | + |
| 126 | + $data = ErrorHandler::call(static function () use ($filename, $datetimeFormat) { |
| 127 | + $data = json_decode(file_get_contents($filename), true); |
| 128 | + $data['read_at'] = date($datetimeFormat); |
| 129 | + file_put_contents($filename, json_encode($data)); |
| 130 | + |
| 131 | + return $data; |
| 132 | + }); |
| 133 | + |
| 134 | +.. _component-debug-class-loader: |
| 135 | + |
| 136 | +Debugging a Class Loader |
| 137 | +------------------------ |
| 138 | + |
| 139 | +The :class:`Symfony\\Component\\ErrorHandler\\DebugClassLoader` attempts to |
| 140 | +throw more helpful exceptions when a class isn't found by the registered |
| 141 | +autoloaders. All autoloaders that implement a ``findFile()`` method are replaced |
| 142 | +with a ``DebugClassLoader`` wrapper. |
| 143 | + |
| 144 | +Using the ``DebugClassLoader`` is done by calling its static |
| 145 | +:method:`Symfony\\Component\\ErrorHandler\\DebugClassLoader::enable` method:: |
| 146 | + |
| 147 | + use Symfony\Component\ErrorHandler\DebugClassLoader; |
| 148 | + |
| 149 | + DebugClassLoader::enable(); |
| 150 | + |
| 151 | +.. _`@-silencing operator`: https://php.net/manual/en/function.json-decode.php |
| 152 | +.. _`json_decode`: https://php.net/manual/en/language.operators.errorcontrol.php |
0 commit comments