PHPackages                             yiisoft/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. [Framework](/categories/framework)
4. /
5. yiisoft/error-handler

ActiveLibrary[Framework](/categories/framework)

yiisoft/error-handler
=====================

Yii Error Handling Library

4.3.2(4mo ago)21363.6k—1.1%18[6 PRs](https://github.com/yiisoft/error-handler/pulls)15BSD-3-ClausePHPPHP 8.1 - 8.5CI passing

Since May 13Pushed 1mo ago14 watchersCompare

[ Source](https://github.com/yiisoft/error-handler)[ Packagist](https://packagist.org/packages/yiisoft/error-handler)[ Docs](https://www.yiiframework.com/)[ GitHub Sponsors](https://github.com/sponsors/yiisoft)[ OpenCollective](https://opencollective.com/yiisoft)[ RSS](/packages/yiisoft-error-handler/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (21)Versions (26)Used By (15)

 [ ![Yii](https://camo.githubusercontent.com/8317c17418b39410a660f5149071d26c5023c0d5fb2b7ebb771324812f666d73/68747470733a2f2f796969736f66742e6769746875622e696f2f646f63732f696d616765732f7969695f6c6f676f2e737667) ](https://github.com/yiisoft)

Yii Error Handler
=================

[](#yii-error-handler)

[![Latest Stable Version](https://camo.githubusercontent.com/2dda68a1b229c4fccbe1a9e63983cdc5cd71d8259d70e7320ccb00749022b73f/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f6572726f722d68616e646c65722f76)](https://packagist.org/packages/yiisoft/error-handler)[![Total Downloads](https://camo.githubusercontent.com/7efddb94fa5c69a99d03557bf6e879bafcde7b709a69ea384071b765ee2d7386/68747470733a2f2f706f7365722e707567782e6f72672f796969736f66742f6572726f722d68616e646c65722f646f776e6c6f616473)](https://packagist.org/packages/yiisoft/error-handler)[![Build status](https://github.com/yiisoft/error-handler/actions/workflows/build.yml/badge.svg)](https://github.com/yiisoft/error-handler/actions/workflows/build.yml)[![Code coverage](https://camo.githubusercontent.com/f585c44b7c181f8ecaff25b8d7b1f9980ab477334c6c8c3aa5a56ae004ffa4d1/68747470733a2f2f636f6465636f762e696f2f67682f796969736f66742f6572726f722d68616e646c65722f67726170682f62616467652e7376673f746f6b656e3d335a433051485148494e)](https://codecov.io/gh/yiisoft/error-handler)[![Mutation testing badge](https://camo.githubusercontent.com/3a4a4c3250f7e17b03dd137d4837fade42184396bc9bda39097f4e64bec4225d/68747470733a2f2f696d672e736869656c64732e696f2f656e64706f696e743f7374796c653d666c61742675726c3d687474707325334125324625324662616467652d6170692e737472796b65722d6d757461746f722e696f2532466769746875622e636f6d253246796969736f66742532466572726f722d68616e646c65722532466d6173746572)](https://dashboard.stryker-mutator.io/reports/github.com/yiisoft/error-handler/master)[![static analysis](https://github.com/yiisoft/error-handler/workflows/static%20analysis/badge.svg)](https://github.com/yiisoft/error-handler/actions?query=workflow%3A%22static+analysis%22)

The package provides advanced error handling. The features are:

- PSR-15 middleware for catching unhandled errors.
- PSR-15 middleware for mapping certain exceptions to custom responses.
- Production and debug modes.
- Debug mode displays details, stacktrace, has dark and light themes and handy buttons to search for error without typing.
- Takes PHP settings into account.
- Handles out of memory errors, fatals, warnings, notices and exceptions.
- Can use any PSR-3 compatible logger for error logging.
- Detects response format based on mime type of the request.
- Supports responding with HTML, plain text, JSON, XML and headers out of the box.
- Has ability to implement your own error rendering for additional types.
- [Friendly exceptions](https://github.com/yiisoft/friendly-exception/) support.

   ![Error handler](docs/images/view-development-light.png)Requirements
------------

[](#requirements)

- PHP 8.1 - 8.5.
- `DOM` PHP extension.
- `mbstring` PHP extension.

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

[](#installation)

The package could be installed with [Composer](https://getcomposer.org):

```
composer require yiisoft/error-handler
```

General usage
-------------

[](#general-usage)

Creating an error handler:

```
use Yiisoft\ErrorHandler\ErrorHandler;
use Yiisoft\ErrorHandler\Renderer\HtmlRenderer;

/**
 * @var \Psr\Log\LoggerInterface $logger
 */

$errorHandler = new ErrorHandler($logger, new HtmlRenderer());
```

The error handler logs information about the error using any [PSR-3](https://www.php-fig.org/psr/psr-3/)compatible logger. If for some reason you do not want to log error information, specify an instance of the `\Psr\Log\NullLogger`.

By default, the error handler is set to production mode and displays no detailed information. You can enable and disable debug mode as follows:

```
// Enable debug mode:
$errorHandler->debug();

// Disable debug mode:
$errorHandler->debug(false);

// Or define the environment dynamically:
$errorHandler->debug($_ENV['debug'] ?? false);
```

The error handler handles out-of-memory errors. To achieve it, memory is pre-allocated so that if a problem occurs with a lack of memory, the error handler can handle the error using this reserved memory. You can specify your own reserve size using the `memoryReserveSize()` method. If you set this value to 0, no memory will be reserved.

```
// Allocate 512KB. Defaults to 256KB.
$errorHandler->memoryReserveSize(524_288);
```

The `register()` method registers the PHP error and exception handlers. To unregister these and restore the PHP error and exception handlers, use the `unregister()` method.

```
$errorHandler->register();
// Errors are being handled.
$errorHandler->unregister();
// Errors are not handled.
```

### Rendering error data

[](#rendering-error-data)

The following renderers are available out of the box:

- `Yiisoft\ErrorHandler\Renderer\HeaderRenderer` - Renders error into HTTP headers. It is used for HEAD requests.
- `Yiisoft\ErrorHandler\Renderer\HtmlRenderer` - Renders error into HTML.
- `Yiisoft\ErrorHandler\Renderer\JsonRenderer` - Renders error into JSON.
- `Yiisoft\ErrorHandler\Renderer\PlainTextRenderer` - Renders error into plain text.
- `Yiisoft\ErrorHandler\Renderer\XmlRenderer` - Renders error into XML.

If the existing renderers are not enough, you can create your own. To do this, you must implement the `Yiisoft\ErrorHandler\ThrowableRendererInterface` and specify it when creating an instance of the error handler.

```
use Yiisoft\ErrorHandler\ErrorHandler;

/**
 * @var \Psr\Log\LoggerInterface $logger
 * @var \Yiisoft\ErrorHandler\ThrowableRendererInterface $renderer
 */

$errorHandler = new ErrorHandler($logger, $renderer);
```

For more information about creating your own renders and examples of rendering error data, [see here](https://yiisoft.github.io/docs/guide/runtime/handling-errors.html#rendering-error-data).

### Using a factory to create a response

[](#using-a-factory-to-create-a-response)

`Yiisoft\ErrorHandler\ThrowableResponseFactory` renders `Throwable` object and produces a response according to the content type provided by the client.

```
use Yiisoft\ErrorHandler\RendererProvider;
use Yiisoft\ErrorHandler\ThrowableResponseFactory;

/**
 * @var \Throwable $throwable
 * @var \Psr\Container\ContainerInterface $container
 * @var \Psr\Http\Message\ResponseFactoryInterface $responseFactory
 * @var \Psr\Http\Message\ServerRequestInterface $request
 * @var \Yiisoft\ErrorHandler\ErrorHandler $errorHandler
 */

$throwableResponseFactory = new ThrowableResponseFactory(
    $responseFactory,
    $errorHandler,
    new RendererProvider\CompositeRendererProvider(
        new RendererProvider\HeadRendererProvider(),
        new RendererProvider\ContentTypeRendererProvider($container),
    ),
);

// Creating an instance of the `Psr\Http\Message\ResponseInterface` with error information.
$response = $throwableResponseFactory->create($throwable, $request);
```

`Yiisoft\ErrorHandler\ThrowableResponseFactory` chooses how to render an exception by renderer provider. Providers available out of the box:

- `HeadRendererProvider` - renders error into HTTP headers. It is used for HEAD requests.
- `ContentTypeRendererProvider` - renders error based on accept HTTP header. By default, JSON, XML and plain text are supported.
- `ClosureRendererProvider` - allows you to create your own renderer provider using closures.
- `CompositeRendererProvider` - allows you to combine several renderer providers.

### Using a middleware for catching unhandled errors

[](#using-a-middleware-for-catching-unhandled-errors)

`Yiisoft\ErrorHandler\Middleware\ErrorCatcher` is a [PSR-15](https://www.php-fig.org/psr/psr-15/) middleware that catches exceptions raised during middleware stack execution and passes them to the instance of `Yiisoft\ErrorHandler\ThrowableResponseFactoryInterface` to create a response.

```
use Yiisoft\ErrorHandler\Middleware\ErrorCatcher;

/**
 * @var \Psr\EventDispatcher\EventDispatcherInterface $eventDispatcher
 * @var \Psr\Http\Message\ServerRequestInterface $request
 * @var \Psr\Http\Server\RequestHandlerInterface $handler
 * @var \Yiisoft\ErrorHandler\ThrowableResponseFactoryInterface $throwableResponseFactory
 */

$errorCatcher = new ErrorCatcher($throwableResponseFactory);

// In any case, it will return an instance of the `Psr\Http\Message\ResponseInterface`.
// Either the expected response, or a response with error information.
$response = $errorCatcher->process($request, $handler);
```

`Yiisoft\ErrorHandler\Middleware\ErrorCatcher` can be instantiated with [PSR-14](https://www.php-fig.org/psr/psr-14/) event dispatcher as an optional dependency. In this case `\Yiisoft\ErrorHandler\Event\ApplicationError` will be dispatched when `ErrorCatcher` catches an error.

```
$errorCatcher = new ErrorCatcher($throwableResponseFactory, $eventDispatcher);
```

### Using a middleware for mapping certain exceptions to custom responses

[](#using-a-middleware-for-mapping-certain-exceptions-to-custom-responses)

`Yiisoft\ErrorHandler\Middleware\ExceptionResponder` is a [PSR-15](https://www.php-fig.org/psr/psr-15/)middleware that maps certain exceptions to custom responses.

```
use Yiisoft\ErrorHandler\Middleware\ExceptionResponder;

/**
 * @var \Psr\Http\Message\ResponseFactoryInterface $responseFactory
 * @var \Psr\Http\Message\ServerRequestInterface $request
 * @var \Psr\Http\Server\RequestHandlerInterface $handler
 * @var \Yiisoft\Injector\Injector $injector
 */

$exceptionMap = [
    // Status code with which the response will be created by the factory.
    MyNotFoundException::class => 404,
    // PHP callable that must return a `Psr\Http\Message\ResponseInterface`.
    MyHttpException::class => static fn (MyHttpException $exception) => new MyResponse($exception),
    // ...
];

$exceptionResponder = new ExceptionResponder($exceptionMap, $responseFactory, $injector);

// Returns the expected response, or the response associated with the thrown exception,
// or throws an exception if it does not present in the exception map.
$response = $exceptionResponder->process($request, $handler);
```

In the application middleware stack `Yiisoft\ErrorHandler\Middleware\ExceptionResponder` must be placed before `Yiisoft\ErrorHandler\Middleware\ErrorCatcher`.

Events
------

[](#events)

- When `ErrorCatcher` catches an error it optionally dispatches `\Yiisoft\ErrorHandler\Event\ApplicationError` event. Instance of `Psr\EventDispatcher\EventDispatcherInterface` must be provided to the `ErrorCatcher`.

Friendly Exceptions
-------------------

[](#friendly-exceptions)

`HtmlRenderer` supports [friendly exceptions](https://github.com/yiisoft/friendly-exception/).

Code blocks in solution markdown support language syntax highlight:

LanguageAliasesBashbash, sh, zshCSScssHTML, XMLxml, html, xhtml, rss, atom, xjb, xsd, xsl, plist, svgJavaScriptjavascript, js, jsxJSONjsonPHPphpPlaintextplaintext, txt, textSQLsqlFor example:

```

    This text is normal.
    This text is bold.

```

Documentation
-------------

[](#documentation)

- [Yii guide to handling errors](https://yiisoft.github.io/docs/guide/runtime/handling-errors.html)
- [Internals](docs/internals.md)

If you need help or have a question, the [Yii Forum](https://forum.yiiframework.com/c/yii-3-0/63) is a good place for that. You may also check out other [Yii Community Resources](https://www.yiiframework.com/community).

License
-------

[](#license)

The Yii Error Handler is free software. It is released under the terms of the BSD License. Please see [`LICENSE`](./LICENSE.md) for more information.

Maintained by [Yii Software](https://www.yiiframework.com/).

Credits
-------

[](#credits)

The Yii Error Handler use code of [Highlight.js](https://highlightjs.org/) by Ivan Sagalaev and other contributors.

Support the project
-------------------

[](#support-the-project)

[![Open Collective](https://camo.githubusercontent.com/a2b15f8e2268d4e3842e00d41ff7a57cce2ad8bd8d8769c5dc4fa05a546a4f62/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4f70656e253230436f6c6c6563746976652d73706f6e736f722d3765616466313f6c6f676f3d6f70656e253230636f6c6c656374697665266c6f676f436f6c6f723d376561646631266c6162656c436f6c6f723d353535353535)](https://opencollective.com/yiisoft)

Follow updates
--------------

[](#follow-updates)

[![Official website](https://camo.githubusercontent.com/d6b0929173e28cc627430d2519ca1853466a70f37395877eaf4820cb3e1e1909/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506f77657265645f62792d5969695f4672616d65776f726b2d677265656e2e7376673f7374796c653d666c6174)](https://www.yiiframework.com/)[![Twitter](https://camo.githubusercontent.com/d077c362ac639792171af8bc002ee827816733dfc0925f70b557e6d151022226/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f747769747465722d666f6c6c6f772d3144413146323f6c6f676f3d74776974746572266c6f676f436f6c6f723d314441314632266c6162656c436f6c6f723d3535353535353f7374796c653d666c6174)](https://twitter.com/yiiframework)[![Telegram](https://camo.githubusercontent.com/4e38dd12535575c39c65bea7119b95e663abb2d1f4e3d669a27bbda07ef603f0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f74656c656772616d2d6a6f696e2d3144413146323f7374796c653d666c6174266c6f676f3d74656c656772616d)](https://t.me/yii3en)[![Facebook](https://camo.githubusercontent.com/48204e301b34b29b0815854544f04c337fc0692096cab35e9a1f8c53a42c2307/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f66616365626f6f6b2d6a6f696e2d3144413146323f7374796c653d666c6174266c6f676f3d66616365626f6f6b266c6f676f436f6c6f723d666666666666)](https://www.facebook.com/groups/yiitalk)[![Slack](https://camo.githubusercontent.com/1a3645ba1c97e6684d0349bc478201e1621ba0d3efad516d81035364d442bad7/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736c61636b2d6a6f696e2d3144413146323f7374796c653d666c6174266c6f676f3d736c61636b)](https://yiiframework.com/go/slack)

###  Health Score

65

—

FairBetter than 99% of packages

Maintenance83

Actively maintained with recent releases

Popularity47

Moderate usage in the ecosystem

Community40

Growing community involvement

Maturity79

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

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

Recently: every ~66 days

Total

17

Last Release

130d ago

Major Versions

1.0.0 → 2.0.02021-11-09

2.1.1 → 3.0.02023-02-14

3.3.0 → 4.0.02025-02-05

PHP version history (4 changes)1.0.0PHP ^7.4|^8.0

3.0.0PHP ^8.0

4.0.0PHP ~8.1.0 || ~8.2.0 || ~8.3.0 || ~8.4.0

4.3.1PHP 8.1 - 8.5

### Community

Maintainers

![](https://www.gravatar.com/avatar/261a6249c6f605f3956a2fae40fbb813f6b2e1e6f2bf806180c851a965426e54?d=identicon)[cebe](/maintainers/cebe)

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

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

![](https://www.gravatar.com/avatar/99106256c24a8cb23871b99fa90e48f37f1aa71608c185759b7d2a88683a5918?d=identicon)[hiqsol](/maintainers/hiqsol)

---

Top Contributors

[![vjik](https://avatars.githubusercontent.com/u/525501?v=4)](https://github.com/vjik "vjik (65 commits)")[![xepozz](https://avatars.githubusercontent.com/u/6815714?v=4)](https://github.com/xepozz "xepozz (54 commits)")[![samdark](https://avatars.githubusercontent.com/u/47294?v=4)](https://github.com/samdark "samdark (32 commits)")[![devanych](https://avatars.githubusercontent.com/u/20116244?v=4)](https://github.com/devanych "devanych (22 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (8 commits)")[![rustamwin](https://avatars.githubusercontent.com/u/16498265?v=4)](https://github.com/rustamwin "rustamwin (7 commits)")[![StyleCIBot](https://avatars.githubusercontent.com/u/11048387?v=4)](https://github.com/StyleCIBot "StyleCIBot (7 commits)")[![luizcmarin](https://avatars.githubusercontent.com/u/67489841?v=4)](https://github.com/luizcmarin "luizcmarin (3 commits)")[![Arhell](https://avatars.githubusercontent.com/u/26163841?v=4)](https://github.com/Arhell "Arhell (2 commits)")[![olegbaturin](https://avatars.githubusercontent.com/u/15981018?v=4)](https://github.com/olegbaturin "olegbaturin (2 commits)")[![thenotsoft](https://avatars.githubusercontent.com/u/44147615?v=4)](https://github.com/thenotsoft "thenotsoft (2 commits)")[![pamparam83](https://avatars.githubusercontent.com/u/9422100?v=4)](https://github.com/pamparam83 "pamparam83 (1 commits)")[![reibengu](https://avatars.githubusercontent.com/u/4273878?v=4)](https://github.com/reibengu "reibengu (1 commits)")[![roxblnfk](https://avatars.githubusercontent.com/u/4152481?v=4)](https://github.com/roxblnfk "roxblnfk (1 commits)")[![mspirkov](https://avatars.githubusercontent.com/u/63721828?v=4)](https://github.com/mspirkov "mspirkov (1 commits)")[![epifrin](https://avatars.githubusercontent.com/u/1763649?v=4)](https://github.com/epifrin "epifrin (1 commits)")[![sankaest](https://avatars.githubusercontent.com/u/21160342?v=4)](https://github.com/sankaest "sankaest (1 commits)")[![DplusG](https://avatars.githubusercontent.com/u/11989901?v=4)](https://github.com/DplusG "DplusG (1 commits)")[![terabytesoftw](https://avatars.githubusercontent.com/u/42547589?v=4)](https://github.com/terabytesoftw "terabytesoftw (1 commits)")[![dbuhonov](https://avatars.githubusercontent.com/u/47827492?v=4)](https://github.com/dbuhonov "dbuhonov (1 commits)")

---

Tags

errorerror-handlinghacktoberfestmiddlewarepsr-15yii3psr-7psr-3PSR-11psr-15error-handleryiisoft

###  Code Quality

TestsPHPUnit

Static AnalysisPsalm, Rector

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[cakephp/cakephp

The CakePHP framework

8.8k18.5M1.6k](/packages/cakephp-cakephp)[chubbyphp/chubbyphp-framework

A minimal, highly performant middleware PSR-15 microframework built with as little complexity as possible, aimed primarily at those developers who want to understand all the vendors they use.

13544.4k4](/packages/chubbyphp-chubbyphp-framework)[neos/flow

Flow Application Framework

862.0M451](/packages/neos-flow)[yiisoft/yii-middleware

Yii Middleware

21151.3k1](/packages/yiisoft-yii-middleware)[neos/flow-development-collection

Flow packages in a joined repository for pull requests.

144179.3k3](/packages/neos-flow-development-collection)[windwalker/framework

The next generation PHP framework.

25639.1k1](/packages/windwalker-framework)

PHPackages © 2026

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