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

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

hgh/exception-handler
=====================

This package centralizes exceptions behaviour of projects. The duty of this package is to prevent to show unwanted exceptions to user.

v1.1.0(5y ago)1411MITPHP

Since Jul 7Pushed 5y ago1 watchersCompare

[ Source](https://github.com/H-Gh/exception-handler)[ Packagist](https://packagist.org/packages/hgh/exception-handler)[ RSS](/packages/hgh-exception-handler/feed)WikiDiscussions develop Synced 1w ago

READMEChangelog (1)Dependencies (1)Versions (4)Used By (1)

Exception Handler
=================

[](#exception-handler)

This package centralizes exceptions behaviour of projects. The duty of this package is to prevent to show unwanted exceptions to user. Actually sometimes you want some exceptions to be shown in output too. This package will handle both sides. All exceptions can use the predefined interfaces.

Table of contents
=================

[](#table-of-contents)

- [Installation](#installation)
- [Interfaces](#interfaces)
- [How to use](#how-to-use)
    - [Exception handling](#exception-handling)
    - [Exception logging](#exception-logging)

Installation
============

[](#installation)

To install this package, require it via the composer.

```
composer require hgh/exception-handler

```

Interfaces
==========

[](#interfaces)

There are 11 interfaces that exceptions can be implemented. The exceptions that are implemented from these interfaces will be log in the files by their type.

InterfaceDescriptionAlertInterfaceMark exception as an alert exceptionCriticalInterfaceMark exception as a critical exceptionDebugInterfaceMark exception as a debug exceptionEmergencyInterfaceMark exception as a emergency exceptionInfoInterfaceMark exception as a info exceptionNoticeInterfaceMark exception as a notice exceptionWarningInterfaceMark exception as a warning exceptionNotLogThe exception will not be logShouldPublishThe exception will publish in response handler, otherwise will replace with `UnexpectedException`WithDescriptionThe exception has extra description addition to exception messageHow to use
==========

[](#how-to-use)

Exception handling
------------------

[](#exception-handling)

First as a sample we define an exception class.

```
/**
* Class SampleException
*/
class SampleException extends BaseException implements ShouldPublish, ErrorInterface {
}
$exception = new SampleException("SampleException");
```

You can use exception handler directly.

```
$exceptionHandler = new ExceptionHandler($exception);
$exception = $exceptionHandler->handle(); // The result will be SampleException class
```

Or use facade:

```
\HGh\Handlers\Exception\Facades\Exception::handle($exception); // The result will be SampleException class
```

As you can see, the result of exception handling is the same exception, because it implements from `ShouldPublish` interface.

Let's define another exception to see another type of exception handling:

```
/**
* Class AnotherSampleException
*/
class AnotherSampleException extends BaseException implements WarningInterface {
}
$exception = new AnotherSampleException("AnotherSampleException");
$exception = \HGh\Handlers\Exception\Facades\Exception::handle($exception); // The result will UnexpectedException
```

See, the result of exception handling is not the same. It is `UnexpectedException` because it doesn't implement the `ShouldPublish` interface.

Exception Logging
-----------------

[](#exception-logging)

Another class exists here to log the exceptions. First before `ExceptionHandler` change the type of exception, log the exception, then handle it by `ExceptionHanlder`.
Let's define an exception.

```
/**
* Class SampleException
*/
class SampleException extends BaseException implements ShouldPublish, ErrorInterface {
}
$exception = new SampleException("SampleException");
```

Now it's the turn of the logger. You can use the direct service as below:

```
use HGh\Handlers\Exception\Services\ExceptionLogger;$filePath = "/tmp/logs";
$exceptionLogger = new ExceptionLogger($filePath);
$exceptionLogger->log($exception);
```

or use the facade:

```
\HGh\Handlers\Exception\Facades\Exception::log($exception, $filePath);
```

then handle it and pass it to output. Basically it's better to extend the facade of package and define your own fileWriter to prevent defining the fileWriter each time.

```
/**
 * This class is a facade to all exception actions
 * PHP version >= 7.0
 *
 * @category Facades
 * @package ExceptionHandler
 * @author Hamed Ghasempour
 * @license MIT https://opensource.org/licenses/MIT
 * @link null
 */
 class Exception extends ExceptionFacade
{
    /**
     * This method will log an exception
     *
     * @param Throwable $throwable The throwable
     * @param string    $filePath  The file path that throwable should be log into
     *
     * @return void
     */
    public static function log(Throwable $throwable, string $filePath = null)
    {
        if (empty($filePath)) {
            $filePath = "/tmp/logs";
        }
        parent::log($throwable, $filePath);
    }
}
```

By this way, before changing the exception by exception handler, you log every exception, and you will never miss them and on the other side, after handling the exceptions, you will never show to the user unwanted exceptions.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity57

Maturing project, gaining track record

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

Total

2

Last Release

2134d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7873222f18ced9739dd54f5bcfa13a8d9738496d22b38ff35d9f4b5b84f3269b?d=identicon)[h.gh](/maintainers/h.gh)

### Embed Badge

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

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

###  Alternatives

[psr/log

Common interface for logging libraries

10.4k1.2B9.2k](/packages/psr-log)[itsgoingd/clockwork

php dev tools in your browser

5.9k27.6M94](/packages/itsgoingd-clockwork)[graylog2/gelf-php

A php implementation to send log-messages to a GELF compatible backend like Graylog2.

41838.2M138](/packages/graylog2-gelf-php)[bugsnag/bugsnag-psr-logger

Official Bugsnag PHP PSR Logger.

32132.5M2](/packages/bugsnag-bugsnag-psr-logger)[consolidation/log

Improved Psr-3 / Psr\\Log logger based on Symfony Console components.

15462.2M7](/packages/consolidation-log)[datadog/php-datadogstatsd

An extremely simple PHP datadogstatsd client

19124.6M15](/packages/datadog-php-datadogstatsd)

PHPackages © 2026

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