承接 sanmai/trycatch 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

sanmai/trycatch

最新稳定版本:v0.3.2

Composer 安装命令:

composer require sanmai/trycatch

包简介

Exception-handling callable wrapper

README 文档

README

Latest Stable Version Build Status Coverage Status

Exception-handling callable wrapper for Closures and other callables.

Usage

Suppose you were passing a closure to configure some dependencies, or do another mundane task:

$object->setCallback($someCallback);

And then you suddenly find that your callback started throwing exceptions here and there. You have the callback from somewhere beyond your control, so you can't really change what it does.

Now, if you want to handle certain types of exceptions gracefully, this is how you could do that:

$retval = $object->setCallback(\TryCatch\TryCatch::wrap($yourCallback)
  ->whenFailed(function (\Exception $e, $a, $b) {
    if ($e instanceof SpecificException) {
        // handle this one gracefully
        return null;             
    } elseif ($a > $b) {
        // else check for rogue callback's arguments
    }

    throw $e;
});
// $retval will be null in case of SpecificException being caught

You can also peek into arguments, that were passed to the callback.

See tests for for additional examples.

Install

composer require sanmai/trycatch

统计信息

  • 总下载量: 11.51k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-05-12