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

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

wubinworks/error-handler
========================

Error Handler converts PHP's traditional runtime errors into ErrorException and respects user specified error reporting level. Exception Handler can output and/or log exception message with well-formatted stack trace.

1.0.0(yesterday)01↑2900%OSL-3.0

Since Jul 22Compare

[ Source](https://github.com/wubinworks/error-handler)[ Packagist](https://packagist.org/packages/wubinworks/error-handler)[ Docs](https://www.wubinworks.com)[ RSS](/packages/wubinworks-error-handler/feed)WikiDiscussions Synced today

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

PHP Error Handler and Exception Handler
=======================================

[](#php-error-handler-and-exception-handler)

This package contains `\Wubinworks\ErrorHandler\ErrorHandler` and `\Wubinworks\ErrorHandler\ExceptionHandler`.

The `\Wubinworks\ErrorHandler\ErrorHandler::handle` converts PHP's *traditional runtime errors* into `\ErrorException` and respects user specified error reporting level.

`\Wubinworks\ErrorHandler\ErrorHandler::handle` can be used as a callback for `set_error_handler` directly.

The `\Wubinworks\ErrorHandler\ExceptionHandler::handle` handles any `\Throwable` and controls whether to output exception message and logging. You can also get the exception message with well-formatted stack trace information as string.

`\Wubinworks\ErrorHandler\ExceptionHandler::handle` can be used as a callback for `set_exception_handler` directly.

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

[](#requirements)

- PHP &gt;= 5.3

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

[](#installation)

```
composer require wubinworks/error-handler
```

Usage
-----

[](#usage)

### `ErrorHandler`

[](#errorhandler)

Just `set_error_handler([new \Wubinworks\ErrorHandler\ErrorHandler(), 'handle'])`.

This handler respects the [error\_reporting](https://www.php.net/manual/en/function.error-reporting.php) level set by user and if the *traditional runtime error* needs to be reported, an `\ErrorException` will be thrown. The `\ErrorException`'s severity level is set to the handled error level.

Example:

```
// Set the error reporting level you prefer
$previousErrorReportingLevel = error_reporting(/** This is just an example level */ E_ALL & ~E_WARNING);

set_error_handler([new \Wubinworks\ErrorHandler\ErrorHandler(), 'handle']);
try {
    // Code like `fopen` that can trigger a traditional runtime error
    // Here we use `trigger_error` for convenience
    trigger_error('The error message.', E_USER_NOTICE);
} catch (\ErrorException $e) {
    var_dump($e->getMessage());
    // The error message.
    var_dump($e->getSeverity());
    // Value of predefined constant E_USER_NOTICE(1024)
}

// Error control operator
@trigger_error('You will not see this message. No error, no exception.', E_USER_NOTICE);

restore_error_handler();
error_reporting($previousErrorReportingLevel);
```

### `ExceptionHandler`

[](#exceptionhandler)

`\Wubinworks\ErrorHandler\ExceptionHandler::__construct` has 3 parameters and these parameters control the handler's behavior.

- `canOutput`Controls whether the handler can output to console/browser.
- `logger`Must be type `null|\Psr\Log\LoggerInterface`. Set to `null` to disable logging. Note the log level is fixed to [`ERROR`](https://github.com/php-fig/log/blob/f16e1d5863e37f8d8c2a01719f5b34baa2b714d3/src/LogLevel.php#L13).
- `isTraceIgnoreArgs`Controls whether to include function/method parameter information in the output/log.

Example:

```
try {
    ...
    if (...) {
        throw new \Exception('The exception message.');
    }
    ...
} catch (\Throwable $e) {
    $logger = new \Monolog\Logger('example-logger');
    $logger->pushHandler(new \Monolog\Handler\StreamHandler(''));
    $exceptionHandler = new \Wubinworks\ErrorHandler\ExceptionHandler(
        false, // No output
        $logger, // PSR-3 Logger Interface
        false // Stack trace does not include function/method parameters
    );

    var_dump($exceptionHandler->getMessage($e));
    // Exception message + exception class + stack trace
    $exceptionHandler->handle($e);
    // No console/browser output but the exception message and stack trace information should have been written to .
}
```

You can also do like this:

```
// Replace the PHP's built-in exception handler
set_exception_handler([new \Wubinworks\ErrorHandler\ExceptionHandler(true, null, false), 'handle']);
```

***The output/log include exception message, exception class and well-formatted stack trace information.***

Example output/log:

```
Exception: Test exception message in file /project/ErrorHandler/tests/Test/Unit/ExceptionHandlerTest.php on line 75
Stack trace:
  1. Exception->() /project/ErrorHandler/tests/Test/Unit/ExceptionHandlerTest.php:75
  2. Wubinworks\ErrorHandler\Test\Unit\ExceptionHandlerTest->testHandleAndGetMessage() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestCase.php:1311
  3. PHPUnit\Framework\TestCase->invokeTestMethod() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestCase.php:1327
  4. PHPUnit\Framework\TestCase->runTest() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestCase.php:452
  5. PHPUnit\Framework\TestCase->runBare() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestRunner/TestRunner.php:110
  6. PHPUnit\Framework\TestRunner\TestRunner->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestCase.php:301
  7. PHPUnit\Framework\TestCase->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestSuite.php:381
  8. PHPUnit\Framework\TestSuite->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestSuite.php:381
  9. PHPUnit\Framework\TestSuite->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestSuite.php:381
 10. PHPUnit\Framework\TestSuite->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/Framework/TestSuite.php:381
 11. PHPUnit\Framework\TestSuite->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:64
 12. PHPUnit\TextUI\TestRunner->run() /project/ErrorHandler/vendor/phpunit/phpunit/src/TextUI/Application.php:254
 13. PHPUnit\TextUI\Application->run() /project/ErrorHandler/vendor/phpunit/phpunit/phpunit:104
 14. include() /project/ErrorHandler/vendor/bin/phpunit:122

```

Unit testing
------------

[](#unit-testing)

Install the `require-dev` dependencies and run the following command.

```
vendor/bin/phpunit
```

♥
-

[](#)

If you like this package or this package helped you, please ***share*** and ***give a ★☆star☆★***, it's NOT hard!

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance100

Actively maintained with recent releases

Popularity2

Limited adoption so far

Community2

Small or concentrated contributor base

Maturity33

Early-stage or recently created project

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

Unknown

Total

1

Last Release

1d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/7de965a6287fb784969afeb4b173521d3cb59c6b873b7248263abb9fc098eddd?d=identicon)[wubinworks](/maintainers/wubinworks)

---

Tags

logerror-reportingerror-handlerstack traceexception handlererrorexceptionset\_error\_handlerset\_exception\_handlertraditional runtime errorerror levelerror controlerror suppressionwell formatted

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[sentry/sentry

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

1.9k247.1M346](/packages/sentry-sentry)[sentry/sentry-laravel

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

1.3k127.1M214](/packages/sentry-sentry-laravel)[sentry/sdk

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

327139.0M168](/packages/sentry-sdk)[e2ex/e2ex

Converts PHP Errors to Exceptions and (optionally) logs PHP Errors, Notices and Warnings with a PSR-3 compatible logger

101.5k](/packages/e2ex-e2ex)

PHPackages © 2026

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