pioniro/contextable-exception

Adds context to your exceptions

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

This package is auto-updated.

Last update: 2024-04-29 04:46:29 UTC


README

Is a common interfaces for adding a context in your exceptions.

It may be useful for providing more context data in logger (or sentry, etc) more context data

before:

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

after:

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]);
}

OR

use Pioniro\ContextableException\ContextableInterface;

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

Did you see this? We provide more data for exception

That's why this library is.