ExceptionHandlerMiddleware.php•809 B
<?php
declare(strict_types=1);
namespace Butschster\ContextGenerator\McpServer\Middleware;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Spiral\Exceptions\ExceptionReporterInterface;
final readonly class ExceptionHandlerMiddleware implements MiddlewareInterface
{
public function __construct(
private ExceptionReporterInterface $reporter,
) {}
public function process(
ServerRequestInterface $request,
RequestHandlerInterface $handler,
): ResponseInterface {
try {
return $handler->handle($request);
} catch (\Throwable $e) {
$this->reporter->report($e);
throw $e;
}
}
}