serafim/evacuator
Composer 安装命令:
composer require serafim/evacuator
包简介
Try-catch with multiple retries
README 文档
README
He trying to save your code, if it is broken :3
Installation
composer require serafim/evacuator
Link to Packagist
Usage example
// What we are trying to keep safe? $result = rescue(function () { if (random_int(0, 9999) > 1) { throw new \Exception('Ooooups =('); } return 23; }); var_dump($result); // int(23)
Advanced usage
use Serafim\Evacuator\Evacuator; $result = (new Evacuator(function() { // Your a very important piece of code })) // Code throws an exception after 100500 call retries ->retry(100500) // or until the cancer is not on the mountain whistles... ->retry(Evacuator::INFINITY_RETRIES) // But if you want catch exception ->catch(function (Throwable $e) { return 'Something went wrong =('; // Will be returns into $result }) ->finally(function ($errorOrResult) { // Run this code after all attempts. // $errorOrResult can be error (if evacuator cant keep safe your code) or result value }) ->onError(function ($error) { // Run this code after every error }) ->invoke(); // Just run your very important code
Catching strategy
$result = (new Evacuator(function() { throw new \LogicException('Error'); })) ->catch(function (\RuntimeException $e) { // I am alone and never be use ='( }) // ->onError(function (\RuntimeException $e) {}) ->catch(function (\LogicException $e) { // Yay! I will calling because Im a LogicException! :D }) // ->onError(function (\LogicException $e) {}) ->invoke();
Enjoy! :3
统计信息
- 总下载量: 211
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 16
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: WTFPL
- 更新时间: 2016-04-06