PHPackages                             rammewerk/error-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. rammewerk/error-handler

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

rammewerk/error-handler
=======================

Simple class to manage and normalize PHP errors

v1.3.1(6mo ago)350MITPHPPHP &gt;=8.2

Since Dec 12Pushed 6mo ago2 watchersCompare

[ Source](https://github.com/fjellvarden/error-handler)[ Packagist](https://packagist.org/packages/rammewerk/error-handler)[ Docs](https://rammewerk.com)[ RSS](/packages/rammewerk-error-handler/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (8)DependenciesVersions (9)Used By (0)

Rammewerk ErrorHandler
======================

[](#rammewerk-errorhandler)

A simple and flexible way to handle and manage errors and exceptions in your PHP application.

- PHP errors are turned into exceptions by default.
- You can register multiple closures to handle your errors.
- No other dependencies - small size - 1 file only.

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

[](#installation)

```
$ composer require rammewerk/error-handler

```

Usage
-----

[](#usage)

The error handler should be registered early in your application to ensure they capture any issues that might occur from the outset. If registered late, initial errors might go uncaught. Early registration also promotes structured error management, facilitating easier debugging and maintenance.

```
use Rammewerk\Component\ErrorHandler;

$errorHandling = new ErrorHandler();

$errorHandling->log( static function (\Throwable $e) {
    # Log your exceptions
});

$errorHandling->report( static function (\Throwable $e) {
    # Show the exception to your user.
});
```

*There are basically no difference between the `log` and `report`. Log closures will be called first, then the reports. So you can add different `log` handlers at later point in script, even though an earlier registered `report`closure might be used to exit PHP.*

`log` closures are called in the order they got registered. `reports` are called the in reverse order. So you can early in your application add a general message and then later on add new reports to handle different scenarios the handler calls the "general" error report.

Why use Rammewerk ErrorHandler?
-------------------------------

[](#why-use-rammewerk-errorhandler)

PHP will either throw exceptions - which are intended to be caught - or print Errors which are generally unrecoverable. But instead of having to deal with both exceptions and errors, we convert errors so you only have to deal with exceptions. No more `@file_get_contents` just nice and neat try/catch.

**Uncaught** exceptions will be given to your `log` and `report` closures.

Many other frameworks or libraries are way more advanced, and comes with loads of functions. This ErrorHandler is simple by design - you decide how to handle your uncaught exceptions and errors.

Tips
----

[](#tips)

- Add the ErrorHandler class as early in your application as possible.
- Add `log` and `report` closure at any point you want. The `ErrorHandler` will only call these closures if the exception occurred after the registered closure. So, consider adding `log` and `report` closures as soon as possible.
- You can add multiple `log` and `report` closures. `log` will be called before `report` closures.
- If you want to remove previous registered closures - set the second argument as true in `log` or `report`. Example: `$error->log( $closure, true )`. This can be useful if you want to replace logging after calling other dependencies at a later point in your scripts.

Example
-------

[](#example)

```
use Rammewerk\Component\ErrorHandler;

CONST DEBUG_MODE = true;

$errorHandler = new ErrorHandler();

/** Log latest exception to JSON file */
$errorHandler->log( static function (\Throwable $e) {
  file_put_contents( 'latest_error.json', json_encode($e->getMessage()) );
});

/** Show default 500 error page */
$errorHandler->report( static function () {
    http_response_code( 500 );
    echo file_get_contents( "errors/500.html" );
    die;
} );

/**
 * Show error details if DEBUG_MODE.
 * Second argument here (true) is used to reset the report list.
 * report closures
 */
if( DEBUG_MODE ) {
  $errorHandler->report( function (\Throwable $e) {
      $debug = new \CustomDebugClass();
      $debug->exception($e);
      die;
  }, true);
}
```

Handle different type of exceptions
-----------------------------------

[](#handle-different-type-of-exceptions)

`report` and `log` closures handle any `Throwable` exceptions. To distinguish between exception types, incorporate an `instanceOf` check within the closure.

```
$errorHandling->report( function (\Throwable $e) {
    if( $e instanceof \MyCustomException) ) {
        die('MyCustomException was thrown');
    }
  } );
```

###  Health Score

41

—

FairBetter than 89% of packages

Maintenance67

Regular maintenance activity

Popularity12

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 71.4% 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 ~151 days

Recently: every ~178 days

Total

8

Last Release

194d ago

PHP version history (2 changes)v1.0-beta.1PHP 8.2

v1.0.0PHP &gt;=8.2

### Community

Maintainers

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

---

Top Contributors

[![follestad](https://avatars.githubusercontent.com/u/32802271?v=4)](https://github.com/follestad "follestad (10 commits)")[![rammewerk](https://avatars.githubusercontent.com/u/120019460?v=4)](https://github.com/rammewerk "rammewerk (4 commits)")

---

Tags

componenterrorsphprammewerk-componentexceptionserror-handler

### Embed Badge

![Health badge](/badges/rammewerk-error-handler/health.svg)

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

###  Alternatives

[sentry/sentry

PHP SDK for Sentry (http://sentry.io)

1.9k227.1M273](/packages/sentry-sentry)[sentry/sentry-laravel

Laravel SDK for Sentry (https://sentry.io)

1.3k114.3M154](/packages/sentry-sentry-laravel)[bugsnag/bugsnag-laravel

Official Bugsnag notifier for Laravel applications.

90234.6M36](/packages/bugsnag-bugsnag-laravel)[bugsnag/bugsnag

Official Bugsnag notifier for PHP applications.

56347.0M78](/packages/bugsnag-bugsnag)[sentry/sdk

This is a meta package of sentry/sentry. We recommend using sentry/sentry directly.

327134.8M151](/packages/sentry-sdk)[bugsnag/bugsnag-psr-logger

Official Bugsnag PHP PSR Logger.

32132.5M2](/packages/bugsnag-bugsnag-psr-logger)

PHPackages © 2026

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