From 51801d4228aa69b2d6ea8789e350cce7d40377ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Tue, 18 Apr 2023 22:22:02 +0200 Subject: [PATCH 1/2] execute every db command in a transaction in controllers --- src/Response/HttpResponse.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Response/HttpResponse.php b/src/Response/HttpResponse.php index b6393cd..a3496d4 100644 --- a/src/Response/HttpResponse.php +++ b/src/Response/HttpResponse.php @@ -4,6 +4,7 @@ use SokoWeb\Interfaces\Response\IRedirect; use SokoWeb\Interfaces\Response\IContent; use SokoWeb\Interfaces\Authentication\IAuthenticationRequired; use SokoWeb\Interfaces\Authorization\ISecured; +use SokoWeb\Interfaces\Database\IConnection; use SokoWeb\Interfaces\Request\IRequest; use SokoWeb\Response\Redirect; use SokoWeb\Response\HtmlContent; @@ -14,6 +15,8 @@ class HttpResponse { private IRequest $request; + private IConnection $dbConnection; + private RouteCollection $routeCollection; private array $appConfig; @@ -26,12 +29,14 @@ class HttpResponse public function __construct( IRequest $request, + IConnection $dbConnection, RouteCollection $routeCollection, array $appConfig, string $requestMethod, string $requestUrl ) { $this->request = $request; + $this->dbConnection = $dbConnection; $this->routeCollection = $routeCollection; $this->appConfig = $appConfig; $this->method = strtolower($requestMethod); @@ -75,7 +80,10 @@ class HttpResponse return; } + $this->dbConnection->startTransaction(); $response = call_user_func([$controller, $handler[1]]); + $this->dbConnection->commit(); + if ($response instanceof IContent) { header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8'); $response->render(); -- 2.45.2 From ad7b8c3eda960a22959761d1157e84948bbc514d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=91cze=20Bence?= Date: Tue, 18 Apr 2023 22:40:01 +0200 Subject: [PATCH 2/2] handle errors and exceptions in controllers --- src/Response/HttpResponse.php | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Response/HttpResponse.php b/src/Response/HttpResponse.php index a3496d4..9a0aed8 100644 --- a/src/Response/HttpResponse.php +++ b/src/Response/HttpResponse.php @@ -1,5 +1,7 @@ request = $request; $this->dbConnection = $dbConnection; $this->routeCollection = $routeCollection; @@ -44,6 +48,11 @@ class HttpResponse $this->rawUrl = $requestUrl; } + public function exceptionsErrorHandler($severity, $message, $filename, $lineno) + { + throw new ErrorException($message, 0, $severity, $filename, $lineno); + } + public function render(): void { $match = $this->routeCollection->match($this->method, $this->parsedUrl['path']); @@ -81,7 +90,13 @@ class HttpResponse } $this->dbConnection->startTransaction(); - $response = call_user_func([$controller, $handler[1]]); + try { + $response = call_user_func([$controller, $handler[1]]); + } catch (Exception $exception) { + $this->dbConnection->rollback(); + $this->render500($exception); + return; + } $this->dbConnection->commit(); if ($response instanceof IContent) { @@ -114,4 +129,17 @@ class HttpResponse header('Content-Type: text/html; charset=UTF-8', true, 404); $content->render(); } + + private function render500(Exception $exception): void + { + if (empty($_ENV['DEV'])) { + $exceptionToPrint = null; + } else { + $exceptionToPrint = (string)$exception; + } + + $content = new HtmlContent($this->appConfig['error500View'], ['exceptionToPrint' => $exceptionToPrint]); + header('Content-Type: text/html; charset=UTF-8', true, 500); + $content->render(); + } } -- 2.45.2