Skip to content

Commit

Permalink
Fixed error handling.
Browse files Browse the repository at this point in the history
  • Loading branch information
AllKeng committed Dec 6, 2023
1 parent 96b1e63 commit 1b74604
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 54 deletions.
116 changes: 68 additions & 48 deletions app/src/main/java/code/client/Controllers/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,23 @@ private void handleRecipePostButton(ActionEvent event) throws IOException {

String recipe = writer.toString();
Thread thread = new Thread(() -> {
String response = model.performRecipeRequest("POST", recipe, null);
// Changes UI to Detailed Recipe Screen
Platform.runLater(
() -> {

if (response.contains("Offline")) {
AppAlert.show("Connection Error", "Something went wrong. Please check your connection and try again.");
} else if (response.contains("Error")) {
AppAlert.show("Error", "Something went wrong. Please check your inputs and try again.");
}
String response = model.performRecipeRequest("POST", recipe, null);
// Changes UI to Detailed Recipe Screen
Platform.runLater(
() -> {

if (response.contains("Offline")) {
AppAlert.show("Connection Error",
"Something went wrong. Please check your connection and try again.");
} else if (response.contains("Error")) {
AppAlert.show("Error", "Something went wrong. Please check your inputs and try again.");
} else {
getUserRecipeList();
displayUserRecipes();
});
});
thread.start();
}
});
});
thread.start();
}

private void handleLogOutOutButton(ActionEvent event) {
Expand All @@ -128,7 +130,7 @@ private void handleHomeButton(ActionEvent event) {
}

private void goToRecipeList(boolean afterChanges) {
if(afterChanges) {
if (afterChanges) {
getUserRecipeList();
displayUserRecipes();
}
Expand All @@ -148,16 +150,30 @@ private void goToRecipeList(boolean afterChanges) {

private void getUserRecipeList() {
String userID = account.getId();
String response = model.performRecipeRequest("GET", null, userID);
Reader reader = new StringReader(response);
recipeReader = new RecipeCSVReader(reader);
Thread thread = new Thread(() -> {
String response = model.performRecipeRequest("GET", null, userID);
if (response.contains("Offline")) {
Platform.runLater(() -> {
view.goToOfflineUI();
});
} else if (response.contains("Error")) {
Platform.runLater(() -> {
AppAlert.show("Error", "Something went wrong. Please check your inputs and try again.");
});
} else {
Reader reader = new StringReader(response);
recipeReader = new RecipeCSVReader(reader);

try {
recipeDb = new RecipeListDb();
recipeReader.readRecipeDb(recipeDb);
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.start();

try {
recipeDb = new RecipeListDb();
recipeReader.readRecipeDb(recipeDb);
} catch (IOException e) {
e.printStackTrace();
}
}

private void displayUserRecipes() {
Expand Down Expand Up @@ -248,12 +264,12 @@ private void handleDetailedViewFromNewRecipeButton(ActionEvent event) {

// Changes UI to Detailed Recipe Screen
Platform.runLater(
() -> {
view.goToDetailedView(chatGPTrecipe, false);
view.getDetailedView().getRecipeDetailsUI().setEditable(false);
handleDetailedViewListeners();
});
() -> {
view.goToDetailedView(chatGPTrecipe, false);
view.getDetailedView().getRecipeDetailsUI().setEditable(false);
handleDetailedViewListeners();
});

});
thread.start();
} catch (Exception exception) {
Expand Down Expand Up @@ -320,15 +336,15 @@ private void deleteGivenRecipe(Recipe recipe) throws IOException {

System.out.println("Deleting id: " + recipe.getId());
Thread thread = new Thread(() -> {
model.performRecipeRequest("DELETE", recipeStr, null);
Platform.runLater(
() -> {
goToRecipeList(true);
this.view.getAppFrameHome().updateDisplay(filter);
addListenersToList();
});
});
model.performRecipeRequest("DELETE", recipeStr, null);
Platform.runLater(
() -> {
goToRecipeList(true);
this.view.getAppFrameHome().updateDisplay(filter);
addListenersToList();
});
});

thread.start();
}

Expand Down Expand Up @@ -377,11 +393,11 @@ private void handleRefreshButton(ActionEvent event) throws URISyntaxException, I

// Changes UI to Detailed Recipe Screen
Platform.runLater(
() -> {
view.goToDetailedView(chatGPTrecipe, false);
view.getDetailedView().getRecipeDetailsUI().setEditable(false);
handleDetailedViewListeners();
});
() -> {
view.goToDetailedView(chatGPTrecipe, false);
view.getDetailedView().getRecipeDetailsUI().setEditable(false);
handleDetailedViewListeners();
});
});
thread.start();
} catch (Exception exception) {
Expand All @@ -393,7 +409,8 @@ private void handleRefreshButton(ActionEvent event) throws URISyntaxException, I
}
}

