. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Fisharebest\Webtrees\Http\Controllers\ErrorController; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; use Psr\Http\Server\RequestHandlerInterface; use Symfony\Component\HttpKernel\Exception\HttpException; use Throwable; /** * Middleware to handle and render errors. */ class HandleExceptions implements MiddlewareInterface { /** @var ErrorController */ private $error_controller; /** * ExceptionHandler constructor. * * @param ErrorController $error_controller */ public function __construct(ErrorController $error_controller) { $this->error_controller = $error_controller; } /** * @param ServerRequestInterface $request * @param RequestHandlerInterface $handler * * @return ResponseInterface */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { try { return $handler->handle($request); } catch (HttpException $exception) { if ($request->getHeaderLine('X-Requested-With') !== '') { return $this->error_controller->ajaxErrorResponse($exception); } return $this->error_controller->errorResponse($exception); } catch (Throwable $exception) { return $this->error_controller->unhandledExceptionResponse($request, $exception); } } }