https://github.com/mpyw/exceper
Provides temporary error handler automatically using set_error_handler() and restore_error_handler().
https://github.com/mpyw/exceper
error exception handler php
Last synced: 5 months ago
JSON representation
Provides temporary error handler automatically using set_error_handler() and restore_error_handler().
- Host: GitHub
- URL: https://github.com/mpyw/exceper
- Owner: mpyw
- License: mit
- Archived: true
- Created: 2017-05-20T05:44:06.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2024-05-05T02:16:43.000Z (about 2 years ago)
- Last Synced: 2025-10-21T11:59:53.541Z (8 months ago)
- Topics: error, exception, handler, php
- Language: PHP
- Homepage:
- Size: 54.7 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Exceper [](https://github.com/mpyw/exceper/actions) [](https://coveralls.io/github/mpyw/exceper?branch=master) [](https://scrutinizer-ci.com/g/mpyw/exceper/?branch=master)
Provides temporary error handler automatically using [`set_error_handler()`] and [`restore_error_handler()`].
| PHP | :question: | Feature Restriction |
|:---:|:---:|:---:|
| 5.3.2~ | :smile: | Supported |
| ~5.3.1 | :boom: | Incompatible |
## Installing
```
composer require mpyw/exceper
```
## Quick Example
```php
Mpyw\Exceper\Convert::to`{$class}`()
Capture errors to convert into an instance of the specified class.
- Error handlers are automatically restored.
- **`->getFile()` and `->getLine()` returns correct locations** like `ErrorException` does.
```php
static Mpyw\Exceper\Convert::to(string $class, callable $callback, int $types = E_ALL | E_STRICT): mixed
static Mpyw\Exceper\Convert::to{$class}(callable $callback, int $types = E_ALL | E_STRICT): mixed
static Mpyw\Exceper\Convert::to(string $class, int $code, callable $callback, int $types = E_ALL | E_STRICT): mixed
static Mpyw\Exceper\Convert::to{$class}(int $code, callable $callback, int $types = E_ALL | E_STRICT): mixed
```
#### Arguments
- **`(string)`** __*$class*__
Conversion target class name which is an instance of `\Exception` or `\Throwable`. Please note that case-sensitivity of class name depends on the implementation of your autoloaders, which can cause an unexpected behavior if the target class is not loaded.
- **`(int)`** __*$code*__
Error code passed to the constructor as the second argument. Default to `0`.
- **`(callable)`** __*$callback*__
Callback function to be executed. This parameter SHOULD be used as `\Closure` because arguments cannot be specified.
- **`(int)`** __*$types*__
Bit mask of target error severities.
#### Return Value
**`(mixed)`**
Propagates upcoming value from `$callback()`.
#### Exception
Converted errors.
### Mpyw\Exceper\Convert::silent()
Capture errors but never throw anything.
```php
static Mpyw\Exceper\Convert::silent(callable $callback, int $types = E_ALL | E_STRICT, bool $captureExceptions = true): mixed
```
#### Arguments
- **`(callable)`** __*$callback*__
Callback function to be executed. This parameter SHOULD be a `\Closure` because arguments cannot be specified.
- **`(int)`** __*$types*__
Bit mask of target error severities.
- **`(bool)`** __*$captureExceptions*__
Capture exceptions that were directly thrown; not originated in `set_error_handler()`.
#### Return Value
**`(mixed)`**
Propagates upcoming value from `$callback()`. Returns `null` if something thrown.
[`set_error_handler()`]: http://www.php.net/manual/function.set-error-handler.php
[`restore_error_handler()`]: http://www.php.net/manual/function.restore-error-handler.php