Skip to content

ErrorResponseFactory

Turns a throwable that escaped the middleware pipeline into a response.

This is the backstop for a pre-pipeline failure — TelemetryMiddleware and ErrorHandlingMiddleware never got a chance to run, so the exception is recorded on whatever span is active here and then rendered by borrowing ErrorHandlingMiddleware’s own renderer, so a request that dies during bootstrap still produces the same RFC 9457 output as one that dies inside an action.

It always returns a ResponseInterface and never writes to the SAPI itself. That is what lets a CLI-hosted runtime (RoadRunner, Swoole) emit the error through its own channel; the previous inline version in Kernel fell back to header()+echo, which off-SAPI meant the client got nothing at all.

final class ErrorResponseFactory

SourceRuntime/ErrorResponseFactory.php

public function __construct(Psr17Factory $psr17 = new Psr17Factory(…)): mixed

ParameterTypeDescription
$psr17Psr17Factory

Returns mixed

MethodDescription
fromThrowable(Throwable $e, ?ServerRequestInterface $request = null): ResponseInterfaceRenders a throwable as an RFC 9457 error response.

public function fromThrowable(Throwable $e, ?ServerRequestInterface $request = null): ResponseInterface

Renders a throwable as an RFC 9457 error response.

The exception is logged at debug level and recorded on the active span, then rendered by ErrorHandlingMiddleware’s own renderer so the output matches a failure caught inside the pipeline. When no request is supplied — a failure predating request construction — a synthetic GET /error stands in. If the renderer itself throws, a plain text/plain 500 is returned instead and the render failure is logged.

ParameterTypeDescription
$eThrowable
$request?ServerRequestInterface

Returns ResponseInterface