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.1.0(2w ago)3652.8k↓41.5%3MITPHPPHP ^8.2CI passing

Since Oct 1Pushed 2w 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 2w ago

READMEChangelog (6)Dependencies (4)Versions (9)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) [![codecov](https://camo.githubusercontent.com/c0da95c5367403c08d0624b72547a5b93e294e28d835f1160d3aeda3589cbc57/68747470733a2f2f636f6465636f762e696f2f67682f66616232732f436f6e74657874457863657074696f6e2f67726170682f62616467652e737667)](https://codecov.io/gh/fab2s/ContextException) [![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) [![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 8.2, 8.3, 8.4 and 8.5

License
-------

[](#license)

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

###  Health Score

62

—

FairBetter than 99% of packages

Maintenance97

Actively maintained with recent releases

Popularity39

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity81

Battle-tested with a long release history

 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 ~460 days

Recently: every ~660 days

Total

8

Last Release

16d ago

Major Versions

1.x-dev → 2.0.02019-09-24

2.x-dev → 3.0.02023-01-04

PHP version history (5 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

3.1.0PHP ^8.2

### 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 (27 commits)")

---

Tags

phpexceptioninterfaceContext

###  Code Quality

TestsPHPUnit

Code StyleLaravel Pint

### 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)
```

PHPackages © 2026

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