maximaster / exceptior
异常处理的函数集合,简化异常处理。
v1.1.1
2024-09-03 08:59 UTC
Requires
- php: ^8.2
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.63
- kahlan/kahlan: ^5.2
- vimeo/psalm: ^5.25
This package is auto-updated.
Last update: 2024-09-03 09:00:18 UTC
README
异常处理的函数集合,简化异常处理。
composer require maximaster/exceptior
案例 #1: 将抛出 void 函数转换为布尔值
use Maximaster\Exceptior\Ex; $noOpeation = function(): void {}; $wouldThrow = fn () => throw new Exception('hello'); Ex::boolize($noOpeation); // === true Ex::boolize($wouldThrow); // === false
案例 #2: 将抛出具有返回值的函数转换为所需的返回值
use Maximaster\Exceptior\Ex; $giveString = fn () => 'hello' $wouldThrow = fn () => throw new Exception('no hello'); Ex::suppressInto($giveString, 'fail'); // === 'hello' Ex::suppressInto($wouldThrow, 'fail'); // === 'fail'
案例 #3: 将异常转换为另一个异常
use Maximaster\Exceptior\Ex; $giveString = fn () => 'hello' $wouldThrow = fn () => throw new Exception('no hello'); $converter = fn (Throwable $thrown) => new RuntimeException($thrown->getMessage()); Ex::convert($giveString, $converter); // === 'hello' Ex::convert($wouldThrow, $converter); // throws RuntimeException
案例 #4: 将抛出的异常规范化为可调用提供的值
use Maximaster\Exceptior\Ex; $giveString = fn () => 'hello' $wouldThrow = fn () => throw new Exception('no hello'); $normaizeToMessage = fn (Throwable $throwable) => $throwable->getMessage(); Ex::normalize($giveString, $normaizeToMessage); // === 'hello' Ex::normalize($wouldThrow, $normaizeToMessage); // === 'no hello'
案例 #5: 如果工作器抛出异常,则返回工作器值或 null
$giveString = fn () => 'hello' $wouldThrow = fn () => throw new Exception('no hello'); Ex::nullize($giveString); // === 'hello' Ex::nullize($wouldThrow); // === null
开发
分支 → 编辑 → composer check
→ 提交请求。