# Class HttpSoft\ErrorHandler\ErrorHandlerMiddleware
Middleware that implements Psr\Http\Server\MiddlewareInterface.
use HttpSoft\ErrorHandler\ErrorHandlerMiddleware;
/**
* @var Psr\Http\Message\ServerRequestInterface $request
* @var Psr\Http\Server\RequestHandlerInterface $handler
*
* @var HttpSoft\Emitter\EmitterInterface $emitter
* @var HttpSoft\ErrorHandler\ErrorListenerInterface $logErrorListener
* @var HttpSoft\ErrorHandler\ErrorListenerInterface $sendErrorListener
* @var HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface $responseGenerator
*/
$errorHandler = new ErrorHandlerMiddleware($responseGenerator);
$errorHandler->addListener($logErrorListener);
$errorHandler->addListener($sendErrorListener);
$response = $errorHandler->process($request, $handler);
$emitter->emit($response);
# Public methods
public function __construct(ErrorResponseGeneratorInterface $responseGenerator = null);
If $responseGenerator
was not specified or is null
, HttpSoft\ErrorHandler\ErrorResponseGenerator will be used.
The methods that are defined in the Psr\Http\Server\MiddlewareInterface interface, are shown below. All other methods with descriptions are in the used HttpSoft\ErrorHandler\ErrorHandlerTrait trait.
# process
Processes an instance of the Psr\Http\Server\RequestHandlerInterface.
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler
): ResponseInterface;
When an exception is intercepted, a response with error information is generated and returned; otherwise, the response returned by Psr\Http\Server\RequestHandlerInterface instance is used.
If an error occurs during processing:
- All listeners that were previously added to the queue using the addListener() method will be triggered.
- Using the HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface instance, an instance of Psr\Http\Message\ResponseInterface will be generated with information about the handled error.
If the generated response with information about the handled error has a valid status code (>= 400 && < 600
), it will be returned unchanged.
If the status code of the generated response is invalid, but the error code is valid, the response code will be changed to an error code.
If the status code of the generated response and error code are not valid, a response with the status code 500
is returned.