PHPackages                             alcamo/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. alcamo/exception

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

alcamo/exception
================

Exception classes with largely automatic message generation

0.1.33(2mo ago)0690—0%16Apache-2.0PHPPHP 7.3 - 8.0

Since Oct 18Pushed 2mo ago1 watchersCompare

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

READMEChangelogDependencies (8)Versions (35)Used By (16)

Usage examples
==============

[](#usage-examples)

Predefined exception classes
----------------------------

[](#predefined-exception-classes)

```
use alcamo\exception\InvalidEnumerator;

try {
    throw (new InvalidEnumerator())->setMessageContext(
        [
            'value' => 'fox',
            'expectedOneOf' => [ 'foo', 'bar', 'baz' ],
            'atLine' => 42
        ]
    );
} catch (\Exception $e) {
    echo $e->getMessage();
}

```

This will output

```
Invalid value "fox", expected one of ["foo", "bar", "baz"] at line 42.

```

The first part up to the comma is taken from `InvalidEnumerator::NORMALIZED_MESSAGE`, the rest is created from the context based on settings in `MessageFactory`.

A number of such predefined exception classes with default messages is provided, as well as a number of context item definitions. Both may easily be extended or modified.

Since the mechanism is based on the interface `ExceptionInterface` and the trait `ExceptionTrait`, it can easily be integrated into any existing exception class hierarchy.

Error handler
-------------

[](#error-handler)

```
use alcamo\exception\ErrorHandler;

public function foo()
{
    $handler = new ErrorHandler();

```

... any error occurring here will throw an `ErrorException` ...

```
}

```

After leaving the scope, the `$handler` has been destroyed, hence previous error handling is restored.

Exception dumper
----------------

[](#exception-dumper)

```
use alcamo\exception\Dumper;

try {

```

...

```
} catch (\Throwable $e) {
    echo (new Dumper())->dump($e);
}

```

Reports the details of the exception. Useful, for instance, in command-line interfaces.

Supplied generic interfaces, traits and classes
===============================================

[](#supplied-generic-interfaces-traits-and-classes)

The following only explains what is needed to implement the mechanism illustrated above. The provided predefined exception classes are basically all trivial because they mainly consist of a class constant with a normalized message.

Interface `MessageFactoryInterface`
-----------------------------------

[](#interface-messagefactoryinterface)

Simple interface requiring a method `createMessage()` which takes a normalized message and a context array and returns a string.

Class `MessageFactory`
----------------------

[](#class-messagefactory)

Implementation of `MessageFactoryInterface` which extends the functionality of Wikimedia's `NormalizedExceptionTrait::getMessageFromNormalizedMessage()` in that it not only replaces placeholders by context elements but generates more message text from the context.

The generation of messages is configured by class constants which can be extended or modified in derived classes. The present implementation focuses on information where in the input data an exception was thrown.

Interface `ExceptionInterface`
------------------------------

[](#interface-exceptioninterface)

Extends Wikimedia's [INormalizedException](https://github.com/wikimedia/mediawiki-libs-NormalizedException/blob/v1.0.1/src/INormalizedException.php)with two methods to set the message context and to add data to an existing message context.

Trait `ExceptionTrait`
----------------------

[](#trait-exceptiontrait)

Extends Wikimedia's [NormalizedExceptionTrait](https://github.com/wikimedia/mediawiki-libs-NormalizedException/blob/v1.0.1/src/NormalizedExceptionTrait.php), implementing `ExceptionInterface`. Uses `MessageFactory` (or any injected object that implements `MessageFactoryInterface`) to generate a message. The underlying normalized message can be taken from a class constant.

Class `ErrorHandler`
--------------------

[](#class-errorhandler)

Implements the [RAII pattern](https://en.wikipedia.org/wiki/Resource_acquisition_is_initialization)to establish an error handler function as long as the `ErrorHandler`object exists. The provided error handler function throws an `ErrorException` and can be overridden in derived classes.

Class `Dumper`
--------------

[](#class-dumper)

Provides a method `dump()` to dump the details of a `Throwable` as multiline text. The pieces of output are created by individual methods so that it can easily be customized in derived classes.

###  Health Score

47

—

FairBetter than 94% of packages

Maintenance86

Actively maintained with recent releases

Popularity19

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity57

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

Recently: every ~15 days

Total

34

Last Release

69d ago

### Community

Maintainers

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

---

Top Contributors

[![rv1971](https://avatars.githubusercontent.com/u/39520256?v=4)](https://github.com/rv1971 "rv1971 (9 commits)")

---

Tags

exception

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[pear/pear_exception

The PEAR Exception base class.

9880.2M101](/packages/pear-pear-exception)[yireo/magento2-webp2

Magento 2 module to add WebP support to the Magento frontend

2091.2M7](/packages/yireo-magento2-webp2)[phpattempt/phpattempt

Helper function to enforce error-first approach in your code

1710.4k](/packages/phpattempt-phpattempt)

PHPackages © 2026

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