# Class HttpSoft\ErrorHandler\ErrorResponseGenerator

Class, that implements HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface.

Source code on GitHub.

The default response status code is 500. If the error code is valid (>= 400 && < 600) and is the value of one of the constants in the HttpSoft\Response\ResponseStatusCodeInterface, the response code will be changed to an error code.

use HttpSoft\ErrorHandler\ErrorResponseGenerator;

/**
 * @var Psr\Http\Message\ServerRequestInterface $request
 */

$responseGenerator = new ErrorResponseGenerator();

$response = $responseGenerator->generate(new \Exception(), $request);
$response->getStatusCode(); // 500
$response->getReasonPhrase(); // 'Internal Server Error'

$response = $responseGenerator->generate(new \Exception('', 301), $request);
$response->getStatusCode(); // 500
$response->getReasonPhrase(); // 'Internal Server Error'

$response = $responseGenerator->generate(new \Exception('', 404), $request);
$response->getStatusCode(); // 404
$response->getReasonPhrase(); // 'Not Found'

$response = $responseGenerator->generate(new \Exception('', 502), $request);
$response->getStatusCode(); // 502
$response->getReasonPhrase(); // 'Bad Gateway'

When generating a response, it tries to determine the content type to return based on the Accept request header.

use HttpSoft\ErrorHandler\ErrorResponseGenerator;

/**
 * @var Throwable $e
 * @var Psr\Http\Message\ServerRequestInterface $request
 */

$responseGenerator = new ErrorResponseGenerator();

// Return an instance of `HttpSoft\Response\TextResponse`
$responseGenerator->generate($e, $request->withHeader('accept', 'text/plain'));
$responseGenerator->generate($e, $request->withHeader('accept', 'application/json;q=0.99, text/plain'));
$responseGenerator->generate($e, $request->withHeader('accept', 'text/plain, text/html, application/json'));

// Return an instance of `HttpSoft\Response\JsonResponse`
$responseGenerator->generate($e, $request->withHeader('accept', 'application/json'));
$responseGenerator->generate($e, $request->withHeader('accept', 'text/plain;q=0.99, application/json'));
$responseGenerator->generate($e, $request->withHeader('accept', 'application/json, text/plain, text/html'));

// Return an instance of `HttpSoft\Response\XmlResponse`
$responseGenerator->generate($e, $request->withHeader('accept', 'text/xml'));
$responseGenerator->generate($e, $request->withHeader('accept', 'application/xml'));
$responseGenerator->generate($e, $request->withHeader('accept', 'text/plain;q=0.99, application/xml'));
$responseGenerator->generate($e, $request->withHeader('accept', 'application/xml, text/plain, text/html'));

// Return an instance of `HttpSoft\Response\HtmlResponse`
$responseGenerator->generate($e, $request); // without Accept header
$responseGenerator->generate($e, $request->withHeader('accept', '*/*'));
$responseGenerator->generate($e, $request->withHeader('accept', 'text/html'));
$responseGenerator->generate($e, $request->withHeader('accept', 'application/json;q=0.99, text/html'));
$responseGenerator->generate($e, $request->withHeader('accept', 'text/html, text/plain, application/json'));

# Public methods

See the original detailed description of the methods in the HttpSoft\ErrorHandler\ErrorResponseGeneratorInterface.

# generate

Generates an instance of Psr\Http\Message\ResponseInterface with information about the handled error.

public function generate(Throwable $error, ServerRequestInterface $request): ResponseInterface;

The HttpSoft\Response implements Psr\Http\Message\ResponseInterface.