PHPackages                             middlewares/json-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. [HTTP &amp; Networking](/categories/http)
4. /
5. middlewares/json-exception-handler

ActiveLibrary[HTTP &amp; Networking](/categories/http)

middlewares/json-exception-handler
==================================

Middleware to catch exceptions and output them as JSON

v1.0.0(1y ago)461MITPHPPHP ^7.2 || ^8.0CI failing

Since Mar 22Pushed 1y ago7 watchersCompare

[ Source](https://github.com/middlewares/json-exception-handler)[ Packagist](https://packagist.org/packages/middlewares/json-exception-handler)[ Docs](https://github.com/middlewares/json-exception-handler)[ RSS](/packages/middlewares-json-exception-handler/feed)WikiDiscussions master Synced 1mo ago

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

middlewares/json-exception-handler
==================================

[](#middlewaresjson-exception-handler)

[![Latest Version on Packagist](https://camo.githubusercontent.com/88cf1b3ff3106e848f3cb1b5b3bba697c58e7ade5ce0139ca0d74715dd7fb4cc/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6964646c6577617265732f6a736f6e2d657863657074696f6e2d68616e646c65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/json-exception-handler)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Testing](https://github.com/middlewares/json-exception-handler/workflows/testing/badge.svg)](https://github.com/middlewares/json-exception-handler/workflows/testing/badge.svg)[![Total Downloads](https://camo.githubusercontent.com/b325238e0cae4d01e4d0c6adf119643ce88f19b244685060df09d1948d539001/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6d6964646c6577617265732f6a736f6e2d657863657074696f6e2d68616e646c65722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/middlewares/json-exception-handler)

Catches exceptions that occur during request handling and output them as JSON.

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

[](#requirements)

- PHP &gt;= 7.2
- A [PSR-7](https://packagist.org/providers/psr/http-message-implementation) http message implementation ([Diactoros](https://github.com/zendframework/zend-diactoros), [Guzzle](https://github.com/guzzle/psr7), [Slim](https://github.com/slimphp/Slim), etc...)
- A [PSR-15 middleware dispatcher](https://github.com/middlewares/awesome-psr15-middlewares#dispatcher)

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

[](#installation)

This package is installable and autoloadable via Composer as [middlewares/json-exception-handler](https://packagist.org/packages/middlewares/json-exception-handler).

```
composer require middlewares/json-exception-handler
```

JsonExceptionHandler
--------------------

[](#jsonexceptionhandler)

Catches exceptions thrown that occur later in request processing an creates a new response with HTTP 500 status and JSON encoded version of the exception as the body.

### `contentType(string $type)`

[](#contenttypestring-type)

Change the Content-Type header of the response. Default is `application/json`.

### `includeTrace(bool $enable)`

[](#includetracebool-enable)

Enable or disable the stack trace in the response. Default is `true`.

### `jsonOptions(int $options)`

[](#jsonoptionsint-options)

Set options for `json_encode` of the exception. Default is `0`.

---

Please see [CHANGELOG](CHANGELOG.md) for more information about recent changes and [CONTRIBUTING](CONTRIBUTING.md) for contributing details.

The MIT License (MIT). Please see [LICENSE](LICENSE) for more information.

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance46

Moderate activity, may be stable

Popularity9

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 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

Unknown

Total

1

Last Release

422d ago

### Community

Maintainers

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

![](https://www.gravatar.com/avatar/42e0d72f42eb7d84f67e20d28606da42e5a3248ca908b1eadb4366aafeae2561?d=identicon)[filisko](/maintainers/filisko)

---

Top Contributors

[![oscarotero](https://avatars.githubusercontent.com/u/377873?v=4)](https://github.com/oscarotero "oscarotero (5 commits)")[![filisko](https://avatars.githubusercontent.com/u/8798694?v=4)](https://github.com/filisko "filisko (3 commits)")[![shadowhand](https://avatars.githubusercontent.com/u/38203?v=4)](https://github.com/shadowhand "shadowhand (2 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (1 commits)")

---

Tags

exceptionhandlerjsonmiddlewarephppsr-15httppsr-7jsonmiddlewareserverexceptionhandlerpsr-15

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

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

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

###  Alternatives

[middlewares/request-handler

Middleware to execute request handlers

451.6M26](/packages/middlewares-request-handler)[middlewares/error-handler

Middleware to handle http errors

14104.2k13](/packages/middlewares-error-handler)[middlewares/payload

Middleware to parse the body of the request with support for json, csv and url-encode

32466.8k17](/packages/middlewares-payload)[middlewares/fast-route

Middleware to use FastRoute

96191.1k15](/packages/middlewares-fast-route)[middlewares/negotiation

Middleware to implement content negotiation

47442.1k11](/packages/middlewares-negotiation)[middlewares/http-authentication

Middleware to implement Basic and Digest Http authentication

35302.0k2](/packages/middlewares-http-authentication)

PHPackages © 2026

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