{"id":17109045,"url":"https://github.com/sirn-se/phrity-util-errorhandler","last_synced_at":"2025-04-13T02:32:06.422Z","repository":{"id":57041101,"uuid":"394243091","full_name":"sirn-se/phrity-util-errorhandler","owner":"sirn-se","description":"[php-lib] Inline error handler; catch and resolve errors for code block.","archived":false,"fork":false,"pushed_at":"2024-09-12T06:50:54.000Z","size":19,"stargazers_count":8,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T20:21:20.637Z","etag":null,"topics":["error-handler","php","php-library"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sirn-se.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-08-09T10:13:35.000Z","updated_at":"2025-01-10T14:19:14.000Z","dependencies_parsed_at":"2024-06-18T21:12:43.497Z","dependency_job_id":"3667a064-b684-4514-b4e4-fcd079772322","html_url":"https://github.com/sirn-se/phrity-util-errorhandler","commit_stats":{"total_commits":6,"total_committers":2,"mean_commits":3.0,"dds":"0.16666666666666663","last_synced_commit":"dc9ac8fb70d733c48a9d9d1eb50f7022172da6bc"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirn-se%2Fphrity-util-errorhandler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirn-se%2Fphrity-util-errorhandler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirn-se%2Fphrity-util-errorhandler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sirn-se%2Fphrity-util-errorhandler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sirn-se","download_url":"https://codeload.github.com/sirn-se/phrity-util-errorhandler/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248388153,"owners_count":21095350,"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","php","php-library"],"created_at":"2024-10-14T16:21:55.342Z","updated_at":"2025-04-13T02:32:06.106Z","avatar_url":"https://github.com/sirn-se.png","language":"PHP","readme":"[![Build Status](https://github.com/sirn-se/phrity-util-errorhandler/actions/workflows/acceptance.yml/badge.svg)](https://github.com/sirn-se/phrity-util-errorhandler/actions)\n[![Coverage Status](https://coveralls.io/repos/github/sirn-se/phrity-util-errorhandler/badge.svg?branch=main)](https://coveralls.io/github/sirn-se/phrity-util-errorhandler?branch=main)\n\n# Error Handler utility\n\nThe PHP [error handling](https://www.php.net/manual/en/book.errorfunc.php) can be somewhat of a headache.\nTypically an application uses a system level [error handler](https://www.php.net/manual/en/function.set-error-handler.php) and/or suppressing errors using the `@` prefix.\nBut those cases when your code need to act on triggered errors are more tricky.\n\nThis library provides two convenience methods to handle errors on code blocks, either by throwing exceptions or running callback code when an error occurs.\n\nCurrent version supports PHP `^7.2|^8.0`.\n\n## Installation\n\nInstall with [Composer](https://getcomposer.org/);\n```\ncomposer require phrity/util-errorhandler\n```\n\n## The Error Handler\n\nThe class provides two main methods; `with()` and `withAll()`.\nThe difference is that `with()` will act immediately on an error and abort further code execution, while `withAll()` will attempt to execute the entire code block before acting on errors that occurred.\n\n### Throwing ErrorException\n\n```php\nuse Phrity\\Util\\ErrorHandler;\n\n$handler = new ErrorHandler();\n$result = $handler-\u003ewith(function () {\n    // Code to execute\n    return $success_result;\n});\n$result = $handler-\u003ewithAll(function () {\n    // Code to execute\n    return $success_result;\n});\n```\nThe examples above will run the callback code, but if an error occurs it will throw an [ErrorException](https://www.php.net/manual/en/class.errorexception.php).\nError message and severity will be that of the triggering error.\n* `with()` will throw immediately when occured\n* `withAll()` will throw when code is complete; if more than one error occurred, the first will be thrown\n\n### Throwing specified Throwable\n\n```php\nuse Phrity\\Util\\ErrorHandler;\n\n$handler = new ErrorHandler();\n$result = $handler-\u003ewith(function () {\n    // Code to execute\n    return $success_result;\n}, new RuntimeException('A specified error'));\n$result = $handler-\u003ewithAll(function () {\n    // Code to execute\n    return $success_result;\n}, new RuntimeException('A specified error'));\n```\nThe examples above will run the callback code, but if an error occurs it will throw provided Throwable.\nThe thrown Throwable will have an [ErrorException](https://www.php.net/manual/en/class.errorexception.php) attached as `$previous`.\n* `with()` will throw immediately when occured\n* `withAll()` will throw when code is complete; if more than one error occurred, the first will be thrown\n\n### Using callback\n\n```php\nuse Phrity\\Util\\ErrorHandler;\n\n$handler = new ErrorHandler();\n$result = $handler-\u003ewith(function () {\n    // Code to execute\n    return $success_result;\n}, function (ErrorException $error) {\n    // Code to handle error\n    return $error_result;\n});\n$result = $handler-\u003ewithAll(function () {\n    // Code to execute\n    return $success_result;\n}, function (array $errors, $success_result) {\n    // Code to handle errors\n    return $error_result;\n});\n```\nThe examples above will run the callback code, but if an error occurs it will call the error callback as well.\n* `with()` will run the error callback immediately when error occured; error callback expects an ErrorException instance\n* `withAll()` will run the error callback when code is complete; error callback expects an array of ErrorException and the returned result of code callback\n\n### Filtering error types\n\nBoth `with()` and `withAll()` accepts error level(s) as last parameter.\n```php\nuse Phrity\\Util\\ErrorHandler;\n\n$handler = new ErrorHandler();\n$result = $handler-\u003ewith(function () {\n    // Code to execute\n    return $success_result;\n}, null, E_USER_ERROR);\n$result = $handler-\u003ewithAll(function () {\n    // Code to execute\n    return $success_result;\n}, null, E_USER_ERROR \u0026 E_USER_WARNING);\n```\nAny value or combination of values accepted by [set_error_handler](https://www.php.net/manual/en/function.set-error-handler.php) is usable.\nDefault is `E_ALL`. [List of constants](https://www.php.net/manual/en/errorfunc.constants.php).\n\n### The global error handler\n\nThe class also has global `set()` and `restore()` methods.\n\n```php\nuse Phrity\\Util\\ErrorHandler;\n\n$handler = new ErrorHandler();\n$handler-\u003eset(); // Throws ErrorException on error\n$handler-\u003eset(new RuntimeException('A specified error')); // Throws provided Throwable on error\n$handler-\u003eset(function (ErrorException $error) {\n    // Code to handle errors\n    return $error_result;\n}); // Runs callback on error\n$handler-\u003erestore(); // Restores error handler\n```\n\n###  Class synopsis\n\n```php\nPhrity\\Util\\ErrorHandler {\n\n    /* Methods */\n    public __construct()\n\n    public with(callable $callback, mixed $handling = null, int $levels = E_ALL) : mixed\n    public withAll(callable $callback, mixed $handling = null, int $levels = E_ALL) : mixed\n    public set($handling = null, int $levels = E_ALL) : mixed\n    public restore() : bool\n}\n```\n\n## Versions\n\n| Version | PHP | |\n| --- | --- | --- |\n| `1.1` | `^7.4\\|^8.0` | Some improvements |\n| `1.0` | `^7.2\\|^8.0` | Initial version |\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsirn-se%2Fphrity-util-errorhandler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsirn-se%2Fphrity-util-errorhandler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsirn-se%2Fphrity-util-errorhandler/lists"}