# Class HttpSoft\ErrorHandler\ErrorHandler

Class, that implements Psr\Http\Server\RequestHandlerInterface.

Source code on GitHub.

use HttpSoft\ErrorHandler\ErrorHandler;

/**
 * @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 ErrorHandler($handler, $responseGenerator);

$errorHandler->addListener($logErrorListener);
$errorHandler->addListener($sendErrorListener);

$response = $errorHandler->handle($request);
$emitter->emit($response);

# Public methods

public function __construct(
    RequestHandlerInterface $handler,
    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\RequestHandlerInterface interface, are shown below. All other methods with descriptions are in the used HttpSoft\ErrorHandler\ErrorHandlerTrait trait.

# handle

Handles an instance of the Psr\Http\Server\RequestHandlerInterface that was passed to the constructor when the object was created.

public function handle(ServerRequestInterface $request): 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 handling:

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.