PHPackages                             fab2s/context-exception - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. fab2s/context-exception

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

fab2s/context-exception
=======================

A context exception interface and implementation

3.0.0(3y ago)4626.4k↓18%3MITPHPPHP ^7.1 || ^8.0

Since Oct 1Pushed 3y ago1 watchersCompare

[ Source](https://github.com/fab2s/ContextException)[ Packagist](https://packagist.org/packages/fab2s/context-exception)[ RSS](/packages/fab2s-context-exception/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (2)Versions (8)Used By (3)

ContextException
================

[](#contextexception)

[![CI](https://github.com/fab2s/ContextException/actions/workflows/ci.yml/badge.svg)](https://github.com/fab2s/ContextException/actions/workflows/ci.yml) [![QA](https://github.com/fab2s/ContextException/actions/workflows/qa.yml/badge.svg)](https://github.com/fab2s/ContextException/actions/workflows/qa.yml) [![Total Downloads](https://camo.githubusercontent.com/3d548efa95aa5d239a9fbe1f9949a3ab3ef7c75d6801708e0e5ea8899c66eaa3/68747470733a2f2f706f7365722e707567782e6f72672f66616232732f636f6e746578742d657863657074696f6e2f646f776e6c6f616473)](https://packagist.org/packages/fab2s/context-exception) [![Monthly Downloads](https://camo.githubusercontent.com/826cd3c5be89aac618b2fe5091134b859044e3b4f2eed037693b4fdc43a8f86e/68747470733a2f2f706f7365722e707567782e6f72672f66616232732f636f6e746578742d657863657074696f6e2f642f6d6f6e74686c79)](https://packagist.org/packages/fab2s/context-exception) [![Latest Stable Version](https://camo.githubusercontent.com/b05dc3618363abdcf338f8f981a6b31cbb44f083d66f78b858960c215efb9b8a/68747470733a2f2f706f7365722e707567782e6f72672f66616232732f636f6e746578742d657863657074696f6e2f762f737461626c65)](https://packagist.org/packages/fab2s/context-exception) [![Code Climate](https://camo.githubusercontent.com/137c929f8d0d9c92f40e00e34bff580aded9ab95c1e19788bb37e51ca8d4efe5/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f66616232732f436f6e74657874457863657074696f6e2f6261646765732f6770612e737667)](https://codeclimate.com/github/fab2s/ContextException) [![Scrutinizer Code Quality](https://camo.githubusercontent.com/20a9e3d069dd7b07c198e6044bc12926a211e405d823e7f3a8c4d88d378c0006/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f66616232732f436f6e74657874457863657074696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/fab2s/ContextException/?branch=master) [![PRs Welcome](https://camo.githubusercontent.com/7d9ed3c8f22eceb1711573169b1390cc0b1194467340dc815205060c162b5309/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5052732d77656c636f6d652d627269676874677265656e2e7376673f7374796c653d666c6174)](http://makeapullrequest.com) [![License](https://camo.githubusercontent.com/d5ad739f3b1338aa277a5d6d7a96e4a812c5788ce15370c1ff3ce00176ff3b55/68747470733a2f2f706f7365722e707567782e6f72672f66616232732f6e6f64616c666c6f772f6c6963656e7365)](https://packagist.org/packages/fab2s/yaetl)

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](https://github.com/Seldaek/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 from there, you get mutability so ...

Installation
------------

[](#installation)

ContextException can be installed using composer:

```
composer require "fab2s/context-exception"

```

> V3.x introduces a breaking change in the `ContextExceptionInterface` which no more includes a signature for the constructor or even a merge function. It introduce and handy `ContextExceptionTrait` you can use to implement your own implementation of `ContextExceptionInterface` without having to inherit `ContextException`

If you want to stick to the previous interface :

```
composer require "fab2s/context-exception" ^2

```

If you want to specifically install the php &gt;=7.1.0 version, use:

```
composer require "fab2s/context-exception" ^1

```

If you want to specifically install the php 5.6/7.0 version, use:

```
composer require "fab2s/context-exception" ^0

```

Requirements
------------

[](#requirements)

ContextException is tested against php 7.1, 7.2, 7.3, 7.4, 8.0, 8.1 and 8.2

License
-------

[](#license)

ContextException is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT).

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity40

Moderate usage in the ecosystem

Community12

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~320 days

Recently: every ~409 days

Total

7

Last Release

1231d ago

Major Versions

1.x-dev → 2.0.02019-09-24

2.x-dev → 3.0.02023-01-04

PHP version history (4 changes)1.0.0PHP &gt;=5.6.0

2.0.0PHP &gt;=7.1

2.x-devPHP ^7.1|^8.0

3.0.0PHP ^7.1 || ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7323989?v=4)[fab2s](/maintainers/fab2s)[@fab2s](https://github.com/fab2s)

---

Top Contributors

[![fab2s](https://avatars.githubusercontent.com/u/7323989?v=4)](https://github.com/fab2s "fab2s (24 commits)")

---

Tags

phpexceptioninterfaceContext

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/fab2s-context-exception/health.svg)

```
[![Health](https://phpackages.com/badges/fab2s-context-exception/health.svg)](https://phpackages.com/packages/fab2s-context-exception)
```

###  Alternatives

[gabrielrcouto/php-gui

Extensionless PHP Graphic User Interface library

2.3k7.2k](/packages/gabrielrcouto-php-gui)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
