PHPackages                             jasny/error-handler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. jasny/error-handler

AbandonedArchivedLibrary[Debugging &amp; Profiling](/categories/debugging)

jasny/error-handler
===================

Error handler with PSR-7 support

v0.2.0(9y ago)710.6k1[3 issues](https://github.com/jasny/error-handler/issues)2MITPHPPHP &gt;=5.6.0

Since Oct 27Pushed 9y ago1 watchersCompare

[ Source](https://github.com/jasny/error-handler)[ Packagist](https://packagist.org/packages/jasny/error-handler)[ RSS](/packages/jasny-error-handler/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (3)Dependencies (4)Versions (6)Used By (2)

Jasny Error Handler
===================

[](#jasny-error-handler)

[![Build Status](https://camo.githubusercontent.com/65d1a4c70566dcef77dde28190ddf3907519d7193b00381b1054b428cf3732cf/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f6a61736e792f6572726f722d68616e646c65722e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/jasny/error-handler)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/8ef81268605b8743e8097960b522ac7413ef93594ee5bae91e08096e6aa7c4af/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f6572726f722d68616e646c65722f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/error-handler/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/9376113179e88fcbcab553749f99d1c5c7598afaae1514196db9dbdc774cc799/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6a61736e792f6572726f722d68616e646c65722f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/jasny/error-handler/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/3c70ebcb3ce5d2d9d27b3b2c4af62524e833ba3e5b3222a64ec5fa054750ee91/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f65303430346237342d376133652d343163332d613338322d3662613132636436333536302f6d696e692e706e67)](https://insight.sensiolabs.com/projects/e0404b74-7a3e-41c3-a382-6ba12cd63560)[![Packagist Stable Version](https://camo.githubusercontent.com/ee33ed3215516b19165bd623b92931b1f7ac6e3c6e97df311923804ef58b1054/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a61736e792f6572726f722d68616e646c65722e737667)](https://packagist.org/packages/jasny/error-handler)[![Packagist License](https://camo.githubusercontent.com/df543ecbe2eadb14c57f1e1c2e64bb458f63fd2751e75a3741b2c8fc67ddd6a0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a61736e792f6572726f722d68616e646c65722e737667)](https://packagist.org/packages/jasny/error-handler)

Error handler with PSR-7 support.

Installation
------------

[](#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
-----

[](#usage)

```
$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
-------

[](#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.

```
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
---------------------------

[](#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**:

```
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**:

```
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

[](#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
---------------------

[](#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.

```
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 "An unexpected error occuredThe error has been logged.";
}, true);
```

Use `true` as second argument of `onFatalError` to the output buffer before calling your function.

Combine with other error handlers
---------------------------------

[](#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:

```
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);
```

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance11

Infrequent updates — may be unmaintained

Popularity28

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 96.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~32 days

Total

3

Last Release

3401d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3379a93d51305df325df9045e1a8b205d195e4e8c01312dff53a000ee79002eb?d=identicon)[jasny](/maintainers/jasny)

---

Top Contributors

[![jasny](https://avatars.githubusercontent.com/u/100821?v=4)](https://github.com/jasny "jasny (26 commits)")[![scrutinizer-auto-fixer](https://avatars.githubusercontent.com/u/6253494?v=4)](https://github.com/scrutinizer-auto-fixer "scrutinizer-auto-fixer (1 commits)")

---

Tags

middlewareerror-handlerexception handler

### Embed Badge

![Health badge](/badges/jasny-error-handler/health.svg)

```
[![Health](https://phpackages.com/badges/jasny-error-handler/health.svg)](https://phpackages.com/packages/jasny-error-handler)
```

###  Alternatives

[php-console/php-console

PHP library for Google Chrome extension "PHP Console".

1.3k2.9M41](/packages/php-console-php-console)[recca0120/laravel-tracy

A Laravel Package to integrate Nette Tracy Debugger

388283.0k3](/packages/recca0120-laravel-tracy)[phpro/http-tools

HTTP tools for developing more consistent HTTP implementations.

28137.8k](/packages/phpro-http-tools)[yiisoft/yii-middleware

Yii Middleware

21151.3k1](/packages/yiisoft-yii-middleware)[kuria/error

Makes handling and debugging PHP errors suck less

1920.0k2](/packages/kuria-error)[php-console/laravel-service-provider

Laravel service provider to handle PHP errors, dump variables, execute PHP code remotely in Google Chrome

7361.2k1](/packages/php-console-laravel-service-provider)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
