{"id":16329278,"url":"https://github.com/josantonius/php-error-handler","last_synced_at":"2026-01-11T16:40:36.058Z","repository":{"id":57001555,"uuid":"76419545","full_name":"josantonius/php-error-handler","owner":"josantonius","description":"PHP library for handling errors","archived":false,"fork":false,"pushed_at":"2022-09-29T15:59:07.000Z","size":1672,"stargazers_count":14,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-01T19:11:41.381Z","etag":null,"topics":["error-handler","error-handling","php","php-error-handler","php-library"],"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":"2016-12-14T03:03:00.000Z","updated_at":"2024-10-08T06:55:40.000Z","dependencies_parsed_at":"2022-08-21T11:40:42.959Z","dependency_job_id":null,"html_url":"https://github.com/josantonius/php-error-handler","commit_stats":null,"previous_names":["josantonius/php-errorhandler"],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-error-handler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-error-handler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-error-handler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/josantonius%2Fphp-error-handler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/josantonius","download_url":"https://codeload.github.com/josantonius/php-error-handler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244246552,"owners_count":20422459,"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":["error-handler","error-handling","php","php-error-handler","php-library"],"created_at":"2024-10-10T23:15:00.169Z","updated_at":"2026-01-11T16:40:36.017Z","avatar_url":"https://github.com/josantonius.png","language":"PHP","readme":"# PHP ErrorHandler library\n\n[![Latest Stable Version](https://poser.pugx.org/josantonius/error-handler/v/stable)](https://packagist.org/packages/josantonius/error-handler)\n[![License](https://poser.pugx.org/josantonius/error-handler/license)](LICENSE)\n[![Total Downloads](https://poser.pugx.org/josantonius/error-handler/downloads)](https://packagist.org/packages/josantonius/error-handler)\n[![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)\n[![CodeCov](https://codecov.io/gh/josantonius/php-error-handler/branch/main/graph/badge.svg)](https://codecov.io/gh/josantonius/php-error-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\nTo handle exceptions you can use the\n[exception-handler](https://github.com/josantonius/php-exception-handler) library.\n\n---\n\n- [Requirements](#requirements)\n- [Installation](#installation)\n- [Available Classes](#available-classes)\n  - [ErrorException Class](#errorexception-class)\n  - [ErrorHandled Class](#errorhandled-class)\n  - [ErrorHandler Class](#errorhandler-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.\n\n## Installation\n\nThe preferred way to install this extension is through [Composer](http://getcomposer.org/download/).\n\nTo install **PHP ErrorHandler library**, simply:\n\n```console\ncomposer require josantonius/error-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/error-handler --prefer-source\n```\n\nYou can also **clone the complete repository** with Git:\n\n```console\ngit clone https://github.com/josantonius/php-error-handler.git\n```\n\n## Available Classes\n\n### ErrorException Class\n\n`Josantonius\\ErrorHandler\\ErrorException` Extends\n[ErrorException](https://www.php.net/manual/en/class.errorexception.php)\n\nGets error file:\n\n```php\npublic function getFile(): string;\n```\n\nGets error level:\n\n```php\npublic function getLevel(): int;\n```\n\nGets error file line:\n\n```php\npublic function getLine(): int;\n```\n\nGets error message:\n\n```php\npublic function getMessage(): string;\n```\n\nGets error name:\n\n```php\npublic function getName(): string;\n```\n\n### ErrorHandled Class\n\n`Josantonius\\ErrorHandler\\ErrorHandled`\n\nGets error file:\n\n```php\npublic function getFile(): string;\n```\n\nGets error level:\n\n```php\npublic function getLevel(): int;\n```\n\nGets error file line:\n\n```php\npublic function getLine(): int;\n```\n\nGets error message:\n\n```php\npublic function getMessage(): string;\n```\n\nGets error name:\n\n```php\npublic function getName(): string;\n```\n\n### ErrorHandler Class\n\n`Josantonius\\ErrorHandler\\ErrorHandler`\n\nConvert errors to exceptions:\n\n```php\n/**\n * The errors will be thrown from the ErrorException instance.\n * \n * @param int[] $errorLevel Define the specific error levels that will become exceptions.\n * \n * @throws WrongErrorLevelException if error level is not valid.\n * \n * @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.\n */\npublic function convertToExceptions(int ...$errorLevel): ErrorHandler;\n```\n\nConvert errors to exceptions except for some of them:\n\n```php\n/**\n * The errors will be thrown from the ErrorException instance.\n * \n * @param int[] $errorLevel Define the specific error levels that will become exceptions.\n * \n * @throws WrongErrorLevelException if error level is not valid.\n * \n * @see https://www.php.net/manual/en/errorfunc.constants.php to view available error levels.\n */\npublic function convertToExceptionsExcept(int ...$errorLevel): ErrorHandler;\n```\n\nRegister error handler function:\n\n```php\n/**\n * The error handler will receive the ErrorHandled object.\n * \n * @see https://www.php.net/manual/en/functions.first_class_callable_syntax.php\n */\npublic function register(callable $callback): ErrorHandler;\n```\n\nUse error reporting to determine which errors are handled:\n\n```php\n/**\n * If the setting value in error_reporting() is used to determine which errors are handled.\n *\n * If this method is not used, all errors will be sent to the handler.\n *\n * @see https://www.php.net/manual/en/function.error-reporting.php\n */\npublic function useErrorReportingLevel(): ErrorHandler;\n```\n\n## Exceptions Used\n\n```php\nuse Josantonius\\ErrorHandler\\Exceptions\\WrongErrorLevelException;\n```\n\n## Usage\n\nExamples of use for this library:\n\n### Convert all errors to exceptions\n\n```php\nuse Josantonius\\ErrorHandler\\ErrorHandler;\n\n$errorHandler = new ErrorHandler();\n\n$errorHandler-\u003econvertToExceptions();\n\n// All errors will be converted to exceptions.\n```\n\n### Convert certain errors to exceptions\n\n```php\nuse Josantonius\\ErrorHandler\\ErrorHandler;\n\n$errorHandler = new ErrorHandler();\n\n$errorHandler-\u003econvertToExceptions(E_USER_ERROR, E_USER_WARNING);\n\n// Only E_USER_ERROR and E_USER_WARNING will be converted to exceptions.\n```\n\n### Convert all errors to exceptions except for some of them\n\n```php\nuse Josantonius\\ErrorHandler\\ErrorHandler;\n\n$errorHandler = new ErrorHandler();\n\n$errorHandler-\u003econvertToExceptionsExcept(E_USER_DEPRECATED, E_USER_NOTICE);\n\n// All errors except E_USER_DEPRECATED and E_USER_NOTICE will be converted to exceptions.\n```\n\n### Convert to exceptions using error reporting level\n\n```php\nuse Josantonius\\ErrorHandler\\ErrorHandler;\n\nerror_reporting(E_USER_ERROR);\n\n$errorHandler = new ErrorHandler();\n\n$errorHandler-\u003econvertToExceptions()-\u003euseErrorReportingLevel();\n\n// Only E_USER_ERROR will be converted to exception.\n```\n\n### Convert to exceptions and use an exception handler\n\n```php\nuse ErrorException;\nuse Josantonius\\ErrorHandler\\ErrorHandler;\n\nset_exception_handler(function (ErrorException $exception) {\n    var_dump([\n        'level'   =\u003e $exception-\u003egetLevel(),\n        'message' =\u003e $exception-\u003egetMessage(),\n        'file'    =\u003e $exception-\u003egetFile(),\n        'line'    =\u003e $exception-\u003egetLine(),\n        'name'    =\u003e $exception-\u003egetName(),\n    ]);\n});\n\n$errorHandler = new ErrorHandler();\n\n$errorHandler-\u003econvertToExceptions();\n\n// All errors will be converted to exceptions.\n```\n\n### Register an error handler function\n\n```php\nuse Josantonius\\ErrorHandler\\ErrorHandled;\nuse Josantonius\\ErrorHandler\\ErrorHandler;\n\nfunction handler(Errorhandled $errorHandled): void {\n    var_dump([\n        'level'   =\u003e $errorHandled-\u003egetLevel(),\n        'message' =\u003e $errorHandled-\u003egetMessage(),\n        'file'    =\u003e $errorHandled-\u003egetFile(),\n        'line'    =\u003e $errorHandled-\u003egetLine(),\n        'name'    =\u003e $errorHandled-\u003egetName(),\n    ]);\n }\n\n$errorHandler = new ErrorHandler();\n\n$errorHandler-\u003eregister(\n    callback: handler(...)\n);\n\n// All errors will be converted to exceptions.\n```\n\n### Register error handler function and convert to exceptions\n\n```php\nuse Josantonius\\ErrorHandler\\ErrorHandled;\nuse Josantonius\\ErrorHandler\\ErrorHandler;\n\nclass Handler {\n    public static function errors(Errorhandled $exception): void { /* do something */ }\n}\n\n$errorHandler = new ErrorHandler();\n\n$errorHandler-\u003eregister(\n    callback: Handler::errors(...)\n)-\u003econvertToExceptions();\n\n// The error will be sent to the error handler and then throw the exception.\n```\n\n### Register error handler function, convert to exceptions and use error reporting level\n\n```php\nerror_reporting(E_USER_ERROR);\n\nclass Handler {\n    public function errors(Errorhandled $exception): void { /* do something */ }\n}\n\n$handler = new Handler();\n\n$errorHandled-\u003eregister(\n    callback: $handler-\u003eerrors(...),\n)-\u003econvertToExceptions()-\u003euseErrorReportingLevel();\n\n// Only E_USER_ERROR will be passed to the handler and converted to exception.\n```\n\n## Tests\n\nTo run [tests](tests) you just need [composer](http://getcomposer.org/download/)\nand to execute the following:\n\n```console\ngit clone https://github.com/josantonius/php-error-handler.git\n```\n\n```console\ncd php-error-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-error-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-error-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 © 2016-present, [Josantonius](https://github.com/josantonius#contact)\n","funding_links":["https://github.com/sponsors/Josantonius"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-error-handler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjosantonius%2Fphp-error-handler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjosantonius%2Fphp-error-handler/lists"}