PHPackages                             levacic/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. [Logging &amp; Monitoring](/categories/logging)
4. /
5. levacic/exception-with-context

ActiveLibrary[Logging &amp; Monitoring](/categories/logging)

levacic/exception-with-context
==============================

A generic interface for exception classes carrying their own context

1.0.0(5y ago)148.7k↑12.6%1MITPHPPHP ^7.0|^8.0

Since Dec 30Pushed 5y ago1 watchersCompare

[ Source](https://github.com/levacic/exception-with-context)[ Packagist](https://packagist.org/packages/levacic/exception-with-context)[ Docs](https://github.com/levacic/exception-with-context)[ RSS](/packages/levacic-exception-with-context/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (1)

Exception with context
======================

[](#exception-with-context)

[![Packagist PHP Version Support](https://camo.githubusercontent.com/d4402d0d7247907670d8aafd2ad9571f804d65aeef0ee5d91235b9120728004c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c6576616369632f657863657074696f6e2d776974682d636f6e74657874)](https://camo.githubusercontent.com/d4402d0d7247907670d8aafd2ad9571f804d65aeef0ee5d91235b9120728004c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6c6576616369632f657863657074696f6e2d776974682d636f6e74657874)[![Packagist Version](https://camo.githubusercontent.com/fd8369a61cc3bd5a0f95c73f61727ba35465c434151b2ca30205760406626ec1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6576616369632f657863657074696f6e2d776974682d636f6e74657874)](https://camo.githubusercontent.com/fd8369a61cc3bd5a0f95c73f61727ba35465c434151b2ca30205760406626ec1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c6576616369632f657863657074696f6e2d776974682d636f6e74657874)[![Packagist Downloads](https://camo.githubusercontent.com/36f54c6c1121ecaf6709e3b686fa332566050a7293ad322c1f170833b017f9c0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6576616369632f657863657074696f6e2d776974682d636f6e74657874)](https://camo.githubusercontent.com/36f54c6c1121ecaf6709e3b686fa332566050a7293ad322c1f170833b017f9c0/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c6576616369632f657863657074696f6e2d776974682d636f6e74657874)[![Packagist License](https://camo.githubusercontent.com/8978b1582e492682c34334f30c7f60be09c117cc1025f5e2b0aa19868b64111d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c6576616369632f657863657074696f6e2d776974682d636f6e74657874)](https://camo.githubusercontent.com/8978b1582e492682c34334f30c7f60be09c117cc1025f5e2b0aa19868b64111d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6c6576616369632f657863657074696f6e2d776974682d636f6e74657874)

This minimal package provides a single `ExceptionWithContext` interface which can be implemented in client-side code so your exception objects can carry their own context.

For more information on exactly how and why you would want to do this, read the "Usage" section.

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

[](#requirements)

- PHP &gt;= 7.0

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

[](#installation)

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

Usage
-----

[](#usage)

### Why?

[](#why)

The "why" obviously depends on how you use exceptions in general, but this is specifically useful when logging exceptions, in order to have some additional information on why the exception was thrown. In general, if you're logging in PHP in 2020, you're more than likely using a logger compatible with PSR-3 - and these loggers allow you to log additional context data, along with the actual log message.

This package, along with some manual or automated wiring (as described further below) allows exceptions to carry their own context, so your logging logic can be cleaner while being more informative at the same time.

### Example implementation

[](#example-implementation)

When using this package, you would usually implement an exception class something like this:

```
