Final Class Yiisoft\ErrorHandler\Middleware\ExceptionResponder
| Inheritance | Yiisoft\ErrorHandler\Middleware\ExceptionResponder |
|---|---|
| Implements | Psr\Http\Server\MiddlewareInterface |
ExceptionResponder maps certain exceptions to custom responses.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | The $exceptionMap specified as an array can be in one of the following two formats: |
Yiisoft\ErrorHandler\Middleware\ExceptionResponder |
| process() | Yiisoft\ErrorHandler\Middleware\ExceptionResponder |
Method Details
The $exceptionMap specified as an array can be in one of the following two formats:
- callable format:
[\LogicException::class => callable, \DomainException::class => callable, ...] - int format:
[\Exception::class => 404, \DomainException::class => 500, ...]
When an exception is thrown, the map in callable format allows to take control of the response.
Сallable must return Psr\Http\Message\ResponseInterface. If specified exception classes are equal,
then the first one will be processed. Below are some examples:
$exceptionMap = [
DomainException::class => function (\Psr\Http\Message\ResponseFactoryInterface $responseFactory) {
return $responseFactory->createResponse(\Yiisoft\Http\Status::CREATED);
},
MyHttpException::class => static fn (MyHttpException $exception) => new MyResponse($exception),
]
When an exception is thrown, the map in int format allows to send the response with set http code. If specified exception classes are equal, then the first one will be processed. Below are some examples:
$exceptionMap = [
\DomainException::class => \Yiisoft\Http\Status::BAD_REQUEST,
\InvalidArgumentException::class => \Yiisoft\Http\Status::BAD_REQUEST,
MyNotFoundException::class => \Yiisoft\Http\Status::NOT_FOUND,
]
| public mixed __construct ( callable[]|integer[] $exceptionMap, \Psr\Http\Message\ResponseFactoryInterface $responseFactory, \Yiisoft\Injector\Injector $injector, boolean $checkResponseBody = false ) | ||
| $exceptionMap | callable[]|integer[] |
A callable that must return a |
| $responseFactory | \Psr\Http\Message\ResponseFactoryInterface | |
| $injector | \Yiisoft\Injector\Injector | |
| $checkResponseBody | boolean |
Whether executing |
public function __construct(
private readonly array $exceptionMap,
private readonly ResponseFactoryInterface $responseFactory,
private readonly Injector $injector,
private readonly bool $checkResponseBody = false,
) {
}
| public \Psr\Http\Message\ResponseInterface process ( \Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Server\RequestHandlerInterface $handler ) | ||
| $request | \Psr\Http\Message\ServerRequestInterface | |
| $handler | \Psr\Http\Server\RequestHandlerInterface | |
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
try {
$response = $handler->handle($request);
if ($this->checkResponseBody) {
$response->getBody();
}
} catch (Throwable $t) {
foreach ($this->exceptionMap as $exceptionType => $responseHandler) {
if ($t instanceof $exceptionType) {
if (is_int($responseHandler)) {
return $this->responseFactory->createResponse($responseHandler);
}
if (is_callable($responseHandler)) {
/** @var ResponseInterface */
return $this->injector->invoke($responseHandler, [
'exception' => $t,
'request' => $request,
]);
}
}
}
throw $t;
}
return $response;
}
Signup or Login in order to comment.