https://github.com/josantonius/php-exception-handler
PHP library for handling exceptions
https://github.com/josantonius/php-exception-handler
exception-handler exception-handling php php-exception
Last synced: about 1 month ago
JSON representation
PHP library for handling exceptions
- Host: GitHub
- URL: https://github.com/josantonius/php-exception-handler
- Owner: josantonius
- License: mit
- Created: 2022-08-04T19:02:03.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2022-09-29T16:31:55.000Z (over 2 years ago)
- Last Synced: 2025-02-17T10:13:20.753Z (4 months ago)
- Topics: exception-handler, exception-handling, php, php-exception
- Language: PHP
- Homepage: https://josantonius.dev
- Size: 37.1 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# PHP ExceptionHandler library
[](https://packagist.org/packages/josantonius/exception-handler)
[](LICENSE)
[](https://packagist.org/packages/josantonius/exception-handler)
[](https://github.com/josantonius/php-exception-handler/actions/workflows/ci.yml)
[](https://codecov.io/gh/josantonius/php-exception-handler)
[](https://www.php-fig.org/psr/psr-1/)
[](https://www.php-fig.org/psr/psr-4/)
[](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
- Operating System: Linux | Windows.
- PHP versions: 8.1 | 8.2.
## Installation
The preferred way to install this extension is through [Composer](http://getcomposer.org/download/).
To install **PHP ExceptionHandler library**, simply:
```console
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:```console
composer require josantonius/exception-handler --prefer-source
```You can also **clone the complete repository** with Git:
```console
git clone https://github.com/josantonius/php-exception-handler.git
```## Available Classes
### ExceptionHandler Class
`Josantonius\ExceptionHandler\ExceptionHandler`
Sets a exception handler:
```php
/**
* 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
```php
use Josantonius\ExceptionHandler\Exceptions\NotCallableException;
use Josantonius\ExceptionHandler\Exceptions\WrongMethodNameException;
```## Usage
Examples of use for this library:
### Sets basic exception handler
```php
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
```php
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
```php
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
```php
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
To run [tests](tests) you just need [composer](http://getcomposer.org/download/) and to execute the following:
```console
git clone https://github.com/josantonius/php-exception-handler.git
``````console
cd php-exception-handler
``````console
composer install
```Run unit tests with [PHPUnit](https://phpunit.de/):
```console
composer phpunit
```Run code standard tests with [PHPCS](https://github.com/squizlabs/PHP_CodeSniffer):
```console
composer phpcs
```Run [PHP Mess Detector](https://phpmd.org/) tests to detect inconsistencies in code style:
```console
composer phpmd
```Run all previous tests:
```console
composer tests
```## 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
Detailed changes for each release are documented in the
[release notes](https://github.com/josantonius/php-exception-handler/releases).## 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)! :heart:
## 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 :blush:## License
This repository is licensed under the [MIT License](LICENSE).
Copyright © 2022-present, [Josantonius](https://github.com/josantonius#contact)