PHPackages                             ifcastle/exceptions - 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. ifcastle/exceptions

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

ifcastle/exceptions
===================

A basic library for exceptions with templates, oriented towards usage in web services

v5.2.1(1y ago)2274119MITPHPPHP &gt;=8.3

Since Jul 16Pushed 1y ago1 watchersCompare

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

READMEChangelog (10)Dependencies (5)Versions (10)Used By (19)

BaseException [![PHP Composer](https://github.com/EdmondDantes/amphp-pool/actions/workflows/php.yml/badge.svg)](https://github.com/EdmondDantes/BaseException/actions/workflows/php.yml) [![codecov](https://camo.githubusercontent.com/6324bbdd8fb6788cfe04925592ddf4f89437fde3afd5e1164a0a77955a6b090f/68747470733a2f2f636f6465636f762e696f2f67682f45646d6f6e6444616e7465732f42617365457863657074696f6e2f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/EdmondDantes/BaseException)

=============

Base Exception Library for PHP 8.3+ (The latest version: 5.1.0)

Missions:

1. Additional structural data for exceptions.
2. Aspects for exceptions.
3. Aggregation exceptions within exceptions.
4. Registration exceptions in the global registry for logging.
5. Support for the concept of message templates.
6. Support tags for exceptions (for elastic logging as example).

**And most importantly: make it all easy and simple ;)**

Overview
========

[](#overview)

Templates for the error message
-------------------------------

[](#templates-for-the-error-message)

```
class MyException extends \Exceptions\BaseException
{
    protected string $template      = 'The template error message with {var}';

    public function __construct($var)
    {
        parent::__construct
        ([
             'var'         => $this->toString($var)
         ]);
    }
}

$exception = new MyException('string');

// should be printed: The template error message with 'string'
echo $exception->getMessage();
```

Independent logging exceptions (Exceptions Registry)
----------------------------------------------------

[](#independent-logging-exceptions-exceptions-registry)

```
use \Exceptions\Registry;
use \Exceptions\LoggableException;

Registry::resetExceptionLog();

$exception      = new LoggableException('this is a loggable exception');

$log            = Registry::getExceptionLog();

if($log[0] === $exception)
{
    echo 'this is loggable $exception';
}
```

Support of the exception context parameters
-------------------------------------------

[](#support-of-the-exception-context-parameters)

The basic use:

```
    throw new BaseException('message', 0, $previous);
```

List of parameters:

```
    // use array()
    $exception = new BaseException
    ([
        'message'     => 'message',
        'code'        => 0,
        'previous'    => $previous,
        'mydata'      => [1,2,3]
    ]);

    ...

    // print_r([1,2,3]);
    print_r($exception->getExceptionData());
```

Monolog integration
-------------------

[](#monolog-integration)

The processor for `Monolog` allows populating exception metadata in the log.

```
