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

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

josantonius/exception-handler
=============================

PHP library for handling exceptions.

v1.0.3(3y ago)213MITPHPPHP ^8.1

Since Aug 4Pushed 3y ago1 watchersCompare

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

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

PHP ExceptionHandler library
============================

[](#php-exceptionhandler-library)

[![Latest Stable Version](https://camo.githubusercontent.com/976505ecfb4d8d2cf47ac8ff0606f49de91bb38787a3af31d80f4de4c1f0adbc/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f657863657074696f6e2d68616e646c65722f762f737461626c65)](https://packagist.org/packages/josantonius/exception-handler)[![License](https://camo.githubusercontent.com/d754c88b9aa454b903ebda3fc919295e11ca72fc1c4d67e305cd6eeebff731db/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f657863657074696f6e2d68616e646c65722f6c6963656e7365)](LICENSE)[![Total Downloads](https://camo.githubusercontent.com/37cd9528f8a9b5dd70229f2475428ed14788eb6741c0295cc6bda888bf071e83/68747470733a2f2f706f7365722e707567782e6f72672f6a6f73616e746f6e6975732f657863657074696f6e2d68616e646c65722f646f776e6c6f616473)](https://packagist.org/packages/josantonius/exception-handler)[![CI](https://github.com/josantonius/php-exception-handler/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/josantonius/php-exception-handler/actions/workflows/ci.yml)[![CodeCov](https://camo.githubusercontent.com/aa4e017d3cbda0bdc156ed33bebde12714c1c89ad6796641154f51165915a145/68747470733a2f2f636f6465636f762e696f2f67682f6a6f73616e746f6e6975732f7068702d657863657074696f6e2d68616e646c65722f6272616e63682f6d61696e2f67726170682f62616467652e737667)](https://codecov.io/gh/josantonius/php-exception-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.

---

- [Requirements](#requirements)
- [Installation](#installation)
- [Available Classes](#available-classes)
    - [ExceptionHandler Class](#exceptionhandler-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 | 8.2.

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

[](#installation)

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

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

```
composer require Josantonius/exception-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/exception-handler --prefer-source
```

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

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

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

[](#available-classes)

### ExceptionHandler Class

[](#exceptionhandler-class)

`Josantonius\ExceptionHandler\ExceptionHandler`

Sets a exception handler:

```
/**
 * Sets a exception handler.
 *
 * @param callable $callback          Exception handler function.
 * @param string[] $runBeforeCallback Method names to call in the exception before run callback.
 * @param string[] $runAfterCallback  Method names to call in the exception after run callback.
 *
 * @throws NotCallableException     if the callback is not callable.
 * @throws WrongMethodNameException if the method names are not string or are empty.
 *
 * @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php
 */
public function __construct(
    private callable $callback,
    private array $runBeforeCallback = [],
    private array $runAfterCallback = []
);
```

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

[](#exceptions-used)

```
use Josantonius\ExceptionHandler\Exceptions\NotCallableException;
use Josantonius\ExceptionHandler\Exceptions\WrongMethodNameException;
```

Usage
-----

[](#usage)

Examples of use for this library:

### Sets basic exception handler

[](#sets-basic-exception-handler)

```
use Josantonius\ExceptionHandler\ExceptionHandler;

function handler(\Throwable $exception) { /* do something */ }

new ExceptionHandler(
    callback: handler(...)
);

/**
 * If an exception is thrown, the following is executed:
 *
 * handler($exception)
 */
```

### Sets methods to execute before calling the callback

[](#sets-methods-to-execute-before-calling-the-callback)

```
use Josantonius\ExceptionHandler\ExceptionHandler;

class FooException extends \Exception
{
    public function context(): void { /* do something */ }
}

class Handler {
    public function exceptions(Throwable $exception): void
    {
        if ($exception instanceof FooException) {
            /* do something */
        }
    }
}

new ExceptionHandler(
    callback: (new Handler())->exceptions(...),
    runBeforeCallback: ['context']
);

/**
 * If FooException() is thrown, the following is executed:
 *
 * FooException->context()
 * Handler->exceptions($exception)
 */
```

### Sets methods to execute after calling the callback

[](#sets-methods-to-execute-after-calling-the-callback)

```
use Josantonius\ExceptionHandler\ExceptionHandler;

class FooException extends \Exception
{
    public function report(): void { /* do something */ }

    public function render(): void { /* do something */ }
}

class Handler {
    public static function exceptions(Throwable $exception): void
    {
        if ($exception instanceof FooException) {
            /* do something */
        }
    }
}

new ExceptionHandler(
    callback: Handler::exceptions(...),
    runAfterCallback: ['report', 'render']
);

/**
 * If FooException() is thrown, the following is executed:
 *
 * Handler::exceptions($exception)
 * FooException->report()
 * FooException->render()
 */
```

### Sets methods to execute before and after calling the callback

[](#sets-methods-to-execute-before-and-after-calling-the-callback)

```
use Josantonius\ExceptionHandler\ExceptionHandler;

class FooException extends \Exception
{
    public function context(): void { /* do something */ }

    public function report(): void { /* do something */ }

    public function render(): void { /* do something */ }
}

function exceptionHandler(Throwable $exception) { /* do something */ }

new ExceptionHandler(
    callback: exceptionHandler(...),
    runBeforeCallback: ['context', 'logger'],
    runAfterCallback: ['report', 'render']
);

/**
 * If FooException() is thrown, the following is executed:
 *
 * FooException->context()
 * exceptionHandler($exception)
 * FooException->report()
 * FooException->render()
 *
 * FooException->logger() is ignored, does not exist in the 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-exception-handler.git
```

```
cd php-exception-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-exception-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-exception-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 © 2022-present, [Josantonius](https://github.com/josantonius#contact)

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity60

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

Total

4

Last Release

1323d ago

### 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 (25 commits)")

---

Tags

exception-handlerexception-handlingphpphp-exceptionphpexceptionthrowableexception handlerthrow

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)[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)
