2017 © Pedro Peláez
 

library context-exception

A context exception interface and implementation

image

fab2s/context-exception

A context exception interface and implementation

  • Friday, July 13, 2018
  • by fab2s
  • Repository
  • 1 Watchers
  • 2 Stars
  • 2,175 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 21 % Grown

The README.md

ContextException

Build Status Total Downloads Monthly Downloads Latest Stable Version SensioLabsInsight Code Climate Codacy Badge Scrutinizer Code Quality PRs Welcome PHPPackages Referenced By License, (*1)

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., (*2)

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., (*3)

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)., (*4)

In your exception handler (or just a try{}catch{}:, (*5)

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 :, (*6)

// 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 :, (*7)

} 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 ..., (*8)

Installation

ContextException can be installed using composer:, (*9)

composer require "fab2s/context-exception"

If you want to specifically install the php >=7.1.0 version, use:, (*10)

composer require "fab2s/context-exception" ^1

If you want to specifically install the php 5.6/7.0 version, use:, (*11)

composer require "fab2s/context-exception" ^0

Requirements

ContextException is tested against php 7.1, 7.2, 7.3 and 7.4, (*12)

License

ContextException is open-sourced software licensed under the MIT license., (*13)

The Versions

13/07 2018

dev-master

9999999-dev

A context exception interface and implementation

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Fabrice de Stefanis

php context exception

13/07 2018

1.0.2

1.0.2.0

A context exception interface and implementation

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Fabrice de Stefanis

php context exception

09/11 2017

1.0.1

1.0.1.0

A context exception interface and implementation

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Fabrice de Stefanis

php context exception

01/10 2017

1.0.0

1.0.0.0

A context exception interface and implementation

  Sources   Download

MIT

The Requires

  • php >=5.6.0

 

The Development Requires

by Fabrice de Stefanis

php context exception