Made a Duplicate Exception class and made that be called instead for better robustness and consistency
This commit is contained in:
parent
e5f7df7318
commit
a1df0f5b24
4 changed files with 34 additions and 10 deletions
|
|
@ -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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
package client.scenes.Ingredient;
|
package client.scenes.Ingredient;
|
||||||
|
|
||||||
|
import client.exception.DuplicateIngredientException;
|
||||||
import client.scenes.nutrition.NutritionDetailsCtrl;
|
import client.scenes.nutrition.NutritionDetailsCtrl;
|
||||||
import client.utils.LocaleAware;
|
import client.utils.LocaleAware;
|
||||||
import client.utils.LocaleManager;
|
import client.utils.LocaleManager;
|
||||||
|
|
@ -113,6 +114,8 @@ public class IngredientListCtrl implements LocaleAware {
|
||||||
refresh(); // reload list from server
|
refresh(); // reload list from server
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
showError("Failed to create ingredient: " + e.getMessage());
|
showError("Failed to create ingredient: " + e.getMessage());
|
||||||
|
} catch (DuplicateIngredientException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package client.scenes.recipe;
|
package client.scenes.recipe;
|
||||||
|
|
||||||
import client.utils.server.ServerUtils;
|
import client.utils.server.ServerUtils;
|
||||||
|
import client.exception.DuplicateIngredientException;
|
||||||
import commons.Ingredient;
|
import commons.Ingredient;
|
||||||
import jakarta.inject.Inject;
|
import jakarta.inject.Inject;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
|
|
@ -66,17 +67,16 @@ public class IngredientsPopupCtrl {
|
||||||
try {
|
try {
|
||||||
server.createIngredient(name); // calls POST /api/ingredients
|
server.createIngredient(name); // calls POST /api/ingredients
|
||||||
refresh(); // reload list from server
|
refresh(); // reload list from server
|
||||||
} catch (IOException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
if (e.getMessage() != null && e.getMessage().startsWith("DUPLICATE:")) {
|
|
||||||
|
showError("Failed to create ingredient: " + e.getMessage());
|
||||||
|
} catch (DuplicateIngredientException e) {
|
||||||
showError("An ingredient with the name " + name + " already exists." +
|
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
|
" 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
|
||||||
|
|
||||||
} else {
|
|
||||||
showError("Failed to create ingredient: " + e.getMessage());
|
|
||||||
}
|
|
||||||
} catch (InterruptedException e){
|
|
||||||
showError("Failed to create ingredient: " + e.getMessage());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package client.utils.server;
|
package client.utils.server;
|
||||||
|
|
||||||
import client.utils.ConfigService;
|
import client.utils.ConfigService;
|
||||||
|
import client.exception.DuplicateIngredientException;
|
||||||
import com.fasterxml.jackson.core.type.TypeReference;
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import com.google.inject.Inject;
|
import com.google.inject.Inject;
|
||||||
|
|
@ -270,7 +271,7 @@ public class ServerUtils {
|
||||||
|
|
||||||
//creates new ingredients in the ingredient list
|
//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);
|
Ingredient ingredient = new Ingredient(name, 0.0, 0.0, 0.0);
|
||||||
String json = objectMapper.writeValueAsString(ingredient);
|
String json = objectMapper.writeValueAsString(ingredient);
|
||||||
|
|
||||||
|
|
@ -280,7 +281,7 @@ public class ServerUtils {
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||||
final int DUPLICATE_STATUS_CODE = 409;
|
final int DUPLICATE_STATUS_CODE = 409;
|
||||||
if (response.statusCode() == DUPLICATE_STATUS_CODE) {
|
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) {
|
if (response.statusCode() != statusOK) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue