From 539e6b2f984d21e8791027e024d1c01af300b582 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Sat, 21 Oct 2023 14:28:55 +0200 Subject: [PATCH] Split out method --- .../src/Http/DashboardController.php | 20 ++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/packages/realtime-compiler/src/Http/DashboardController.php b/packages/realtime-compiler/src/Http/DashboardController.php index 9c7279c134a..5afe1cb133c 100644 --- a/packages/realtime-compiler/src/Http/DashboardController.php +++ b/packages/realtime-compiler/src/Http/DashboardController.php @@ -71,7 +71,7 @@ public function __construct() throw $exception; } - $this->sendJsonResponse($exception); + $this->sendJsonErrorResponse($exception); } } } @@ -320,16 +320,30 @@ protected static function getPackageVersion(string $packageName): string return $prettyVersion ?? 'unreleased'; } - protected function sendJsonResponse(HttpException $exception): never + protected function sendJsonResponse(int $statusCode, string $body): never { - $statusMessage = match ($exception->getStatusCode()) { + $statusMessage = match ($statusCode) { 200 => 'OK', 201 => 'Created', + default => 'Internal Server Error', + }; + + (new JsonResponse($statusCode, $statusMessage, [ + 'body' => $body, + ]))->send(); + + exit; + } + + protected function sendJsonErrorResponse(HttpException $exception): never + { + $statusMessage = match ($exception->getStatusCode()) { 400 => 'Bad Request', 403 => 'Forbidden', 409 => 'Conflict', default => 'Internal Server Error', }; + (new JsonResponse($exception->getStatusCode(), $statusMessage, [ 'error' => $exception->getMessage(), ]))->send();