fab2s/context-exception

A context exception interface and implementation

1.0.2 2018-07-13 21:14 UTC

README

Build Status Total Downloads Latest Stable Version SensioLabsInsight Code Climate Codacy Badge Scrutinizer Code Quality PRs Welcome PHPPackages Referenced By 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 even add more context
    $e->mergeContext($moreContext);
}

And from there, you get mutability so ...

Installation

ContextException can be installed using composer :

composer require "fab2s/context-exception"

Requirements

ContextException is tested against php 5.6, 7.0, 7.1, 7.2 and 7.3.

License

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