From a1df0f5b24f6e9197f63b33048d033c98646b9a1 Mon Sep 17 00:00:00 2001 From: Rithvik Sriram Date: Fri, 16 Jan 2026 14:03:14 +0100 Subject: [PATCH] Made a Duplicate Exception class and made that be called instead for better robustness and consistency --- .../DuplicateIngredientException.java | 20 +++++++++++++++++++ .../scenes/Ingredient/IngredientListCtrl.java | 3 +++ .../scenes/recipe/IngredientsPopupCtrl.java | 16 +++++++-------- .../java/client/utils/server/ServerUtils.java | 5 +++-- 4 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 client/src/main/java/client/exception/DuplicateIngredientException.java diff --git a/client/src/main/java/client/exception/DuplicateIngredientException.java b/client/src/main/java/client/exception/DuplicateIngredientException.java new file mode 100644 index 0000000..af04346 --- /dev/null +++ b/client/src/main/java/client/exception/DuplicateIngredientException.java @@ -0,0 +1,20 @@ +package client.exception; + +public class DuplicateIngredientException extends Exception{ + private final String ingredientName; + + public DuplicateIngredientException(String ingredientName){ + super("An ingredient with name " + ingredientName + " already exists, please provide a different name"); + this.ingredientName = ingredientName; + } + + public DuplicateIngredientException(String ingredientName, String message){ + super(message); + this.ingredientName = ingredientName; + } + + public String getIngredientName() { + return ingredientName; + } + +} diff --git a/client/src/main/java/client/scenes/Ingredient/IngredientListCtrl.java b/client/src/main/java/client/scenes/Ingredient/IngredientListCtrl.java index 329eb56..38e6077 100644 --- a/client/src/main/java/client/scenes/Ingredient/IngredientListCtrl.java +++ b/client/src/main/java/client/scenes/Ingredient/IngredientListCtrl.java @@ -1,5 +1,6 @@ package client.scenes.Ingredient; +import client.exception.DuplicateIngredientException; import client.scenes.nutrition.NutritionDetailsCtrl; import client.utils.LocaleAware; import client.utils.LocaleManager; @@ -113,6 +114,8 @@ public class IngredientListCtrl implements LocaleAware { refresh(); // reload list from server } catch (IOException | InterruptedException e) { showError("Failed to create ingredient: " + e.getMessage()); + } catch (DuplicateIngredientException e) { + throw new RuntimeException(e); } } diff --git a/client/src/main/java/client/scenes/recipe/IngredientsPopupCtrl.java b/client/src/main/java/client/scenes/recipe/IngredientsPopupCtrl.java index 203fb91..091befd 100644 --- a/client/src/main/java/client/scenes/recipe/IngredientsPopupCtrl.java +++ b/client/src/main/java/client/scenes/recipe/IngredientsPopupCtrl.java @@ -1,6 +1,7 @@ package client.scenes.recipe; import client.utils.server.ServerUtils; +import client.exception.DuplicateIngredientException; import commons.Ingredient; import jakarta.inject.Inject; import javafx.fxml.FXML; @@ -66,17 +67,16 @@ public class IngredientsPopupCtrl { try { server.createIngredient(name); // calls POST /api/ingredients refresh(); // reload list from server - } catch (IOException e) { - if (e.getMessage() != null && e.getMessage().startsWith("DUPLICATE:")) { - showError("An ingredient with the name " + name + " already exists." + - " Please provide a different name."); //checks if error received has the DUPLICATE string and creates a showError instance with an appropriate error message and description + } catch (IOException | InterruptedException e) { - } else { - showError("Failed to create ingredient: " + e.getMessage()); - } - } catch (InterruptedException e){ showError("Failed to create ingredient: " + e.getMessage()); + } catch (DuplicateIngredientException e) { + showError("An ingredient with the name " + name + " already exists." + + " Please provide a different name."); //checks if error received has the DUPLICATE string and creates a showError instance with an appropriate error message and description + } + + } diff --git a/client/src/main/java/client/utils/server/ServerUtils.java b/client/src/main/java/client/utils/server/ServerUtils.java index 3e2aa7e..c9a308d 100644 --- a/client/src/main/java/client/utils/server/ServerUtils.java +++ b/client/src/main/java/client/utils/server/ServerUtils.java @@ -1,6 +1,7 @@ package client.utils.server; import client.utils.ConfigService; +import client.exception.DuplicateIngredientException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.google.inject.Inject; @@ -270,7 +271,7 @@ public class ServerUtils { //creates new ingredients in the ingredient list - public Ingredient createIngredient(String name) throws IOException, InterruptedException { + public Ingredient createIngredient(String name) throws IOException, InterruptedException, DuplicateIngredientException { Ingredient ingredient = new Ingredient(name, 0.0, 0.0, 0.0); String json = objectMapper.writeValueAsString(ingredient); @@ -280,7 +281,7 @@ public class ServerUtils { HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); final int DUPLICATE_STATUS_CODE = 409; if (response.statusCode() == DUPLICATE_STATUS_CODE) { - throw new IOException("DUPLICATE: An ingredient with the name '" + name + "' already exists."); + throw new DuplicateIngredientException(name); } if (response.statusCode() != statusOK) {