middlewares / json-exception-handler
Middleware to catch exceptions and output them as JSON
Installs: 5
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 7
Forks: 1
Open Issues: 0
pkg:composer/middlewares/json-exception-handler
Requires
- php: ^7.2 || ^8.0
- middlewares/utils: ^2 || ^3 || ^4
- psr/http-server-middleware: ^1
- thecodingmachine/safe: ^1 || ^2 || ^3
- wyrihaximus/json-throwable: ^2 || ^3 || ^4
Requires (Dev)
- ext-json: *
- friendsofphp/php-cs-fixer: ^3
- laminas/laminas-diactoros: ^2 || ^3
- oscarotero/php-cs-fixer-config: ^2
- phpstan/phpstan: ^1 || ^2
- phpunit/phpunit: ^8 || ^9
- squizlabs/php_codesniffer: ^3
This package is auto-updated.
Last update: 2025-10-25 10:13:04 UTC
README
Catches exceptions that occur during request handling and output them as JSON.
Requirements
- PHP >= 7.2
- A PSR-7 http message implementation (Diactoros, Guzzle, Slim, etc...)
- A PSR-15 middleware dispatcher
Installation
This package is installable and autoloadable via Composer as middlewares/json-exception-handler.
composer require middlewares/json-exception-handler
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)
Change the Content-Type header of the response. Default is application/json.
includeTrace(bool $enable)
Enable or disable the stack trace in the response. Default is true.
jsonOptions(int $options)
Set options for json_encode of the exception. Default is 0.
Please see CHANGELOG for more information about recent changes and CONTRIBUTING for contributing details.
The MIT License (MIT). Please see LICENSE for more information.