1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
Jasny Error Handler
===
[](http://travis-ci.org/jasny/error-handler)
[](https://scrutinizer-ci.com/g/jasny/error-handler/?branch=master)
[](https://scrutinizer-ci.com/g/jasny/error-handler/?branch=master)
[](https://insight.sensiolabs.com/projects/e0404b74-7a3e-41c3-a382-6ba12cd63560)
[](https://packagist.org/packages/jasny/error-handler)
[](https://packagist.org/packages/jasny/error-handler)
Error handler with PSR-7 support.
Installation
---
The Jasny Error Handler package is available on [packagist](https://packagist.org/packages/jasny/error-handler).
Install it using composer:
composer require jasny/error-handler
Usage
---
```php
$errorHandler = new Jasny\ErrorHandler();
```
Just creating an error handler will do nothing. You can use it for logging, handling fatal errors and as PSR-7 compatible
middleware.
## Logging
By default the error handler with only catch [Throwables](http://php.net/manual/en/class.throwable.php) and not set the
[php error handler](http://php.net/set_error_handler).
To log errors, set the logger using `setLogger()`. You can log with any [PSR-3 compatible
logger](http://www.php-fig.org/psr/psr-3/) like [Monolog](https://github.com/Seldaek/monolog).
The `logUncaught()` method will set the error handler, so warnings and notices can be logged. It may also [register a
shutdown function](http://php.net/manual/en/function.register-shutdown-function.php) to handle uncatchable fatal
errors.
```php
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
$errorHandler = new Jasny\ErrorHandler();
$log = new Logger('test');
$log->pushHandler(new StreamHandler('path/to/your.log'));
// Log fatal errors, warnings and uncaught exceptions
$errorHandler->setLogger($log);
$errorHandler->logUncaught(E_PARSE | E_ERROR | E_WARNING | E_USER_WARNING);
$errorHandler->logUncaught(Exception::class);
$errorHandler->logUncaught(Error::class); // PHP7 only
```
## PSR-7 compatible middleware
The error handler can be used as PSR-7 compatible (double-pass) middleware.
The error will catch [Exceptions](http://php.net/manual/en/class.exception.php) and
[Errors](http://php.net/manual/en/class.error.php).
You can use this middleware with:
* [Jasny Router](https://github.com/jasny/router)
* [Relay](https://github.com/relayphp/Relay.Relay)
* [Expressive](http://framework.zend.com/expressive)
* [Slim 3](http://www.slimframework.com)
For example use it with **Relay**:
```php
use Relay\RelayBuilder;
use Jasny\HttpMessage\ServerRequest;
use Jasny\HttpMessage\Response;
$errorHandler = new Jasny\ErrorHandler();
$relay = new RelayBuilder();
$dispatcher = $relay->newInstance([$errorHandler->asMiddleware()]);
$response = $dispatcher((new ServerRequest())->withGlobalEnvironment(), new Response());
```
Or with **Jasny Router**:
```php
use Jasny\Router;
use Jasny\Router\Routes\Glob as Routes;
use Jasny\HttpMessage\ServerRequest;
use Jasny\HttpMessage\Response;
$router = new Router(new Routes(['/**' => ['controller' => '$1', 'id' => '$2']));
$errorHandler = new Jasny\ErrorHandler();
$router->add($errorHandler->asMiddleware());
$response = $dispatcher((new ServerRequest())->withGlobalEnvironment(), new Response());
```
### PHP 5 support
With PHP 5 errors aren't thrown, so the middleware won't handle it. To add middleware support for errors in PHP5, you
should call `converErrorsToExceptions()`. This method will convert an error to an
[ErrorException](http://php.net/manual/en/class.errorexception.php).
## Handling fatal errors
Errors that are not thrown, like syntax errors, are not caught and will cause a fatal error. With the `logUncaught()`
method, you can specify that the error handler should also these kind of errors.
With the `onFatalError()` method you take additional action, like output a pretty error message.
```php
ob_start();
$errorHandler = new Jasny\ErrorHandler();
$errorHandler->logUncaught(E_ERROR | E_RECOVERABLE_ERROR | E_USER_ERROR);
$errorHandler->onFatalError(function() {
http_response_code(500);
header('Content-Type: text/html');
echo "<h1>An unexpected error occured</h1><p>The error has been logged.</p>";
}, true);
```
Use `true` as second argument of `onFatalError` to the output buffer before calling your function.
## Combine with other error handlers
Using the error logger might lose backtrace information that other error handlers can pick up. Jasny Error Handler
will always call the previous error handler, including the PHP internal error handler for non-thrown errors.
When using [Rollbar](https://rollbar.com/) you should not use the Rollbar handler for Monolog. By using Rollbar's own error
handler, you'll get better error reports:
```php
use Jasny\ErrorHandler;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
// Rollbar error handler will log uncaught errors
Rollbar::init(array('access_token' => 'POST_SERVER_ITEM_ACCESS_TOKEN'));
$log = new Logger('test');
$log->pushHandler(new RollbarHandler(Rollbar::$instance));
$errorHandler = new ErrorHandler();
// Jasny error handler will only log caught errors
$errorHandler->setLogger($log);
```
|