meritum/http-exception-handler
Composer 安装命令:
composer require meritum/http-exception-handler
包简介
meritum/http exception handler that translates exceptions into structured JSON error responses using the meritum/structured-logging pipeline
README 文档
README
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
composer require meritum/http-exception-handler
Usage
Module registration
Register ExceptionHandlerModule alongside StructuredLoggingModule. StructuredLoggingModule requires a LoggerInterface to already be registered:
use Meritum\HttpExceptionHandler\ExceptionHandlerModule; use Meritum\StructuredLogging\StructuredLoggingModule; $kernel->define(LoggerInterface::class, fn() => new MyLogger()); $kernel->addModule(new StructuredLoggingModule()); $kernel->addModule(new ExceptionHandlerModule()); $kernel->boot();
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": "HTTP_404",
"status": 404,
"title": "Not Found",
"detail": "The requested resource could not be found."
}
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 benull
Custom HTTP exceptions
Extend HttpException or implement HttpExceptionInterface directly to define domain-specific HTTP exceptions:
use Meritum\Http\Exception\HttpException; final class ResourceNotFoundException extends HttpException { protected string $title = 'Not Found'; protected int $status = 404; }
Throw the exception from within the request pipeline:
throw new ResourceNotFoundException($request, 'User 42 does not exist.');
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):
use Throwable; use Meritum\StructuredLogging\TranslationHandler; use Meritum\StructuredLogging\Exception\DomainException; final class ValidationExceptionTranslationHandler implements TranslationHandler { public function matches(Throwable $exception): bool { return $exception instanceof ValidationHttpException; } public function handle(Throwable $exception): DomainException { assert($exception instanceof ValidationHttpException); return new ValidationDomainException($exception); } public function priority(): int { return 1; } }
Register and tag it in your module:
$kernel->define(ValidationExceptionTranslationHandler::class, fn() => new ValidationExceptionTranslationHandler()) ->tag('exception.translator.handlers');
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:
use Meritum\HttpExceptionHandler\ErrorEnvelope; // From a caught domain exception $envelope = ErrorEnvelope::fromDomainException($domainException); // With explicit values $envelope = new ErrorEnvelope('HTTP_403', 403, 'Forbidden', 'You do not have permission.'); return new JsonResponse($envelope, $envelope->status);
License
MIT
统计信息
- 总下载量: 0
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-06-12