From ee4c5155fa3190c36faf8d9a3894729eb9e50a7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Sun, 15 Jan 2023 05:00:44 +0100 Subject: [PATCH] Improve exception handling --- src/main/java/wtf/beatrice/hidekobot/HidekoBot.java | 7 ++++++- .../wtf/beatrice/hidekobot/commands/base/Trivia.java | 2 +- .../hidekobot/datasources/ConfigurationSource.java | 9 ++++++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index 4a2eb6a..b5ce394 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -77,7 +77,12 @@ public class HidekoBot ); jda = jdaBuilder.build().awaitReady(); - } catch (Exception e) + } catch (InterruptedException e) { + LOGGER.error(e.getMessage()); // print the error message, omit the stack trace. + Thread.currentThread().interrupt(); // send interrupt to the thread. + shutdown(); // if we failed connecting and authenticating, then quit. + } + catch (Exception e) { LOGGER.error(e.getMessage()); // print the error message, omit the stack trace. shutdown(); // if we failed connecting and authenticating, then quit. diff --git a/src/main/java/wtf/beatrice/hidekobot/commands/base/Trivia.java b/src/main/java/wtf/beatrice/hidekobot/commands/base/Trivia.java index b4bf9c6..d6d9e8e 100644 --- a/src/main/java/wtf/beatrice/hidekobot/commands/base/Trivia.java +++ b/src/main/java/wtf/beatrice/hidekobot/commands/base/Trivia.java @@ -67,7 +67,7 @@ public class Trivia List categories = Trivia.parseCategories(categoriesJson); if(categories.isEmpty()) return new MessageResponse("Error parsing trivia categories!", null); // todo nicer with emojis - + categories.sort(new TriviaCategoryComparator()); EmbedBuilder embedBuilder = new EmbedBuilder(); diff --git a/src/main/java/wtf/beatrice/hidekobot/datasources/ConfigurationSource.java b/src/main/java/wtf/beatrice/hidekobot/datasources/ConfigurationSource.java index 5ee0b6d..f4755b2 100644 --- a/src/main/java/wtf/beatrice/hidekobot/datasources/ConfigurationSource.java +++ b/src/main/java/wtf/beatrice/hidekobot/datasources/ConfigurationSource.java @@ -47,7 +47,14 @@ public class ConfigurationSource if(!fsConfigFile.exists()) { // try to create config file - try { fsConfigFile.createNewFile(); } + try { + if(!fsConfigFile.createNewFile()) + { + LOGGER.error("We tried creating a file that already exists!"); + HidekoBot.shutdown(); + return; + } + } catch (IOException e) { LOGGER.error("Error creating configuration file!", e); HidekoBot.shutdown();