Skip to content

Commit

Permalink
Split out method
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Oct 21, 2023
1 parent b901dbd commit 539e6b2
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/realtime-compiler/src/Http/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function __construct()
throw $exception;
}

$this->sendJsonResponse($exception);
$this->sendJsonErrorResponse($exception);
}
}
}
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 539e6b2

Please sign in to comment.