|
| 1 | +[](https://github.com/sirn-se/phrity-util-errorhandler/actions) |
| 2 | +[](https://coveralls.io/github/sirn-se/phrity-util-errorhandler?branch=main) |
| 3 | + |
| 4 | +# Error Handler utility |
| 5 | + |
| 6 | +The PHP [error handling](https://www.php.net/manual/en/book.errorfunc.php) can be somewhat of a headache. |
| 7 | +Typically 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. |
| 8 | +But those cases when your code need to act on triggered errors are more tricky. |
| 9 | + |
| 10 | +This library provides tow convenience methods to handle errors on code blocks, either by throwing exceptions or running callback code when an error occurs. |
| 11 | + |
| 12 | +Current version supports PHP `^7.2|^8.0`. |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +Install with [Composer](https://getcomposer.org/); |
| 17 | +``` |
| 18 | +composer require phrity/util-errorhandler |
| 19 | +``` |
| 20 | + |
| 21 | +## The Error Handler |
| 22 | + |
| 23 | +The class provides two main methods; `with()` and `withAll()`. |
| 24 | +The 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. |
| 25 | + |
| 26 | +### Throwing ErrorException |
| 27 | + |
| 28 | +```php |
| 29 | +use Phrity\Util\ErrorHandler; |
| 30 | + |
| 31 | +$handler = new ErrorHandler(); |
| 32 | +$result = $handler->with(function () { |
| 33 | + // Code to execute |
| 34 | + return $success_result; |
| 35 | +}); |
| 36 | +$result = $handler->withAll(function () { |
| 37 | + // Code to execute |
| 38 | + return $success_result; |
| 39 | +}); |
| 40 | +``` |
| 41 | +The 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). |
| 42 | +Error message and severity will be that of the triggering error. |
| 43 | +* `with()` will throw immediately when occured |
| 44 | +* `withAll()` will throw when code is complete; if more than one error occurred, the first will be thrown |
| 45 | + |
| 46 | +### Throwing specified Throwable |
| 47 | + |
| 48 | +```php |
| 49 | +use Phrity\Util\ErrorHandler; |
| 50 | + |
| 51 | +$handler = new ErrorHandler(); |
| 52 | +$result = $handler->with(function () { |
| 53 | + // Code to execute |
| 54 | + return $success_result; |
| 55 | +}, new RuntimeException('A specified error')); |
| 56 | +$result = $handler->withAll(function () { |
| 57 | + // Code to execute |
| 58 | + return $success_result; |
| 59 | +}, new RuntimeException('A specified error')); |
| 60 | +``` |
| 61 | +The examples above will run the callback code, but if an error occurs it will throw provided Throwable. |
| 62 | +The thrown Throwable will have an [ErrorException](https://www.php.net/manual/en/class.errorexception.php) attached as `$previous`. |
| 63 | +* `with()` will throw immediately when occured |
| 64 | +* `withAll()` will throw when code is complete; if more than one error occurred, the first will be thrown |
| 65 | + |
| 66 | +### Using callback |
| 67 | + |
| 68 | +```php |
| 69 | +use Phrity\Util\ErrorHandler; |
| 70 | + |
| 71 | +$handler = new ErrorHandler(); |
| 72 | +$result = $handler->with(function () { |
| 73 | + // Code to execute |
| 74 | + return $success_result; |
| 75 | +}, function (ErrorException $error) { |
| 76 | + // Code to handle error |
| 77 | + return $error_result; |
| 78 | +}); |
| 79 | +$result = $handler->withAll(function () { |
| 80 | + // Code to execute |
| 81 | + return $success_result; |
| 82 | +}, function (array $errors, $success_result) { |
| 83 | + // Code to handle errors |
| 84 | + return $error_result; |
| 85 | +}); |
| 86 | +``` |
| 87 | +The examples above will run the callback code, but if an error occurs it will call the error callback as well. |
| 88 | +* `with()` will run the error callback immediately when occured; error callback expects an ErrorException instance |
| 89 | +* `withAll()` will run the error callback when code is complete; error callback expects an array of ErrorException and the returened result of code callback |
| 90 | + |
| 91 | +### Filtering error types |
| 92 | + |
| 93 | +Both `with()` and `withAll()` accepts error level(s) as last parameter. |
| 94 | +```php |
| 95 | +use Phrity\Util\ErrorHandler; |
| 96 | + |
| 97 | +$handler = new ErrorHandler(); |
| 98 | +$result = $handler->with(function () { |
| 99 | + // Code to execute |
| 100 | + return $success_result; |
| 101 | +}, null, E_USER_ERROR); |
| 102 | +$result = $handler->withAll(function () { |
| 103 | + // Code to execute |
| 104 | + return $success_result; |
| 105 | +}, null, E_USER_ERROR & E_USER_WARNING); |
| 106 | +``` |
| 107 | +Any value or combination of values accepted by [set_error_handler](https://www.php.net/manual/en/function.set-error-handler.php) is usable. |
| 108 | +Default is `E_ALL`. [List of constants](https://www.php.net/manual/en/errorfunc.constants.php). |
| 109 | + |
| 110 | +### The global error handler |
| 111 | + |
| 112 | +The class also has global `set()` and `restore()` methods. |
| 113 | + |
| 114 | +```php |
| 115 | +use Phrity\Util\ErrorHandler; |
| 116 | + |
| 117 | +$handler = new ErrorHandler(); |
| 118 | +$handler->set(); // Throws ErrorException on error |
| 119 | +$handler->set(new RuntimeException('A specified error')); // Throws provided Throwable on error |
| 120 | +$handler->set(function (ErrorException $error) { |
| 121 | + // Code to handle errors |
| 122 | + return $error_result; |
| 123 | +}); // Runs callback on error |
| 124 | +$handler->restore(); // Restores error handler |
| 125 | +``` |
| 126 | + |
| 127 | +### Class synopsis |
| 128 | + |
| 129 | +```php |
| 130 | +Phrity\Util\ErrorHandler { |
| 131 | + |
| 132 | + /* Methods */ |
| 133 | + public __construct() |
| 134 | + |
| 135 | + public with(callable $callback, mixed $handling = null, int $levels = E_ALL) : mixed |
| 136 | + public withAll(callable $callback, mixed $handling = null, int $levels = E_ALL) : mixed |
| 137 | + public set($handling = null, int $levels = E_ALL) : mixed |
| 138 | + public restore() : bool |
| 139 | +} |
| 140 | +``` |
| 141 | + |
| 142 | +## Versions |
| 143 | + |
| 144 | +| Version | PHP | | |
| 145 | +| --- | --- | --- | |
| 146 | +| `1.0` | `^7.2|^8.0` | Initial version | |
0 commit comments