ErrorHandlingMiddleware
Catches unhandled throwables from downstream middleware/action dispatch and produces a generic 500 (or mapped) response.
Currently minimal; can be extended to perform content negotiation (HTML/JSON) and structured logging.
Synopsis
Section titled “Synopsis”class ErrorHandlingMiddleware implements MiddlewareInterface
| Implements | MiddlewareInterface |
| Source | Middleware/ErrorHandlingMiddleware.php |
Constructor
Section titled “Constructor”__construct()
Section titled “__construct()”public function __construct(callable(Throwable $e, ServerRequestInterface $r): void|null $logger = null): mixed
| Parameter | Type | Description |
|---|---|---|
$logger | `callable(Throwable $e, ServerRequestInterface $r): void“ | “null` |
Returns mixed
Methods
Section titled “Methods”| Method | Description |
|---|---|
process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface | Catches any throwable escaping the rest of the stack and renders it as a response. |
renderExceptionResponse(ServerRequestInterface $request, Throwable $e): ResponseInterface | Public helper so Kernel (or other bootstrap code) can render a unified exception response. |
process()
Section titled “process()”public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
Catches any throwable escaping the rest of the stack and renders it as a response.
Logs a single diagnostic line carrying the exception class, message, throw site, the triggering request, the cause chain and the trace, then hands the exception to ErrorHandlingMiddleware::renderExceptionResponse() for the actual body and status. Nothing propagates out of here, which is the point of its high bootstrap priority: everything ordered inside it is covered.
Middleware that must see error and 404 responses has to be ordered outside this one; after: ErrorHandlingMiddleware places it within the try and it will be skipped whenever an exception is thrown.
| Parameter | Type | Description |
|---|---|---|
$request | ServerRequestInterface | |
$handler | RequestHandlerInterface |
Returns ResponseInterface
renderExceptionResponse()
Section titled “renderExceptionResponse()”public function renderExceptionResponse(ServerRequestInterface $request, Throwable $e): ResponseInterface
Public helper so Kernel (or other bootstrap code) can render a unified exception response.
| Parameter | Type | Description |
|---|---|---|
$request | ServerRequestInterface | |
$e | Throwable |
Returns ResponseInterface