diff --git a/src/Response/HttpResponse.php b/src/Response/HttpResponse.php index b6393cd..9a0aed8 100644 --- a/src/Response/HttpResponse.php +++ b/src/Response/HttpResponse.php @@ -1,9 +1,12 @@ request = $request; + $this->dbConnection = $dbConnection; $this->routeCollection = $routeCollection; $this->appConfig = $appConfig; $this->method = strtolower($requestMethod); @@ -39,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']); @@ -75,7 +89,16 @@ class HttpResponse return; } - $response = call_user_func([$controller, $handler[1]]); + $this->dbConnection->startTransaction(); + 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) { header('Content-Type: ' . $response->getContentType() . '; charset=UTF-8'); $response->render(); @@ -106,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(); + } }