|
1 |
| -# BitFrame\ErrorHandler\Whoops |
| 1 | +# BitFrame\Whoops |
| 2 | + |
| 3 | +[](https://codecov.io/gh/designcise/bitframe-whoops) |
| 4 | +[](https://travis-ci.org/designcise/bitframe-whoops) |
2 | 5 |
|
3 | 6 | Whoops error handler middleware to handle application or middleware specific errors.
|
4 | 7 |
|
5 |
| -### Installation |
| 8 | +## Installation |
| 9 | + |
| 10 | +``` |
| 11 | +$ composer require "designcise/bitframe-whoops" |
| 12 | +``` |
| 13 | + |
| 14 | +Please note that this package requires PHP 7.4.0 or newer. |
| 15 | + |
| 16 | +## Quickstart |
| 17 | + |
| 18 | +### Instantiating |
| 19 | + |
| 20 | +The constructor has the following signature: |
| 21 | + |
| 22 | +```php |
| 23 | +new ErrorHandler( |
| 24 | + \Psr\Http\Message\ResponseFactoryInterface, |
| 25 | + \BitFrame\Whoops\Provider\HandlerProviderNegotiator::class |
| 26 | + [options] |
| 27 | +); |
| 28 | +``` |
| 29 | + |
| 30 | +1. The first argument to the constructor must be an instance of `Psr\Http\Message\ResponseFactoryInterface`; |
| 31 | +1. The second argument to the constructor must be the name of the handler provider class (which extends `\BitFrame\Whoops\Provider\AbstractProvider`) |
| 32 | +1. The third argument to the constructor is an optional array of options to specify the following: |
| 33 | + 1. `catchGlobalErrors`: When set to `true` errors will be handled outside of current batch of middleware set. |
| 34 | + 1. Other options are simply method names in `Whoops\Handler\*Handler.php` and `BitFrame\Whoops\Handler\*Handler.php`. For example, to set `Whoops\Handler\JsonResponseHandler::setJsonApi()` you would pass in: `['setJsonApi' => false]`, etc. |
| 35 | + |
| 36 | +As a shortcut, you can also use the static method `ErrorHandler::fromNegotiator($factory, $options)`. This would use the `\BitFrame\Whoops\Provider\HandlerProviderNegotiator` by default. |
6 | 37 |
|
7 |
| -See [installation docs](https://www.bitframephp.com/middleware/error-handler/whoops) for instructions on installing and using this middleware. |
| 38 | +### How to Run the Middleware |
8 | 39 |
|
9 |
| -### Usage Example |
| 40 | +To run the middleware, simply pass in a `BitFrame\Whoops\ErrorHandler` instance to your middleware runner / dispatcher. |
10 | 41 |
|
| 42 | +For example, to handle middleware-specific errors with `BitFrame\App` (or other PSR-15 dispatchers) it would look something like this: |
| 43 | + |
| 44 | +```php |
| 45 | +use BitFrame\App; |
| 46 | +use BitFrame\Emitter\SapiEmitter; |
| 47 | +use BitFrame\Whoops\ErrorHandler; |
| 48 | +use \BitFrame\Whoops\Provider\HandlerProviderNegotiator; |
| 49 | +use BitFrame\Factory\HttpFactory; |
| 50 | + |
| 51 | +$app = new App(); |
| 52 | + |
| 53 | +$middleware = function () { |
| 54 | + throw new \Exception('hello world!'); |
| 55 | +}; |
| 56 | + |
| 57 | +$app->use([ |
| 58 | + SapiEmitter::class, |
| 59 | + new ErrorHandler(HttpFactory::getFactory(), HandlerProviderNegotiator::class, [ |
| 60 | + 'addTraceToOutput' => true, |
| 61 | + 'setJsonApi' => false, |
| 62 | + ]), |
| 63 | + $middleware, |
| 64 | +]); |
| 65 | + |
| 66 | +$app->run(); |
11 | 67 | ```
|
12 |
| -use \BitFrame\ErrorHandler\WhoopsErrorHandler; |
13 | 68 |
|
14 |
| -require 'vendor/autoload.php'; |
| 69 | +To handle global errors with `BitFrame\App` (or other PSR-15 dispatchers) it would look something like this: |
15 | 70 |
|
16 |
| -$app = new \BitFrame\Application; |
| 71 | +```php |
| 72 | +use BitFrame\App; |
| 73 | +use BitFrame\Whoops\ErrorHandler; |
| 74 | +use BitFrame\Factory\HttpFactory; |
17 | 75 |
|
18 |
| -$format = 'auto'; |
19 |
| -$handler = new WhoopsErrorHandler($format); |
| 76 | +$app = new App(); |
20 | 77 |
|
21 | 78 | $app->run([
|
22 |
| - /* In order to output response from the whoops error handler, |
23 |
| - * make sure you include a response emitter middleware, for example: |
24 |
| - * \BitFrame\Message\DiactorosResponseEmitter::class, */ |
25 |
| - $handler |
| 79 | + ErrorHandler::fromNegotiator(HttpFactory::getFactory(), [ |
| 80 | + 'catchGlobalErrors' => true, |
| 81 | + 'addTraceToOutput' => true, |
| 82 | + 'setJsonApi' => false, |
| 83 | + ]), |
26 | 84 | ]);
|
| 85 | + |
| 86 | +throw new \Exception('hello world!'); |
27 | 87 | ```
|
28 | 88 |
|
29 |
| -### Tests |
| 89 | +### How Does It Work? |
| 90 | + |
| 91 | +The error handler middleware automatically determines the error handler to use based on the `Accept` header. The following error handler provders are included: |
| 92 | + |
| 93 | +1. `BitFrame\Whoops\Provider\HtmlHandlerProvider` for `Whoops\Handler\PrettyPageHandler`; |
| 94 | +1. `BitFrame\Whoops\Provider\JsonHandlerProvider` for `Whoops\Handler\JsonResponseHandler`; |
| 95 | +1. `BitFrame\Whoops\Provider\JsonpHandlerProvider` for `BitFrame\Whoops\Handler\JsonpResponseHandler`; |
| 96 | +1. `BitFrame\Whoops\Provider\TextHandlerProvider` for `Whoops\Handler\PlainTextHandler`; |
| 97 | +1. `BitFrame\Whoops\Provider\XmlHandlerProvider` for `Whoops\Handler\XmlResponseHandler`; |
30 | 98 |
|
31 |
| -To execute the test suite, you will need [PHPUnit](https://phpunit.de/). |
| 99 | +## Tests |
32 | 100 |
|
33 |
| -### Contributing |
| 101 | +To run the tests you can use the following commands: |
| 102 | + |
| 103 | +| Command | Type | |
| 104 | +| ---------------- |:---------------:| |
| 105 | +| `composer test` | PHPUnit tests | |
| 106 | +| `composer style` | CodeSniffer | |
| 107 | +| `composer md` | MessDetector | |
| 108 | +| `composer check` | PHPStan | |
| 109 | + |
| 110 | +## Contributing |
34 | 111 |
|
35 | 112 | * File issues at https://github.com/designcise/bitframe-whoops/issues
|
36 | 113 | * Issue patches to https://github.com/designcise/bitframe-whoops/pulls
|
37 | 114 |
|
38 |
| -### Documentation |
39 |
| - |
40 |
| -Documentation is available at: |
| 115 | +## Documentation |
41 | 116 |
|
42 |
| -* https://www.bitframephp.com/middleware/error-handler/whoops/ |
| 117 | +Complete documentation for v2.0 will be available soon. |
43 | 118 |
|
44 |
| -### License |
| 119 | +## License |
45 | 120 |
|
46 | 121 | Please see [License File](LICENSE.md) for licensing information.
|
0 commit comments