/////////////////////////////// ACCOUNT MANAGEMENT ///////////////////////////////////
/////////////////////////////// ACCOUNT MANAGEMENT
/////////////////////////////// ///////////////////////////////////
private void handleCreateAcc(ActionEvent event) {
GridPane grid = view.getAccountCreationUI().getRoot();
String username = view.getAccountCreationUI().getUsernameTextField().getText();
Expand All @@ -420,7 +437,8 @@ private void handleCreateAcc(ActionEvent event) {
} else {
view.goToCreateAcc();
Platform.runLater(
() -> view.showErrorPane(grid, "Error. This username is already taken. Please choose another one."));
() -> view.showErrorPane(grid,
"Error. This username is already taken. Please choose another one."));
}
});
thread.start();
Expand Down Expand Up @@ -512,7 +530,7 @@ private void loadCredentials() {
private boolean performLogin(String username, String password) {
// Will add logic for failed login later
String response = model.performAccountRequest("GET", username, password);
if (response.contains("Offline")) {
if (response.contains("Offline") || response.contains("Connection")) {
view.goToOfflineUI();
return false;
} else if (response.contains("Error")) {
Expand All @@ -528,9 +546,11 @@ private boolean performLogin(String username, String password) {
account = new Account(accountId, username, password);
return true;
}
/////////////////////////////// ACCOUNT MANAGEMENT ///////////////////////////////////
/////////////////////////////// ACCOUNT MANAGEMENT
/////////////////////////////// ///////////////////////////////////

/////////////////////////////// AUDIO MANAGEMENT///////////////////////////////////
/////////////////////////////// AUDIO
/////////////////////////////// MANAGEMENT///////////////////////////////////
public void handleRecordMealType(ActionEvent event) throws IOException, URISyntaxException {
recordMealType();
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/code/server/AccountRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
} catch (MongoWriteException ex) {
ex.printStackTrace();
response = "Duplicate Key Error";
} catch (MongoSocketReadException | MongoTimeoutException e) {
} catch (ConnectException e) {
response = "Server Offline";
} catch (Exception e) {
response = "Error";
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/code/server/ChatGPTRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.sun.net.httpserver.*;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient;
Expand Down Expand Up @@ -34,7 +35,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
} catch (IndexOutOfBoundsException e) {
response = "Provide valid meal type or ingredients";
e.printStackTrace();
} catch (InterruptedException | URISyntaxException e) {
} catch (InterruptedException | URISyntaxException | ConnectException e) {
response = "An error occurred.";
e.printStackTrace();
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/code/server/DallERequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
String recipeTitle = query.substring(query.indexOf("=") + 1);
recipeTitle = URLEncoder.encode(recipeTitle, "UTF-8");
response = getResponse(recipeTitle);
} catch (InterruptedException e) {
} catch (InterruptedException | ConnectException e) {
response = "Error";
e.printStackTrace();
}
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/code/server/RecipeRequestHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ public void handle(HttpExchange httpExchange) throws IOException {
} catch (MongoWriteException ex) {
ex.printStackTrace();
response = "Duplicate Key Error";
} catch (MongoSocketReadException ex) {
ex.printStackTrace();
response = "Server Offline";
} catch (ConnectException ex) {
// ex.printStackTrace();
response = "Error Server Offline";
} catch (Exception e) {
response = "Error";
System.out.println("An erroneous request");
Expand Down

0 comments on commit 1b74604

Please sign in to comment.