PHPackages                             beter/exception-with-context - 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. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. beter/exception-with-context

ActiveLibrary[Debugging &amp; Profiling](/categories/debugging)

beter/exception-with-context
============================

Interface, Trait and Class of the exception with a context

1.0.1(4y ago)110.5k—3.1%12MITPHPPHP &gt;=7.4.0

Since Jun 10Pushed 4y ago1 watchersCompare

[ Source](https://github.com/BETER-CO/php-exception-with-context)[ Packagist](https://packagist.org/packages/beter/exception-with-context)[ Docs](https://github.com/BETER-CO/php-exception-with-context)[ RSS](/packages/beter-exception-with-context/feed)WikiDiscussions master Synced yesterday

READMEChangelogDependenciesVersions (6)Used By (2)

exception-with-context
======================

[](#exception-with-context)

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

[](#requirements)

PHP &gt;= 7.4.0

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

[](#installation)

The preferred way to install this extension is through [composer](https://getcomposer.org/).

Either run

```
composer require beter/exception-with-context

```

or add

```
"beter/exception-with-context": "~1.0.0"  // put the actual version

```

to the `require` section of your composer.json.

Usage
-----

[](#usage)

Usage of the `ExceptionWithContext`:

```
use Beter\ExceptionWithContext\ExceptionWithContext;

$exceptionCode = 0;
$previousException = null;
$context = [
    'userIp' => '1.2.3.4',
    'userId' => 1,
    'request' => [
        'headers' => [ /* put headers data, for example */ ]
    ],
];

$e = new ExceptionWithContext('Action is not allowed for the user', $exceptionCode, $previousException, $context);

// or you may not to pass the context

$e = new ExceptionWithContext('Action is not allowed for the user', $exceptionCode, $previousException);

// or even skip $exceptionCode and $previousException

$e = new ExceptionWithContext('Action is not allowed for the user');

// or set context via setContext method call
$e = new ExceptionWithContext('Action is not allowed for the user');
$e->setContext($context);

// and get context back
var_dump($e->getContext());
```

You may create a chain of exceptions too:

```
use Beter\ExceptionWithContext\ExceptionWithContext;

try {
    do_smth();
} catch (\Throwable $catched) {
    $e = new ExceptionWithContext('Something went wrong', 0, $catched, ['key' => 'value']);
}
```

You may redefine your base exceptions and add context trait to them. They will behave the same way.

```
use Beter\ExceptionWithContext\ExceptionWithContextInterface;
use Beter\ExceptionWithContext\ExceptionWithContextTrait;

class CustomException extends \Exception implements ExceptionWithContextInterface
{
    use ExceptionWithContextTrait;

    public function __construct($message = "", $code = 0, Throwable $previous = null, array $context = [])
    {
        $this->context = $context;

        parent::__construct($message, $code, $previous);
    }
}

$e = new CustomException('Message text', 0, null, ['key' => 'value']);
$e->setContext(['newkey' => 'newvalue']);
var_dump($e->getContext());
```

Also, you may implement your own custom exceptions, you even don't need to use traits. Just implement `Beter\Exception\ExceptionWithContextInterface`.

### Ideas for usage

[](#ideas-for-usage)

- add context to log messages;
- protect from flooding exception message with placeholded values right into the message;
- pass more data to sentry/logentries/datadog/newrelic and so on.

> You need to implement that support by yourself. There are only ideas.

Development and testing
-----------------------

[](#development-and-testing)

Follow the [development and testing doc](doc/development-and-testing.md).

Related projects
----------------

[](#related-projects)

These projects use `beter/exception-with-context`:

- Bulletproof logging for yii2 projects - [BETER-CO/yii2-beter-logging](https://github.com/BETER-CO/yii2-beter-logging)
- Yii2 component to generate log entries with a context for the request and response events for the CLI and WEB - [BETER-CO/yii2-log-request-response](https://github.com/BETER-CO/yii2-log-request-response)

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity27

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

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

Total

5

Last Release

1484d ago

Major Versions

0.0.3 → 1.0.02022-06-10

### Community

Maintainers

![](https://www.gravatar.com/avatar/a9f186099e0b0ef9fcd3ac205ac00a692cd5b0b08b0ff401f443e4c7f73bbb55?d=identicon)[beter](/maintainers/beter)

---

Top Contributors

[![WoZ](https://avatars.githubusercontent.com/u/85790?v=4)](https://github.com/WoZ "WoZ (7 commits)")

---

Tags

exceptionerrorContextbeterexception with contextexception-wtih-context

### Embed Badge

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

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

###  Alternatives

[filp/whoops

php error handling for cool kids

13.6k429.7M1.5k](/packages/filp-whoops)[zeuxisoo/slim-whoops

PHP whoops error on slim framework

1371.1M27](/packages/zeuxisoo-slim-whoops)[ikkez/f3-falsum

Pretty error handling for Fat-Free Framework

22149.2k3](/packages/ikkez-f3-falsum)[thehocinesaad/laravel-error-ai

This package adds Ask AI button to the error page.

2226.9k](/packages/thehocinesaad-laravel-error-ai)[kuria/error

Makes handling and debugging PHP errors suck less

2120.8k2](/packages/kuria-error)[orisai/tracy-pets

Tracy got an angry pet to remind you of your failures

1026.3k4](/packages/orisai-tracy-pets)

PHPackages © 2026

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