PHPackages                             josantonius/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. josantonius/error-handler

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

josantonius/error-handler
=========================

PHP library for handling errors.

v2.0.2(3y ago)14434MITPHPPHP ^8.1

Since Dec 14Pushed 3y ago1 watchersCompare

[ Source](https://github.com/josantonius/php-error-handler)[ Packagist](https://packagist.org/packages/josantonius/error-handler)[ GitHub Sponsors](https://github.com/Josantonius)[ RSS](/packages/josantonius-error-handler/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (3)Versions (15)Used By (0)

PHP ErrorHandler library
========================

[](#php-errorhandler-library)

[![Latest Stable Version](https://camo.githubusercontent.com/7bb4cc1aece84652ef121a2ec6c74a16a1c7b530baec8fa11edba560b3f3416d/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f6572726f722d68616e646c65722f762f737461626c65)](https://packagist.org/packages/josantonius/error-handler)[![License](https://camo.githubusercontent.com/086b2900c685f5b03321d5f4fe35c6d9c49cd90c9a4a80be81f88b7560759dd2/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f6572726f722d68616e646c65722f6c6963656e7365)](LICENSE)[![Total Downloads](https://camo.githubusercontent.com/4843d5a60413cf2aff77755c71f4610516078f73444c32d0c8e8ca3fd1ebb680/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f6572726f722d68616e646c65722f646f776e6c6f616473)](https://packagist.org/packages/josantonius/error-handler)[![CI](https://github.com/josantonius/php-error-handler/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/josantonius/php-error-handler/actions/workflows/ci.yml)[![CodeCov](https://camo.githubusercontent.com/c552672a8bc2a79aaed2808307cd6cd861c7d7d958f65f13684e0248457636e3/68747470733a2f2f636f6465636f762e696f2f67682f6a6f73616e746f6e6975732f7068702d6572726f722d68616e646c65722f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/josantonius/php-error-handler)[![PSR1](https://camo.githubusercontent.com/b502a899c9aec217e98971160f816f87346be272cf1a25cfa4793f2ee724bfc8/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d312d6635373034362e737667)](https://www.php-fig.org/psr/psr-1/)[![PSR4](https://camo.githubusercontent.com/d1c090de87e968254a6658528f3bfe9c9dad422d6047fd1323dc211560182c01/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d342d3962353962362e737667)](https://www.php-fig.org/psr/psr-4/)[![PSR12](https://camo.githubusercontent.com/19c529c6dc0656dcc2a16c1a84af450b7bd0dc7b0571b8f17e4fc9f2414f8821/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5053522d31322d3161626339632e737667)](https://www.php-fig.org/psr/psr-12/)

**Translations**: [Español](.github/lang/es-ES/README.md)

PHP library for handling exceptions.

To handle exceptions you can use the [exception-handler](https://github.com/josantonius/php-exception-handler) library.

---

- [Requirements](#requirements)
- [Installation](#installation)
- [Available Classes](#available-classes)
    - [ErrorException Class](#errorexception-class)
    - [ErrorHandled Class](#errorhandled-class)
    - [ErrorHandler Class](#errorhandler-class)
- [Exceptions Used](#exceptions-used)
- [Usage](#usage)
- [Tests](#tests)
- [TODO](#todo)
- [Changelog](#changelog)
- [Contribution](#contribution)
- [Sponsor](#sponsor)
- [License](#license)

---

Requirements
------------

[](#requirements)

- Operating System: Linux | Windows.
- PHP versions: 8.1.

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

[](#installation)

The preferred way to install this extension is through [Composer](http://getcomposer.org/download/).

To install **PHP ErrorHandler library**, simply:

```
composer require josantonius/error-handler
```

The previous command will only install the necessary files, if you prefer to **download the entire source code** you can use:

```
composer require josantonius/error-handler --prefer-source
```

You can also **clone the complete repository** with Git:

```
git clone https://github.com/josantonius/php-error-handler.git
```

Available Classes
-----------------

[](#available-classes)

### ErrorException Class

[](#errorexception-class)

`Josantonius\ErrorHandler\ErrorException` Extends [ErrorException](https://www.php.net/manual/en/class.errorexception.php)

Gets error file:

```
public function getFile(): string;
```

Gets error level:

```
public function getLevel(): int;
```

Gets error file line:

```
public function getLine(): int;
```

Gets error message:

```
public function getMessage(): string;
```

Gets error name:

```
public function getName(): string;
```

### ErrorHandled Class

[](#errorhandled-class)

`Josantonius\ErrorHandler\ErrorHandled`

Gets error file:

```
public function getFile(): string;
```

Gets error level:

```
public function getLevel(): int;
```

Gets error file line:

```
public function getLine(): int;
```

Gets error message:

```
public function getMessage(): string;
```

Gets error name:

```
public function getName(): string;
```

### ErrorHandler Class

[](#errorhandler-class)

`Josantonius\ErrorHandler\ErrorHandler`

Convert errors to exceptions:

```
/**
 * The errors will be thrown from the ErrorException instance.
 *
 * @param int[] $errorLevel Define the specific error levels that will become exceptions.
 *
 * @throws WrongErrorLevelException if error level is not valid.
 *
 * @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
 */
public function convertToExceptions(int ...$errorLevel): ErrorHandler;
```

Convert errors to exceptions except for some of them:

```
/**
 * The errors will be thrown from the ErrorException instance.
 *
 * @param int[] $errorLevel Define the specific error levels that will become exceptions.
 *
 * @throws WrongErrorLevelException if error level is not valid.
 *
 * @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.
 */
public function convertToExceptionsExcept(int ...$errorLevel): ErrorHandler;
```

Register error handler function:

```
/**
 * The error handler will receive the ErrorHandled object.
 *
 * @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php
 */
public function register(callable $callback): ErrorHandler;
```

Use error reporting to determine which errors are handled:

```
/**
 * If the setting value in error_reporting() is used to determine which errors are handled.
 *
 * If this method is not used, all errors will be sent to the handler.
 *
 * @see https://www.php.net/manual/en/function.error-reporting.php
 */
public function useErrorReportingLevel(): ErrorHandler;
```

Exceptions Used
---------------

[](#exceptions-used)

```
use Josantonius\ErrorHandler\Exceptions\WrongErrorLevelException;
```

Usage
-----

[](#usage)

Examples of use for this library:

### Convert all errors to exceptions

[](#convert-all-errors-to-exceptions)

```
use Josantonius\ErrorHandler\ErrorHandler;

$errorHandler = new ErrorHandler();

$errorHandler->convertToExceptions();

// All errors will be converted to exceptions.
```

### Convert certain errors to exceptions

[](#convert-certain-errors-to-exceptions)

```
use Josantonius\ErrorHandler\ErrorHandler;

$errorHandler = new ErrorHandler();

$errorHandler->convertToExceptions(E_USER_ERROR, E_USER_WARNING);

// Only E_USER_ERROR and E_USER_WARNING will be converted to exceptions.
```

### Convert all errors to exceptions except for some of them

[](#convert-all-errors-to-exceptions-except-for-some-of-them)

```
use Josantonius\ErrorHandler\ErrorHandler;

$errorHandler = new ErrorHandler();

$errorHandler->convertToExceptionsExcept(E_USER_DEPRECATED, E_USER_NOTICE);

// All errors except E_USER_DEPRECATED and E_USER_NOTICE will be converted to exceptions.
```

### Convert to exceptions using error reporting level

[](#convert-to-exceptions-using-error-reporting-level)

```
use Josantonius\ErrorHandler\ErrorHandler;

error_reporting(E_USER_ERROR);

$errorHandler = new ErrorHandler();

$errorHandler->convertToExceptions()->useErrorReportingLevel();

// Only E_USER_ERROR will be converted to exception.
```

### Convert to exceptions and use an exception handler

[](#convert-to-exceptions-and-use-an-exception-handler)

```
use ErrorException;
use Josantonius\ErrorHandler\ErrorHandler;

set_exception_handler(function (ErrorException $exception) {
    var_dump([
        'level'   => $exception->getLevel(),
        'message' => $exception->getMessage(),
        'file'    => $exception->getFile(),
        'line'    => $exception->getLine(),
        'name'    => $exception->getName(),
    ]);
});

$errorHandler = new ErrorHandler();

$errorHandler->convertToExceptions();

// All errors will be converted to exceptions.
```

### Register an error handler function

[](#register-an-error-handler-function)

```
use Josantonius\ErrorHandler\ErrorHandled;
use Josantonius\ErrorHandler\ErrorHandler;

function handler(Errorhandled $errorHandled): void {
    var_dump([
        'level'   => $errorHandled->getLevel(),
        'message' => $errorHandled->getMessage(),
        'file'    => $errorHandled->getFile(),
        'line'    => $errorHandled->getLine(),
        'name'    => $errorHandled->getName(),
    ]);
 }

$errorHandler = new ErrorHandler();

$errorHandler->register(
    callback: handler(...)
);

// All errors will be converted to exceptions.
```

### Register error handler function and convert to exceptions

[](#register-error-handler-function-and-convert-to-exceptions)

```
use Josantonius\ErrorHandler\ErrorHandled;
use Josantonius\ErrorHandler\ErrorHandler;

class Handler {
    public static function errors(Errorhandled $exception): void { /* do something */ }
}

$errorHandler = new ErrorHandler();

$errorHandler->register(
    callback: Handler::errors(...)
)->convertToExceptions();

// The error will be sent to the error handler and then throw the exception.
```

### Register error handler function, convert to exceptions and use error reporting level

[](#register-error-handler-function-convert-to-exceptions-and-use-error-reporting-level)

```
error_reporting(E_USER_ERROR);

class Handler {
    public function errors(Errorhandled $exception): void { /* do something */ }
}

$handler = new Handler();

$errorHandled->register(
    callback: $handler->errors(...),
)->convertToExceptions()->useErrorReportingLevel();

// Only E_USER_ERROR will be passed to the handler and converted to exception.
```

Tests
-----

[](#tests)

To run [tests](tests) you just need [composer](http://getcomposer.org/download/)and to execute the following:

```
git clone https://github.com/josantonius/php-error-handler.git
```

```
cd php-error-handler
```

```
composer install
```

Run unit tests with [PHPUnit](https://phpunit.de/):

```
composer phpunit
```

Run code standard tests with [PHPCS](https://github.com/squizlabs/PHP_CodeSniffer):

```
composer phpcs
```

Run [PHP Mess Detector](https://phpmd.org/) tests to detect inconsistencies in code style:

```
composer phpmd
```

Run all previous tests:

```
composer tests
```

TODO
----

[](#todo)

- Add new feature
- Improve tests
- Improve documentation
- Improve English translation in the README file
- Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)

Changelog
---------

[](#changelog)

Detailed changes for each release are documented in the [release notes](https://github.com/josantonius/php-error-handler/releases).

Contribution
------------

[](#contribution)

Please make sure to read the [Contributing Guide](.github/CONTRIBUTING.md), before making a pull request, start a discussion or report a issue.

Thanks to all [contributors](https://github.com/josantonius/php-error-handler/graphs/contributors)! ❤️

Sponsor
-------

[](#sponsor)

If this project helps you to reduce your development time, [you can sponsor me](https://github.com/josantonius#sponsor) to support my open source work 😊

License
-------

[](#license)

This repository is licensed under the [MIT License](LICENSE).

Copyright © 2016-present, [Josantonius](https://github.com/josantonius#contact)

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity79

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 ~176 days

Recently: every ~445 days

Total

13

Last Release

1325d ago

Major Versions

1.1.8 → v2.0.02022-08-10

PHP version history (4 changes)1.0.0PHP &gt;=7.0

1.1.0PHP &gt;=5.6

1.1.2PHP ^5.6 || ^7.0

v2.0.0PHP ^8.1

### Community

Maintainers

![](https://www.gravatar.com/avatar/4b221283501ec8a9cbaefaf27821a91ae8ddd33bddf1fccc6c6815b7ad216ff1?d=identicon)[Josantonius](/maintainers/Josantonius)

---

Top Contributors

[![josantonius](https://avatars.githubusercontent.com/u/18104336?v=4)](https://github.com/josantonius "josantonius (68 commits)")

---

Tags

error-handlererror-handlingphpphp-error-handlerphp-libraryphperrorthrowableerror-handlerthrow

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[filp/whoops

php error handling for cool kids

13.2k402.4M1.4k](/packages/filp-whoops)[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)[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)[kuria/error

Makes handling and debugging PHP errors suck less

1920.0k2](/packages/kuria-error)[thehocinesaad/laravel-error-ai

This package adds Ask AI button to the error page.

2214.4k](/packages/thehocinesaad-laravel-error-ai)

PHPackages © 2026

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