Download the PHP package ctw/ctw-middleware-httpexception without Composer
On this page you can find all versions of the php package ctw/ctw-middleware-httpexception. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ctw/ctw-middleware-httpexception
More information about ctw/ctw-middleware-httpexception
Files in ctw/ctw-middleware-httpexception
Package ctw-middleware-httpexception
Short Description This PSR-15 middleware catches exceptions implementing "Ctw\Http\HttpException\HttpException\HttpExceptionInterface" and returns a custom error page.
License BSD-3-Clause
Informations about the package ctw-middleware-httpexception
Package "ctw/ctw-middleware-httpexception"
PSR-15 middleware that catches HTTP exceptions and renders custom error pages for production environments, with automatic JSON support for API requests.
Introduction
Why This Library Exists
Web applications need graceful error handling that presents user-friendly error pages in production while preserving detailed error information during development. The default PHP error handling exposes sensitive information and provides poor user experience.
This middleware integrates with ctw/ctw-http exceptions to:
- Catch HTTP exceptions: Intercepts exceptions implementing
HttpExceptionInterface - Render custom templates: Uses Mezzio's template renderer for branded error pages
- Support JSON responses: Automatically returns RFC 7807 Problem Details for API requests
- Respect development mode: Re-throws exceptions in development for debugging
Problems This Library Solves
- Generic error pages: Default framework error pages are ugly and expose technical details
- Missing JSON errors: API clients receive HTML error pages instead of structured JSON
- Development vs production: Need different error handling behavior per environment
- Inconsistent responses: Different error codes handled inconsistently across the application
- Security exposure: Stack traces and internal paths leak in production errors
Where to Use This Library
- Production web applications: Present branded, user-friendly error pages
- REST APIs: Return RFC 7807 Problem Details JSON for all HTTP errors
- Hybrid applications: Automatically serve HTML or JSON based on
Acceptheader - Mezzio applications: Integrates seamlessly with the Mezzio template system
- Multi-environment deployments: Different behavior in development vs production
Design Goals
- Content negotiation: Serves HTML or JSON based on client
Acceptheader - RFC 7807 compliance: JSON errors follow Problem Details specification
- Template-based rendering: Uses Mezzio template renderer for HTML responses
- Development awareness: Re-throws exceptions when development mode is enabled
- Exception hierarchy: Only catches
HttpExceptionInterface, re-throws others
Requirements
- PHP 8.3 or higher
- ctw/ctw-middleware ^4.0
- ctw/ctw-http ^4.0
- mezzio/mezzio-template ^2.4
- mezzio/mezzio-laminasviewrenderer ^2.2
Installation
Install by adding the package as a Composer requirement:
Usage Examples
Basic Pipeline Registration (Mezzio)
ConfigProvider Registration
Throwing HTTP Exceptions
HTML Response (Browser)
When a browser requests a page and an exception occurs:
JSON Response (API)
When a client sends Accept: application/json:
Configuration Options
| Option | Description | Default |
|---|---|---|
template_http_exception |
Template name for HTML error pages | error::http-exception.phtml |
layout |
Layout template for error pages | (none) |
Template Variables
The error template receives:
| Variable | Type | Description |
|---|---|---|
entity |
HttpStatus |
HTTP status entity with code, name, phrase |
exception |
HttpExceptionInterface |
The caught exception |
layout |
string |
Layout template name (if configured) |
Development Mode
In development mode (when laminas/laminas-development-mode is enabled), exceptions are re-thrown to allow the Whoops error handler or similar tools to display detailed debugging information.
All versions of ctw-middleware-httpexception with dependencies
ctw/ctw-http Version ^5.0
laminas/laminas-diactoros Version ^3.0
mezzio/mezzio-template Version ^2.4
psr/container Version ^1.0 || ^2.0
psr/http-message Version ^1.1 || ^2.0
psr/http-server-handler Version ^1.0
psr/http-server-middleware Version ^1.0