mpyw / exceper
此包已被弃用且不再维护。未建议替代包。
使用 set_error_handler() 和 restore_error_handler() 自动提供临时错误处理器。
v2.0.0
2021-06-24 18:51 UTC
Requires
- php: >=5.3.2
Requires (Dev)
- php-coveralls/php-coveralls: ^1.1
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-05-05 02:23:31 UTC
README
使用 set_error_handler()
和 restore_error_handler()
自动提供临时错误处理器。
PHP | ❓ | 功能限制 |
---|---|---|
5.3.2~ | 😄 | 支持 |
~5.3.1 | 💥 | 不兼容 |
安装
composer require mpyw/exceper
快速示例
<?php require __DIR__ . '/vendor/autoload.php'; use Mpyw\Exceper\Convert; try { // We pick errors triggered by fopen() or fgets(). // They are converted into \RuntimeException. Convert::toRuntimeException(function () { $fp = fopen('http://example.com', 'rb'); while (false !== $line = fgets($fp)) { echo $line; } }); } catch (\RuntimeException $e) { // Do something here } // Error handler is already automatically restored.
API
Mpyw\Exceper\Convert::to()
Mpyw\Exceper\Convert::to{$class}
()
捕获错误并转换为指定类的实例。
- 错误处理器将自动恢复。
->getFile()
和->getLine()
返回正确位置,就像ErrorException
一样。
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
参数
(string)
$class
转换目标类名,该类名是\Exception
或\Throwable
的实例。请注意,类名的区分大小写取决于您自动加载器的实现,如果目标类未加载,这可能会导致意外的行为。(int)
$code
作为构造函数第二个参数传递的错误代码。默认为0
。(callable)
$callback
要执行的回调函数。此参数应使用\Closure
,因为无法指定参数。(int)
$types
目标错误严重性的位掩码。
返回值
(mixed)
从 $callback()
传播即将到来的值。
异常
转换后的错误。
Mpyw\Exceper\Convert::silent()
捕获错误但永远不会抛出任何内容。
static Mpyw\Exceper\Convert::silent(callable $callback, int $types = E_ALL | E_STRICT, bool $captureExceptions = true): mixed
参数
(callable)
$callback
要执行的回调函数。此参数应使用\Closure
,因为无法指定参数。(int)
$types
目标错误严重性的位掩码。(bool)
$captureExceptions
捕获直接抛出的异常;不是来自set_error_handler()
。
返回值
(mixed)
从 $callback()
传播即将到来的值。如果抛出任何内容,则返回 null
。