From a22c1275005760ff826d4e8562aab32b4924fe4d Mon Sep 17 00:00:00 2001
From: Phan Huy Tran
Date: Thu, 24 Apr 2025 14:48:30 +0200
Subject: [PATCH 1/2] refactor: throw and handle unsufficient funds exception
---
.../casino/blackjack/BlackJackGameController.java | 3 ++-
.../exceptionHandling/GlobalExceptionHandler.java | 7 +++++++
.../exceptions/InsufficientFundsException.java | 11 +++++++++++
.../de/szut/casino/lootboxes/LootBoxController.java | 5 ++---
.../java/de/szut/casino/slots/SlotController.java | 3 ++-
5 files changed, 24 insertions(+), 5 deletions(-)
create mode 100644 backend/src/main/java/de/szut/casino/exceptionHandling/exceptions/InsufficientFundsException.java
diff --git a/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameController.java b/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameController.java
index 0144a36..6813236 100644
--- a/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameController.java
+++ b/backend/src/main/java/de/szut/casino/blackjack/BlackJackGameController.java
@@ -1,5 +1,6 @@
package de.szut.casino.blackjack;
+import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
import de.szut.casino.shared.dto.BetDto;
import de.szut.casino.shared.service.BalanceService;
@@ -123,7 +124,7 @@ public class BlackJackGameController {
UserEntity user = optionalUser.get();
if (!this.balanceService.hasFunds(user, betDto)) {
- return ResponseEntity.badRequest().body(Collections.singletonMap("error", "Insufficient funds"));
+ throw new InsufficientFundsException();
}
return ResponseEntity.ok(blackJackService.createBlackJackGame(user, betDto.getBetAmount()));
diff --git a/backend/src/main/java/de/szut/casino/exceptionHandling/GlobalExceptionHandler.java b/backend/src/main/java/de/szut/casino/exceptionHandling/GlobalExceptionHandler.java
index fde23e6..df20fe0 100644
--- a/backend/src/main/java/de/szut/casino/exceptionHandling/GlobalExceptionHandler.java
+++ b/backend/src/main/java/de/szut/casino/exceptionHandling/GlobalExceptionHandler.java
@@ -1,5 +1,6 @@
package de.szut.casino.exceptionHandling;
+import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@@ -17,4 +18,10 @@ public class GlobalExceptionHandler {
ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
return new ResponseEntity<>(errorDetails, HttpStatus.NOT_FOUND);
}
+
+ @ExceptionHandler(InsufficientFundsException.class)
+ public ResponseEntity> handleInsufficientFundsException(InsufficientFundsException ex, WebRequest request) {
+ ErrorDetails errorDetails = new ErrorDetails(new Date(), ex.getMessage(), request.getDescription(false));
+ return new ResponseEntity<>(errorDetails, HttpStatus.BAD_REQUEST);
+ }
}
diff --git a/backend/src/main/java/de/szut/casino/exceptionHandling/exceptions/InsufficientFundsException.java b/backend/src/main/java/de/szut/casino/exceptionHandling/exceptions/InsufficientFundsException.java
new file mode 100644
index 0000000..7c87acc
--- /dev/null
+++ b/backend/src/main/java/de/szut/casino/exceptionHandling/exceptions/InsufficientFundsException.java
@@ -0,0 +1,11 @@
+package de.szut.casino.exceptionHandling.exceptions;
+
+import org.springframework.http.HttpStatus;
+import org.springframework.web.bind.annotation.ResponseStatus;
+
+@ResponseStatus(value = HttpStatus.BAD_REQUEST)
+public class InsufficientFundsException extends RuntimeException {
+ public InsufficientFundsException() {
+ super("insufficient funds");
+ }
+}
diff --git a/backend/src/main/java/de/szut/casino/lootboxes/LootBoxController.java b/backend/src/main/java/de/szut/casino/lootboxes/LootBoxController.java
index 1070e6c..4ef8247 100644
--- a/backend/src/main/java/de/szut/casino/lootboxes/LootBoxController.java
+++ b/backend/src/main/java/de/szut/casino/lootboxes/LootBoxController.java
@@ -1,5 +1,6 @@
package de.szut.casino.lootboxes;
+import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
import de.szut.casino.user.UserEntity;
import de.szut.casino.user.UserRepository;
@@ -44,9 +45,7 @@ public class LootBoxController {
UserEntity user = optionalUser.get();
if (lootBoxService.hasSufficientBalance(user, lootBox.getPrice())) {
- Map errorResponse = new HashMap<>();
- errorResponse.put("error", "Insufficient balance");
- return ResponseEntity.badRequest().body(errorResponse);
+ throw new InsufficientFundsException();
}
RewardEntity reward = lootBoxService.determineReward(lootBox);
diff --git a/backend/src/main/java/de/szut/casino/slots/SlotController.java b/backend/src/main/java/de/szut/casino/slots/SlotController.java
index bb96715..e69de94 100644
--- a/backend/src/main/java/de/szut/casino/slots/SlotController.java
+++ b/backend/src/main/java/de/szut/casino/slots/SlotController.java
@@ -1,5 +1,6 @@
package de.szut.casino.slots;
+import de.szut.casino.exceptionHandling.exceptions.InsufficientFundsException;
import de.szut.casino.exceptionHandling.exceptions.UserNotFoundException;
import de.szut.casino.shared.dto.BetDto;
import de.szut.casino.shared.service.BalanceService;
@@ -38,7 +39,7 @@ public class SlotController {
UserEntity user = optionalUser.get();
if (!this.balanceService.hasFunds(user, betDto)) {
- return ResponseEntity.badRequest().body(Collections.singletonMap("error", "Insufficient funds"));
+ throw new InsufficientFundsException();
}
SpinResult spinResult = this.slotService.spin(
From 9a901154b6ee8f27a12e8cd51be50244ac0dea18 Mon Sep 17 00:00:00 2001
From: csimonis
Date: Thu, 24 Apr 2025 14:48:40 +0200
Subject: [PATCH 2/2] feat(docker): wait for backend host to resolve before
start
---
frontend/.docker/entrypoint.sh | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/frontend/.docker/entrypoint.sh b/frontend/.docker/entrypoint.sh
index 3842b5a..7aa167e 100755
--- a/frontend/.docker/entrypoint.sh
+++ b/frontend/.docker/entrypoint.sh
@@ -3,5 +3,11 @@
: ${BACKEND_HOST:=localhost}
: ${BACKEND_PORT:=8080}
+# Wait until the backend host is resolvable
+echo "Waiting for backend host $BACKEND_HOST..."
+until getent hosts "$BACKEND_HOST" > /dev/null; do
+ sleep 1
+done
+
envsubst '$BACKEND_HOST $BACKEND_PORT' < /etc/nginx/templates/nginx.conf.template > /etc/nginx/conf.d/default.conf
exec nginx -g 'daemon off;'