PHPackages                             phrity/util-errorhandler - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. phrity/util-errorhandler

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

phrity/util-errorhandler
========================

Inline error handler; catch and resolve errors for code block.

1.2.2(5mo ago)910.1M—9.1%4MITPHPPHP ^8.1CI passing

Since Aug 9Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/sirn-se/phrity-util-errorhandler)[ Packagist](https://packagist.org/packages/phrity/util-errorhandler)[ Docs](https://phrity.sirn.se/util-errorhandler)[ RSS](/packages/phrity-util-errorhandler/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (7)Dependencies (4)Versions (8)Used By (4)

[![Phrity Util ErrorHandler](docs/logotype.png)](docs/logotype.png)

[![Build Status](https://github.com/sirn-se/phrity-util-errorhandler/actions/workflows/acceptance.yml/badge.svg)](https://github.com/sirn-se/phrity-util-errorhandler/actions)[![Coverage Status](https://camo.githubusercontent.com/23fc038a384789b447f37bb610e1917e5ec9243e1f8295a1a7db5a7626568af6/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f7369726e2d73652f7068726974792d7574696c2d6572726f7268616e646c65722f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/sirn-se/phrity-util-errorhandler?branch=main)

Error Handler utility
=====================

[](#error-handler-utility)

The PHP [error handling](https://www.php.net/manual/en/book.errorfunc.php) can be somewhat of a headache. Typically an application uses a system level [error handler](https://www.php.net/manual/en/function.set-error-handler.php) and/or suppressing errors using the `@` prefix. But those cases when your code need to act on triggered errors are more tricky.

This library provides two convenience methods to handle errors on code blocks, either by throwing exceptions or running callback code when an error occurs.

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

[](#installation)

Install with [Composer](https://getcomposer.org/);

```
composer require phrity/util-errorhandler

```

The Error Handler
-----------------

[](#the-error-handler)

The class provides two main methods; `with()` and `withAll()`. The difference is that `with()` will act immediately on an error and abort further code execution, while `withAll()` will attempt to execute the entire code block before acting on errors that occurred.

### Throwing ErrorException

[](#throwing-errorexception)

```
use Phrity\Util\ErrorHandler;

$handler = new ErrorHandler();
$result = $handler->with(function () {
    // Code to execute
    return $success_result;
});
$result = $handler->withAll(function () {
    // Code to execute
    return $success_result;
});
```

The examples above will run the callback code, but if an error occurs it will throw an [ErrorException](https://www.php.net/manual/en/class.errorexception.php). Error message and severity will be that of the triggering error.

- `with()` will throw immediately when occured
- `withAll()` will throw when code is complete; if more than one error occurred, the first will be thrown

### Throwing specified Throwable

[](#throwing-specified-throwable)

```
use Phrity\Util\ErrorHandler;

$handler = new ErrorHandler();
$result = $handler->with(function () {
    // Code to execute
    return $success_result;
}, new RuntimeException('A specified error'));
$result = $handler->withAll(function () {
    // Code to execute
    return $success_result;
}, new RuntimeException('A specified error'));
```

The examples above will run the callback code, but if an error occurs it will throw provided Throwable. The thrown Throwable will have an [ErrorException](https://www.php.net/manual/en/class.errorexception.php) attached as `$previous`.

- `with()` will throw immediately when occured
- `withAll()` will throw when code is complete; if more than one error occurred, the first will be thrown

### Using callback

[](#using-callback)

```
use Phrity\Util\ErrorHandler;

$handler = new ErrorHandler();
$result = $handler->with(function () {
    // Code to execute
    return $success_result;
}, function (ErrorException $error) {
    // Code to handle error
    return $error_result;
});
$result = $handler->withAll(function () {
    // Code to execute
    return $success_result;
}, function (array $errors, $success_result) {
    // Code to handle errors
    return $error_result;
});
```

The examples above will run the callback code, but if an error occurs it will call the error callback as well.

- `with()` will run the error callback immediately when error occured; error callback expects an ErrorException instance
- `withAll()` will run the error callback when code is complete; error callback expects an array of ErrorException and the returned result of code callback

### Filtering error types

[](#filtering-error-types)

Both `with()` and `withAll()` accepts error level(s) as last parameter.

```
use Phrity\Util\ErrorHandler;

$handler = new ErrorHandler();
$result = $handler->with(function () {
    // Code to execute
    return $success_result;
}, null, E_USER_ERROR);
$result = $handler->withAll(function () {
    // Code to execute
    return $success_result;
}, null, E_USER_ERROR & E_USER_WARNING);
```

Any value or combination of values accepted by [set\_error\_handler](https://www.php.net/manual/en/function.set-error-handler.php) is usable. Default is `E_ALL`. [List of constants](https://www.php.net/manual/en/errorfunc.constants.php).

### The global error handler

[](#the-global-error-handler)

The class also has global `set()` and `restore()` methods.

```
use Phrity\Util\ErrorHandler;

$handler = new ErrorHandler();
$handler->set(); // Throws ErrorException on error
$handler->set(new RuntimeException('A specified error')); // Throws provided Throwable on error
$handler->set(function (ErrorException $error) {
    // Code to handle errors
    return $error_result;
}); // Runs callback on error
$handler->restore(); // Restores error handler
```

### Class synopsis

[](#class-synopsis)

```
Phrity\Util\ErrorHandler {

    /* Methods */
    public __construct()

    public with(callable $callback, mixed $handling = null, int $levels = E_ALL) : mixed
    public withAll(callable $callback, mixed $handling = null, int $levels = E_ALL) : mixed
    public set($handling = null, int $levels = E_ALL) : mixed
    public restore() : bool
}
```

Versions
--------

[](#versions)

VersionPHP`1.2``^8.1`Some improvements`1.1``^7.4|^8.0`Some improvements`1.0``^7.2|^8.0`Initial version

###  Health Score

55

—

FairBetter than 98% of packages

Maintenance71

Regular maintenance activity

Popularity51

Moderate usage in the ecosystem

Community13

Small or concentrated contributor base

Maturity65

Established project with proven stability

 Bus Factor1

Top contributor holds 100% 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 ~263 days

Recently: every ~160 days

Total

7

Last Release

164d ago

PHP version history (3 changes)1.0.0PHP ^7.2|^8.0

1.1.0PHP ^7.4 | ^8.0

1.2.0PHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4255391?v=4)[Sören Jensen](/maintainers/sirn-se)[@sirn-se](https://github.com/sirn-se)

---

Top Contributors

[![sirn-se](https://avatars.githubusercontent.com/u/4255391?v=4)](https://github.com/sirn-se "sirn-se (24 commits)")

---

Tags

error-handlerphpphp-libraryerrorwarning

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phrity-util-errorhandler/health.svg)

```
[![Health](https://phpackages.com/badges/phrity-util-errorhandler/health.svg)](https://phpackages.com/packages/phrity-util-errorhandler)
```

###  Alternatives

[liliuwei/thinkphp-jump

适用于thinkphp6.0的跳转扩展

2874.4k1](/packages/liliuwei-thinkphp-jump)[dstuecken/php7ify

php7ify is a project that brings new php7 classes and exceptions to php 5.x.

11157.7k1](/packages/dstuecken-php7ify)[bizley/cookiemonster

Yii extension to manage cookie warning.

2020.1k1](/packages/bizley-cookiemonster)[phpattempt/phpattempt

Helper function to enforce error-first approach in your code

1710.4k](/packages/phpattempt-phpattempt)

PHPackages © 2026

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