Skip to content

Commit a6289c9

Browse files
committed
update docs
1 parent ec32379 commit a6289c9

1 file changed

Lines changed: 231 additions & 5 deletions

File tree

README.md

Lines changed: 231 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,23 @@
1818

1919
![ThrowableHandler](.github/banner/throwable-handler-logo.svg)
2020

21-
## Quick start
21+
## Summary
22+
23+
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
2228

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).
2430

2531
```sh
2632
composer require chevere/throwable-handler
2733
```
2834

29-
Register ThrowableHandler to handle all errors.
35+
## Quick start
36+
37+
For quick catch-all use, first register ThrowableHandler to handle all errors.
3038

3139
```php
3240
use Chevere\ThrowableHandler\ThrowableHandler;
@@ -35,7 +43,7 @@ set_error_handler(ThrowableHandler::ERROR_AS_EXCEPTION);
3543
register_shutdown_function(ThrowableHandler::SHUTDOWN_ERROR_AS_EXCEPTION);
3644
```
3745

38-
Register your exception handler, you can choose:
46+
Then register your exception handler, you can choose:
3947

4048
* `ThrowableHandler::PLAIN`
4149
* `ThrowableHandler::CONSOLE`
@@ -47,6 +55,8 @@ use Chevere\ThrowableHandler\ThrowableHandler;
4755
set_exception_handler(ThrowableHandler::PLAIN);
4856
```
4957

58+
Keep reading the documentation for more advanced usage and configuration options.
59+
5060
## Demo
5161

5262
![HTML demo](demo/demo.svg)
@@ -56,13 +66,229 @@ set_exception_handler(ThrowableHandler::PLAIN);
5666
* [Plain text](https://chevere.github.io/throwable-handler/demo/output/plain.txt)
5767
* [Console (asciinema)](https://asciinema.org/a/491732)
5868

69+
## Features
70+
71+
* Multiple use modes (auto, triggered, manual)
72+
* Supports nested throwables (previous: `$e`)
73+
* Console document
74+
* Colorful console output (where available)
75+
* Plain document
76+
* Same as console (no-color)
77+
* Same as copy HTML text
78+
* HTML document
79+
* Responsive design (narrow devices)
80+
* Silent mode for end-user
81+
82+
## Errors as exceptions
83+
84+
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.
89+
90+
```php
91+
use Chevere\ThrowableHandler\ThrowableHandler;
92+
93+
set_error_handler(ThrowableHandler::ERROR_AS_EXCEPTION);
94+
```
95+
96+
### shutdownErrorAsException
97+
98+
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.
99+
100+
```php
101+
use Chevere\ThrowableHandler\ThrowableHandler;
102+
103+
register_shutdown_function(ThrowableHandler::SHUTDOWN_ERROR_AS_EXCEPTION);
104+
```
105+
106+
## Automatic handling
107+
108+
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+
59285
## Documentation
60286

61287
Documentation is available at [chevere.org](https://chevere.org/packages/throwable-handler).
62288

63289
## License
64290

65-
Copyright 2024 [Rodolfo Berrios A.](https://rodolfoberrios.com/)
291+
Copyright [Rodolfo Berrios A.](https://rodolfoberrios.com/)
66292

67293
Chevere is licensed under the Apache License, Version 2.0. See [LICENSE](LICENSE) for the full license text.
68294

0 commit comments

Comments
 (0)