定制 fab2s/context-exception 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

fab2s/context-exception

最新稳定版本:3.0.0

Composer 安装命令:

composer require fab2s/context-exception

包简介

A context exception interface and implementation

README 文档

README

CI QA Total Downloads Monthly Downloads Latest Stable Version Code Climate Scrutinizer Code Quality PRs Welcome License

Exception logging proves particularly useful to monitor application failure, but as it is, cannot provide with contextual data that could help out when processing those exception logs.

This Exception interface and implementation is just a step towards providing context to exceptions allowing you to interact with such exception and log them together with the extra information you would have provided.

It goes particularly well with Monolog and global exception handling where you can directly and globally add exception context to your log (when available) which may be of virtually any nature (monolog has a lot of handler and you can add more yourself to suite your need).

In your exception handler (or just a try{}catch{}:

if ($exception instanceof ContextExceptionInterface) {
    $context = $exception->getContext();
    // now you can add this data to your monolog's context
}

Setting context can be achieved in several ways :

// arguable way
throw (new ContextException('Message'))->setContext($contextData);

// or more conventional
throw new ContextException('Message', 0, null, $contextData);

// or step by step (also arguable)
$exception = new ContextException;
$exception->setContext($contextData);
throw $exception;

IMHO, the arguability of some of the way to provide context to exceptions does not match the usefulness they provide IRL. I'm not saying that the conventional way to throw should not be in principle preferred, but I definitely find IRL use for things like :

} catch (ContextException $e) {
    // access context
    $context = $e->getContext();
    // set context in case there is none
    $e->setContext($context);
}

And from there, you get mutability so ...

Installation

ContextException can be installed using composer:

composer require "fab2s/context-exception"

V3.x introduces a breaking change in the ContextExceptionInterface which no more includes a signature for the constructor or even a merge function. It introduce and handy ContextExceptionTrait you can use to implement your own implementation of ContextExceptionInterface without having to inherit ContextException

If you want to stick to the previous interface :

composer require "fab2s/context-exception" ^2

If you want to specifically install the php >=7.1.0 version, use:

composer require "fab2s/context-exception" ^1

If you want to specifically install the php 5.6/7.0 version, use:

composer require "fab2s/context-exception" ^0

Requirements

ContextException is tested against php 7.1, 7.2, 7.3, 7.4, 8.0, 8.1 and 8.2

License

ContextException is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-30