zeeproject/exceptions

Set of the base exceptions

0.1.0 2018-12-08 21:34 UTC

README

Software License Latest Version on Packagist Build Status Coverage Status Quality Score

This repository contains a set of useful exception classes.

The exception classes interface is extended with array of context data, because often the message and the code are not enough to debug.

Install

Using Composer:

composer require zeeproject/exceptions

Usage

Throw exceptions as usual, but you are able also to add context values:

throw new InvalidArgumentException('Something went wrong', [
    'key' => 'velue',
]);

Now you can handle this exception, e.q. your error handler may log error details:

final class ErrorHandler
{
    private $logger;

    public function __construct(Psr\Log\LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function handleException(Exception $exception)
    {
        if ($exception instanceof Zee\Exceptions\Throwable) {
            $this->logger->error($exception->getMessage(), $exception->getContext());
        } else {
            $this->logger->error($exception->getMessage());
        }
    }
}

Testing

phpunit

Contributing

Contributions are welcome and will be fully credited. Please see CONTRIBUTING and CODE OF CONDUCT for details.

Credits

License

It's BSD 2-Clause.