You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The ThrowableHandler package provides handling for [throwable](https://www.php.net/throwable) with rich formatting support for console, HTML and plain text documents. Its purpose is to ease the understanding of [Exceptions](https://www.php.net/manual/en/language.exceptions.php) on production systems by providing a consistent and user-friendly output.
24
+
25
+
Read [Throwable Handler for PHP](https://rodolfoberrios.com/2022/05/03/throwable-handler/) at Rodolfo's blog for a comprehensive introduction to this package.
26
+
27
+
## Installing
22
28
23
-
Install ThrowableHandler using [Composer](https://getcomposer.org).
29
+
ThrowableHandler is available through [Packagist](https://packagist.org/packages/chevere/throwable-handler) and the repository source is at [chevere/throwable-handle](https://github.com/chevere/throwable-handler).
24
30
25
31
```sh
26
32
composer require chevere/throwable-handler
27
33
```
28
34
29
-
Register ThrowableHandler to handle all errors.
35
+
## Quick start
36
+
37
+
For quick catch-all use, first register ThrowableHandler to handle all errors.
Use the following helpers to forward errors as exceptions, which will be then handled by ThrowableHandler.
85
+
86
+
### errorAsException
87
+
88
+
Use function `ThrowableHandler::ERROR_AS_EXCEPTION` to handle errors as exceptions. By doing this the system will throw exception instead of emitting errors.
Use function `ThrowableHandler::SHUTDOWN_ERROR_AS_EXCEPTION` to register errors on shutdown. This will take care or register errors in shutdown by forwarding the error to the exception handler.
Use the following helpers to quick handle throwables by registering global handlers.
109
+
110
+
### Plain handler
111
+
112
+
Use `ThrowableHandler::PLAIN` to set plain handler for all exceptions.
113
+
114
+
```php
115
+
use Chevere\ThrowableHandler\ThrowableHandler;
116
+
117
+
set_exception_handler(ThrowableHandler::PLAIN);
118
+
```
119
+
120
+
### Console handler
121
+
122
+
Use `ThrowableHandler::CONSOLE` to set console handler for all exceptions.
123
+
124
+
```php
125
+
use Chevere\ThrowableHandler\ThrowableHandler;
126
+
127
+
set_exception_handler(ThrowableHandler::CONSOLE);
128
+
```
129
+
130
+
### HTML handler
131
+
132
+
Use `ThrowableHandler::HTML` to set console handler for all exceptions.
133
+
134
+
```php
135
+
use Chevere\ThrowableHandler\ThrowableHandler;
136
+
137
+
set_exception_handler(ThrowableHandler::HTML);
138
+
```
139
+
140
+
## Triggered handling
141
+
142
+
Use the following helpers to quick handle catches for throwables.
143
+
144
+
### handleAsPlain
145
+
146
+
Use function `handleAsPlain` to handle throwable as plain text.
147
+
148
+
```php
149
+
use function Chevere\ThrowableHandler\handleAsPlain;
150
+
151
+
try {
152
+
// ...
153
+
} catch(Throwable $e) {
154
+
handleAsPlain($e);
155
+
}
156
+
```
157
+
158
+
### handleAsConsole
159
+
160
+
Use function `handleAsConsole` to handle throwable as console text.
161
+
162
+
```php
163
+
use function Chevere\ThrowableHandler\handleAsConsole;
164
+
165
+
try {
166
+
// ...
167
+
} catch(Throwable $e) {
168
+
handleAsConsole($e);
169
+
}
170
+
```
171
+
172
+
### htmlHandler
173
+
174
+
Use function `htmlHandler` to handle throwable as HTML.
175
+
176
+
```php
177
+
use function Chevere\ThrowableHandler\htmlHandler;
178
+
179
+
try {
180
+
// ...
181
+
} catch(Throwable $e) {
182
+
htmlHandler($e);
183
+
}
184
+
```
185
+
186
+
## Manual handling
187
+
188
+
### Documents
189
+
190
+
Generate context documents with information about the throwable.
191
+
192
+
#### Plain document
193
+
194
+
Use `plainDocument` to create a plain text document.
195
+
196
+
```php
197
+
use function Chevere\ThrowableHandler\plainDocument;
198
+
199
+
$document = plainDocument($e);
200
+
$plain = $document->__toString();
201
+
```
202
+
203
+
Use `Documents\PlainDocument` to create a plain text document by passing its handler.
204
+
205
+
```php
206
+
use Chevere\ThrowableHandler\Documents\PlainDocument;
207
+
208
+
$handler = throwableHandler($throwable);
209
+
$document = new PlainDocument($handler);
210
+
```
211
+
212
+
#### Console document
213
+
214
+
Use `consoleDocument` to create a console document.
215
+
216
+
```php
217
+
use function Chevere\ThrowableHandler\consoleDocument;
218
+
219
+
$document = consoleDocument($e);
220
+
$console = $document->__toString();
221
+
```
222
+
223
+
Use `Documents\ConsoleDocument` to create a console document by passing its handler.
224
+
225
+
```php
226
+
use Chevere\ThrowableHandler\Documents\ConsoleDocument;
227
+
228
+
$handler = throwableHandler($e);
229
+
$document = new ConsoleDocument($handler);
230
+
```
231
+
232
+
#### HTML document
233
+
234
+
Use `htmlDocument` to create an HTML document.
235
+
236
+
```php
237
+
use function Chevere\ThrowableHandler\htmlDocument;
238
+
239
+
$document = htmlDocument($throwable);
240
+
$html = $document->__toString();
241
+
```
242
+
243
+
Use `Documents\HtmlDocument` to create a console document by passing its handler.
244
+
245
+
```php
246
+
use Chevere\ThrowableHandler\Documents\HtmlDocument;
247
+
248
+
$handler = throwableHandler($throwable);
249
+
$document = new HtmlDocument($handler);
250
+
```
251
+
252
+
### Multiple documents
253
+
254
+
Multiple documents can be created **from the same** handler event:
255
+
256
+
```php
257
+
use Chevere\ThrowableHandler\Documents\ConsoleDocument;
258
+
use Chevere\ThrowableHandler\Documents\HtmlDocument;
259
+
use Chevere\ThrowableHandler\Documents\PlainDocument;
260
+
use function Chevere\ThrowableHandler\throwableHandler;
261
+
262
+
$handler = throwableHandler($e);
263
+
$consoleDoc = new ConsoleDocument($handler);
264
+
$plainDoc = new PlainDocument($handler);
265
+
$htmlDoc = new HtmlDocument($handler);
266
+
```
267
+
268
+
### Debug
269
+
270
+
The method `withIsDebug` in `ThrowableHandlerInterface` can be used to toggle debug information on generated documents.
271
+
272
+
```php
273
+
use Chevere\ThrowableHandler\Documents\HtmlDocument;
274
+
use function Chevere\ThrowableHandler\throwableHandler;
275
+
276
+
$handler = throwableHandler($e);
277
+
$docLoud = new HtmlDocument($handler);
278
+
$docSilent = new HtmlDocument(
279
+
$handler->withIsDebug(false)
280
+
);
281
+
```
282
+
283
+
For the code above, `$docLoud` contains debug information (throwable info, file, line, trace and server) while `$docSilent` provides a generic message but referencing to the throwable handled id.
284
+
59
285
## Documentation
60
286
61
287
Documentation is available at [chevere.org](https://chevere.org/packages/throwable-handler).
0 commit comments