PHPackages                             tobento/service-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. [Debugging &amp; Profiling](/categories/debugging)
4. /
5. tobento/service-error-handler

ActiveLibrary[Debugging &amp; Profiling](/categories/debugging)

tobento/service-error-handler
=============================

PHP errors and exceptions handling

2.0(7mo ago)02652MITPHPPHP &gt;=8.4

Since Jan 31Pushed 7mo ago1 watchersCompare

[ Source](https://github.com/tobento-ch/service-error-handler)[ Packagist](https://packagist.org/packages/tobento/service-error-handler)[ Docs](https://www.tobento.ch)[ RSS](/packages/tobento-service-error-handler/feed)WikiDiscussions 2.x Synced 1mo ago

READMEChangelog (6)Dependencies (8)Versions (8)Used By (2)

Error Handler Service
=====================

[](#error-handler-service)

The Error Handler Service provides tools to manage errors and exceptions.

Table of Contents
-----------------

[](#table-of-contents)

- [Getting started](#getting-started)
    - [Requirements](#requirements)
    - [Highlights](#highlights)
- [Documentation](#documentation)
    - [Error Handling](#error-handling)
    - [Throwable Handlers](#throwable-handlers)
        - [Create Throwable Handlers](#create-throwable-handlers)
        - [Add Throwable Handler](#add-throwable-handler)
        - [Restrict Throwable Handler](#restrict-throwable-handler)
        - [Prioritize Throwable Handler](#prioritize-throwable-handler)
        - [Handle Throwable](#handle-throwable)
        - [Handlers](#handlers)
            - [Debug](#debug)
            - [Errors](#errors)
            - [Log](#log)
- [Credits](#credits)

---

Getting started
===============

[](#getting-started)

Add the latest version of the error handler service project running this command.

```
composer require tobento/service-error-handler

```

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

[](#requirements)

- PHP 8.4 or greater

Highlights
----------

[](#highlights)

- Framework-agnostic, will work with any project
- Decoupled design

Documentation
=============

[](#documentation)

Error Handling
--------------

[](#error-handling)

Check out [Throwable Handlers](#throwable-handlers) to learn more about the Throwable Handlers.

```
use Tobento\Service\ErrorHandler\ErrorHandling;
use Tobento\Service\ErrorHandler\ThrowableHandlers;
use Tobento\Service\ErrorHandler\ThrowableHandlerFactory;
use Tobento\Service\ErrorHandler\Handler;

$throwableHandlers = new ThrowableHandlers(new ThrowableHandlerFactory());

// adding any handler:
// $throwableHandlers->add(ValidationExceptionHandler::class);
// $throwableHandlers->add(GeneralExceptionHandler::class);

// only on development:
$throwableHandlers->add(Handler\Debug::class);

// adding last:
$throwableHandlers->add(Handler\Errors::class);

(new ErrorHandling($throwableHandlers))->register();
```

Throwable Handlers
------------------

[](#throwable-handlers)

The throwable handlers can be used where you need to handle exceptions in general.

### Create Throwable Handlers

[](#create-throwable-handlers)

```
use Tobento\Service\ErrorHandler\ErrorHandling;
use Tobento\Service\ErrorHandler\ThrowableHandlers;
use Tobento\Service\ErrorHandler\ThrowableHandlersInterface;
use Tobento\Service\ErrorHandler\ThrowableHandlerFactory;

$throwableHandlers = new ThrowableHandlers(new ThrowableHandlerFactory());

var_dump($throwableHandlers instanceof ThrowableHandlersInterface);
// bool(true)
```

**With autowiring factory**

The autowiring factory is needed if you [Add Throwable Handlers](#add-throwable-handler) with dependencies.

```
use Tobento\Service\ErrorHandler\ErrorHandling;
use Tobento\Service\ErrorHandler\ThrowableHandlers;
use Tobento\Service\ErrorHandler\ThrowableHandlersInterface;
use Tobento\Service\ErrorHandler\AutowiringThrowableHandlerFactory;

// Any PSR-11 container
$container = new \Tobento\Service\Container\Container();

$throwableHandlers = new ThrowableHandlers(
    new AutowiringThrowableHandlerFactory($container)
);

var_dump($throwableHandlers instanceof ThrowableHandlersInterface);
// bool(true)
```

### Add Throwable Handler

[](#add-throwable-handler)

**By class instance**

```
$throwableHandlers->add(new Handler\Errors());
```

**By class name**

```
$throwableHandlers->add(Handler\Errors::class);
```

**By class name with build-in parameters (not resolvable by autowiring)**

```
$throwableHandlers->add([ThrowableHandler::class, 'name' => 'value']);
```

**By anonymous function**

```
use Throwable;

$throwableHandlers->add(function(Throwable $t): mixed {
    // Return throwable if cannot handle, otherwise anything else.
    return $t;
});
```

### Restrict Throwable Handler

[](#restrict-throwable-handler)

**By error level(s)**

The throwable handler will only be used on the specified error levels.

```
$throwableHandlers->add(ThrowableHandler::class)
                  ->level(\E_USER_WARNING, \E_WARNING);
```

**By exception(s)**

The throwable handler will only be used on the specified exceptions.

```
$throwableHandlers->add(ThrowableHandler::class)
                  ->handles(SomeException::class, AnotherException::class);
```

### Prioritize Throwable Handler

[](#prioritize-throwable-handler)

You might want to prioritize the excution order of the handlers by the following way (highest first):

```
$throwableHandlers->add(ThrowableHandler::class)
                  ->priority(1000); // is default
```

### Handle Throwable

[](#handle-throwable)

```
use Throwable;

try {
    // do something
} catch (Throwable $t) {
    // do something with the response:
    $response = $throwableHandlers->handleThrowable($t);
}
```

### Handlers

[](#handlers)

#### Debug

[](#debug)

The debug handler will render a debugging page on any exception not caught.

```
use Tobento\Service\ErrorHandler\Handler\Debug;
use Tobento\Service\View\ViewInterface;
use Tobento\Service\ErrorHandler\ThrowableHandlerInterface;

$debug = new Debug(
    view: null, // null|ViewInterface
);

var_dump($debug instanceof ThrowableHandlerInterface);
// bool(true)
```

**Custom view**

Check out [View Service](https://github.com/tobento-ch/service-view) to learn more about the View in general.

```
private/
    view/
        debug/
            error.php
            throwable.php

```

```
use Tobento\Service\ErrorHandler\Handler\Debug;
use Tobento\Service\View\ViewInterface;
use Tobento\Service\View\View;
use Tobento\Service\View\PhpRenderer;
use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\Dir;

$view = new View(
    new PhpRenderer(
        new Dirs(
            new Dir('/private/view'),
        )
    )
);

$debug = new Debug(
    view: $view, // null|ViewInterface
);
```

#### Errors

[](#errors)

The errors handler will render an error page on shutdown.

```
use Tobento\Service\ErrorHandler\Handler\Errors;
use Tobento\Service\View\ViewInterface;
use Tobento\Service\ErrorHandler\ThrowableHandlerInterface;

$errors = new Errors(
    view: null, // null|ViewInterface
);

var_dump($errors instanceof ThrowableHandlerInterface);
// bool(true)
```

**Custom error view**

Check out [View Service](https://github.com/tobento-ch/service-view) to learn more about the View in general.

```
private/
    view/
        error.php

```

```
use Tobento\Service\ErrorHandler\Handler\Errors;
use Tobento\Service\View\ViewInterface;
use Tobento\Service\View\View;
use Tobento\Service\View\PhpRenderer;
use Tobento\Service\Dir\Dirs;
use Tobento\Service\Dir\Dir;

$view = new View(
    new PhpRenderer(
        new Dirs(
            new Dir('/private/view'),
        )
    )
);

$errors = new Errors(
    view: $view, // null|ViewInterface
);
```

#### Log

[](#log)

The log handler will log any errors or exceptions.

```
use Tobento\Service\ErrorHandler\Handler\Log;
use Psr\Log\LoggerInterface;
use Monolog\Logger;
use Monolog\Handler\TestHandler;
use Tobento\Service\ErrorHandler\ThrowableHandlerInterface;

$logger = new Logger('name');
$logger->pushHandler(new TestHandler());

$log = new Log(
    logger: $logger, // Closure|LoggerInterface
);

var_dump($log instanceof ThrowableHandlerInterface);
// bool(true)
```

**Example with Closure and throwable handlers**

```
use Tobento\Service\ErrorHandler\Handler\Log;
use Psr\Log\LoggerInterface;
use Monolog\Logger;
use Monolog\Handler\TestHandler;

$throwableHandlers->add(new Log(function(): LoggerInterface {
    $logger = new Logger('name');
    $testHandler = new TestHandler();
    $logger->pushHandler($testHandler);
    return $logger;
}))->levels(E_ERROR, E_CORE_ERROR);
```

Credits
=======

[](#credits)

- [Tobias Strub](https://www.tobento.ch)
- [All Contributors](../../contributors)

###  Health Score

43

—

FairBetter than 91% of packages

Maintenance63

Regular maintenance activity

Popularity15

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity71

Established project with proven stability

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

Recently: every ~259 days

Total

8

Last Release

228d ago

Major Versions

1.x-dev → 2.02025-09-24

PHP version history (2 changes)1.0.0PHP &gt;=8.0

2.0PHP &gt;=8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/055d6a1b5c2384bb179c75ab0b55914231d898fdc4dffeb30770f81200e52206?d=identicon)[TOBENTOch](/maintainers/TOBENTOch)

---

Top Contributors

[![tobento-ch](https://avatars.githubusercontent.com/u/16684832?v=4)](https://github.com/tobento-ch "tobento-ch (11 commits)")

---

Tags

phppackageexceptionserror-handlertobento

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[php-console/php-console

PHP library for Google Chrome extension "PHP Console".

1.3k2.9M41](/packages/php-console-php-console)[php-console/laravel-service-provider

Laravel service provider to handle PHP errors, dump variables, execute PHP code remotely in Google Chrome

7361.2k1](/packages/php-console-laravel-service-provider)

PHPackages © 2026

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