pioniro / contextable-exception

为您的异常添加上下文

v1.0 2020-04-01 11:15 UTC

This package is auto-updated.

Last update: 2024-09-29 05:55:36 UTC


README

这是一个为异常添加上下文的常用接口。

它可能有助于在日志记录器(或sentry等)中提供更多上下文数据

在...

function badFunction($id)
{
    throw new \Exception(sprintf('bad Id: %d', $id));
}

之后...

use Pioniro\ContextableException\ContextableInterface;
use Pioniro\ContextableException\ContextableTrait;

class MyException extends \Exception implements ContextableInterface {
    use ContextableTrait;
}

function badFunction($id)
{
    throw (new MyException('bad Id'))->addContext(['id' => $id]);
}

或者

use Pioniro\ContextableException\ContextableInterface;

function badSuperFunction($id, $name)
{
    try {
        badThirdPartyFunction($id);
    } catch (ContextableInterface $e) {
        $e->addContext(['name' => $name]);
        throw $e;
    }
}

您看到了吗?我们为异常提供了更多信息

这就是为什么这个库存在的原因。