Download the PHP package andrewdyer/json-error-handler without Composer
On this page you can find all versions of the php package andrewdyer/json-error-handler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download andrewdyer/json-error-handler
More information about andrewdyer/json-error-handler
Files in andrewdyer/json-error-handler
Package json-error-handler
Short Description A structured JSON error handler for Slim Framework applications that maps exceptions to typed, consistent error payloads
License MIT
Homepage https://github.com/andrewdyer/json-error-handler
Informations about the package json-error-handler
JSON Error Handler
A structured JSON error handler for Slim Framework applications that maps exceptions to typed, consistent error payloads.
Introduction
This library extends Slim Framework's built-in error handling to intercept exceptions and transform them into well-formed JSON responses with appropriate HTTP status codes. It supports optional error detail exposure for debug environments and integrates directly with Slim's error middleware. For shutdown handling workflows, it can be paired with andrewdyer/shutdown-handler to keep unhandled error responses consistent.
Prerequisites
- PHP: Version 8.3 or higher is required.
- Composer: Dependency management tool for PHP.
- Slim Framework: Version 4 is required.
Installation
Getting Started
1. Create the application
2. Add error middleware
Add the error middleware and set JsonErrorHandler as the default handler. The $displayErrorDetails flag controls whether exception messages are included in responses — this should be false in production:
Note: A PSR-3 logger can be passed as the third argument to enable error logging. Monolog is a popular choice for this.
By default, payloads are encoded with JSON_PRETTY_PRINT. Custom flags can be passed as the fourth constructor argument:
Note that JSON_THROW_ON_ERROR is always masked out internally to prevent encoding failures from cascading during error handling.
3. Register routes
Register routes to handle incoming requests:
4. Run the application
Start the application to begin handling incoming HTTP requests:
Usage
Once the handler is registered, Slim will route exceptions through JsonErrorHandler and return structured JSON error responses.
Successful request
Response: 200 OK
Error request
Response: 404 Not Found
Advanced Usage
Shutdown handler integration
For complete fatal error coverage — including errors that occur outside of Slim's request lifecycle — JsonErrorHandler can be integrated with andrewdyer/shutdown-handler:
Wrap JsonErrorHandler in a CallableErrorResponder and register a ShutdownHandler before running the application:
Refer to the shutdown-handler documentation for full details on implementing a response emitter.
License
Licensed under the MIT license and is free for private or commercial projects.
All versions of json-error-handler with dependencies
andrewdyer/actions Version ^1.3
psr/http-factory Version ^1.1
psr/http-message Version ^1.1 || ^2.0
psr/log Version ^1.1 || ^2.0 || ^3.0
slim/slim Version ^4.15