Download the PHP package meritum/http-exception-handler without Composer
On this page you can find all versions of the php package meritum/http-exception-handler. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download meritum/http-exception-handler
More information about meritum/http-exception-handler
Files in meritum/http-exception-handler
Package http-exception-handler
Short Description meritum/http exception handler that translates exceptions into structured JSON error responses using the meritum/structured-logging pipeline
License MIT
Informations about the package http-exception-handler
meritum/http-exception-handler
HTTP exception handler that translates exceptions into structured JSON error responses using the meritum/structured-logging pipeline.
Requirements
- PHP 8.4+
georgeff/kernel^1.6meritum/http^1.0meritum/structured-logging^1.0
Installation
Usage
Module registration
Register ExceptionHandlerModule alongside StructuredLoggingModule. StructuredLoggingModule requires a LoggerInterface to already be registered:
ExceptionHandlerModule registers:
ExceptionHandlerInterface— the handlermeritum/httpresolves when an exception reaches the kernel boundaryHttpExceptionTranslationHandler— tagged asexception.translator.handlers, translatesHttpExceptionInterfaceinstances into structured domain exceptions
How it works
When an exception reaches the HTTP kernel boundary:
ExceptionHandlerInterface::handle()is called with the exception and the current request- The exception is passed through the
meritum/structured-loggingtranslate→report pipeline — it is translated to a domain exception and logged - An
ErrorEnvelopeis built from the domain exception and returned as a JSON response
HTTP exceptions (implementing HttpExceptionInterface) are translated by HttpExceptionTranslationHandler into an HttpDomainException, which carries the request context and sets the appropriate log severity. Any other exception falls through to the structured-logging catch-all translator and produces a generic 500 response.
Response format
All error responses use a consistent JSON envelope:
code— the domain exception error code;HTTP_followed by the HTTP status code for HTTP exceptionsstatus— the HTTP status code as an integer; also set as the response statustitle— the HTTP exception title;"Unexpected Error"for non-HTTP exceptionsdetail— the exception message; may benullerrors— an optional array of additional error detail objects; omitted when empty
Custom HTTP exceptions
Extend HttpException or implement HttpExceptionInterface directly to define domain-specific HTTP exceptions:
Throw the exception from within the request pipeline:
HttpExceptionTranslationHandler matches any HttpExceptionInterface, so custom exceptions are handled automatically with no additional registration.
Custom translation handlers
To produce a different domain exception for a specific HTTP exception type, implement TranslationHandler and register it at a higher priority than the default handler (0):
Register and tag it in your module:
Because this handler runs at priority 1, it matches before HttpExceptionTranslationHandler (0) for ValidationHttpException instances. All other HTTP exceptions continue to be handled by the default.
Using ErrorEnvelope directly
ErrorEnvelope is public API. Use it to build consistent error responses anywhere in your application:
Attaching additional errors
Call withErrors() to attach a list of error detail objects to the envelope. The shape of each entry is up to the caller — withErrors() carries any string-keyed map:
This produces:
errors is omitted from the response entirely when withErrors() is not called. withErrors() replaces any previously set errors — pass the full list in a single call.
License
MIT
All versions of http-exception-handler with dependencies
georgeff/kernel Version ^1.6
meritum/http Version ^1.0
meritum/structured-logging Version ^1.0