{"id":16329275,"url":"https://github.com/josantonius/php-exception-handler","last_synced_at":"2025-05-15T08:33:56.433Z","repository":{"id":54465351,"uuid":"521384076","full_name":"josantonius/php-exception-handler","owner":"josantonius","description":"PHP library for handling exceptions","archived":false,"fork":false,"pushed_at":"2022-09-29T16:31:55.000Z","size":38,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-02-17T10:13:20.753Z","etag":null,"topics":["exception-handler","exception-handling","php","php-exception"],"latest_commit_sha":null,"homepage":"https://josantonius.dev","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/josantonius.png","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","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["Josantonius"]}},"created_at":"2022-08-04T19:02:03.000Z","updated_at":"2024-06-07T08:58:20.000Z","dependencies_parsed_at":"2022-08-13T16:30:55.741Z","dependency_job_id":null,"html_url":"https://github.com/josantonius/php-exception-handler","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-exception-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-exception-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-exception-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-exception-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josantonius","download_url":"https://codeload.github.com/josantonius/php-exception-handler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254304781,"owners_count":22048465,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["exception-handler","exception-handling","php","php-exception"],"created_at":"2024-10-10T23:14:58.636Z","updated_at":"2025-05-15T08:33:56.401Z","avatar_url":"https://github.com/josantonius.png","language":"PHP","funding_links":["https://github.com/sponsors/Josantonius"],"categories":[],"sub_categories":[],"readme":"# PHP ExceptionHandler library\n\n[![Latest Stable Version](https://poser.pugx.org/josantonius/exception-handler/v/stable)](https://packagist.org/packages/josantonius/exception-handler)\n[![License](https://poser.pugx.org/josantonius/exception-handler/license)](LICENSE)\n[![Total Downloads](https://poser.pugx.org/josantonius/exception-handler/downloads)](https://packagist.org/packages/josantonius/exception-handler)\n[![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)\n[![CodeCov](https://codecov.io/gh/josantonius/php-exception-handler/branch/main/graph/badge.svg)](https://codecov.io/gh/josantonius/php-exception-handler)\n[![PSR1](https://img.shields.io/badge/PSR-1-f57046.svg)](https://www.php-fig.org/psr/psr-1/)\n[![PSR4](https://img.shields.io/badge/PSR-4-9b59b6.svg)](https://www.php-fig.org/psr/psr-4/)\n[![PSR12](https://img.shields.io/badge/PSR-12-1abc9c.svg)](https://www.php-fig.org/psr/psr-12/)\n\n**Translations**: [Español](.github/lang/es-ES/README.md)\n\nPHP library for handling exceptions.\n\n---\n\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Available Classes](#available-classes)\n  - [ExceptionHandler Class](#exceptionhandler-class)\n- [Exceptions Used](#exceptions-used)\n- [Usage](#usage)\n- [Tests](#tests)\n- [TODO](#todo)\n- [Changelog](#changelog)\n- [Contribution](#contribution)\n- [Sponsor](#sponsor)\n- [License](#license)\n\n---\n\n## Requirements\n\n- Operating System: Linux | Windows.\n\n- PHP versions: 8.1 | 8.2.\n\n## Installation\n\nThe preferred way to install this extension is through [Composer](http://getcomposer.org/download/).\n\nTo install **PHP ExceptionHandler library**, simply:\n\n```console\ncomposer require Josantonius/exception-handler\n```\n\nThe previous command will only install the necessary files,\nif you prefer to **download the entire source code** you can use:\n\n```console\ncomposer require josantonius/exception-handler --prefer-source\n```\n\nYou can also **clone the complete repository** with Git:\n\n```console\ngit clone https://github.com/josantonius/php-exception-handler.git\n```\n\n## Available Classes\n\n### ExceptionHandler Class\n\n`Josantonius\\ExceptionHandler\\ExceptionHandler`\n\nSets a exception handler:\n\n```php\n/**\n * Sets a exception handler.\n *\n * @param callable $callback          Exception handler function.\n * @param string[] $runBeforeCallback Method names to call in the exception before run callback.\n * @param string[] $runAfterCallback  Method names to call in the exception after run callback.\n * \n * @throws NotCallableException     if the callback is not callable.\n * @throws WrongMethodNameException if the method names are not string or are empty.\n * \n * @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php\n */\npublic function __construct(\n    private callable $callback,\n    private array $runBeforeCallback = [],\n    private array $runAfterCallback = []\n);\n```\n\n## Exceptions Used\n\n```php\nuse Josantonius\\ExceptionHandler\\Exceptions\\NotCallableException;\nuse Josantonius\\ExceptionHandler\\Exceptions\\WrongMethodNameException;\n```\n\n## Usage\n\nExamples of use for this library:\n\n### Sets basic exception handler\n\n```php\nuse Josantonius\\ExceptionHandler\\ExceptionHandler;\n\nfunction handler(\\Throwable $exception) { /* do something */ }\n\nnew ExceptionHandler(\n    callback: handler(...)\n);\n\n/**\n * If an exception is thrown, the following is executed:\n *\n * handler($exception)\n */\n```\n\n### Sets methods to execute before calling the callback\n\n```php\nuse Josantonius\\ExceptionHandler\\ExceptionHandler;\n\nclass FooException extends \\Exception\n{\n    public function context(): void { /* do something */ }\n}\n\nclass Handler {\n    public function exceptions(Throwable $exception): void\n    {\n        if ($exception instanceof FooException) {\n            /* do something */\n        }\n    }\n}\n\nnew ExceptionHandler(\n    callback: (new Handler())-\u003eexceptions(...),\n    runBeforeCallback: ['context']\n);\n\n/**\n * If FooException() is thrown, the following is executed:\n * \n * FooException-\u003econtext()\n * Handler-\u003eexceptions($exception)\n */\n```\n\n### Sets methods to execute after calling the callback\n\n```php\nuse Josantonius\\ExceptionHandler\\ExceptionHandler;\n\nclass FooException extends \\Exception\n{\n    public function report(): void { /* do something */ }\n\n    public function render(): void { /* do something */ }\n}\n\nclass Handler {\n    public static function exceptions(Throwable $exception): void\n    {\n        if ($exception instanceof FooException) {\n            /* do something */\n        }\n    }\n}\n\nnew ExceptionHandler(\n    callback: Handler::exceptions(...),\n    runAfterCallback: ['report', 'render']\n);\n\n/**\n * If FooException() is thrown, the following is executed:\n * \n * Handler::exceptions($exception)\n * FooException-\u003ereport()\n * FooException-\u003erender()\n */\n```\n\n### Sets methods to execute before and after calling the callback\n\n```php\nuse Josantonius\\ExceptionHandler\\ExceptionHandler;\n\nclass FooException extends \\Exception\n{\n    public function context(): void { /* do something */ }\n\n    public function report(): void { /* do something */ }\n\n    public function render(): void { /* do something */ }\n}\n\nfunction exceptionHandler(Throwable $exception) { /* do something */ }\n\nnew ExceptionHandler(\n    callback: exceptionHandler(...),\n    runBeforeCallback: ['context', 'logger'],\n    runAfterCallback: ['report', 'render']\n);\n\n/**\n * If FooException() is thrown, the following is executed:\n * \n * FooException-\u003econtext()\n * exceptionHandler($exception)\n * FooException-\u003ereport()\n * FooException-\u003erender()\n * \n * FooException-\u003elogger() is ignored, does not exist in the exception.\n */\n```\n\n## Tests\n\nTo run [tests](tests) you just need [composer](http://getcomposer.org/download/) and to execute the following:\n\n```console\ngit clone https://github.com/josantonius/php-exception-handler.git\n```\n\n```console\ncd php-exception-handler\n```\n\n```console\ncomposer install\n```\n\nRun unit tests with [PHPUnit](https://phpunit.de/):\n\n```console\ncomposer phpunit\n```\n\nRun code standard tests with [PHPCS](https://github.com/squizlabs/PHP_CodeSniffer):\n\n```console\ncomposer phpcs\n```\n\nRun [PHP Mess Detector](https://phpmd.org/) tests to detect inconsistencies in code style:\n\n```console\ncomposer phpmd\n```\n\nRun all previous tests:\n\n```console\ncomposer tests\n```\n\n## TODO\n\n- [ ] Add new feature\n- [ ] Improve tests\n- [ ] Improve documentation\n- [ ] Improve English translation in the README file\n- [ ] Refactor code for disabled code style rules (see phpmd.xml and phpcs.xml)\n\n## Changelog\n\nDetailed changes for each release are documented in the\n[release notes](https://github.com/josantonius/php-exception-handler/releases).\n\n## Contribution\n\nPlease make sure to read the [Contributing Guide](.github/CONTRIBUTING.md), before making a pull\nrequest, start a discussion or report a issue.\n\nThanks to all [contributors](https://github.com/josantonius/php-exception-handler/graphs/contributors)! :heart:\n\n## Sponsor\n\nIf this project helps you to reduce your development time,\n[you can sponsor me](https://github.com/josantonius#sponsor) to support my open source work :blush:\n\n## License\n\nThis repository is licensed under the [MIT License](LICENSE).\n\nCopyright © 2022-present, [Josantonius](https://github.com/josantonius#contact)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-exception-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosantonius%2Fphp-exception-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-exception-handler/lists"}