From ef85f0cdd60f0b3ed33778ec3c0853a11bc14251 Mon Sep 17 00:00:00 2001 From: Louis Vallat Date: Wed, 28 Oct 2020 00:01:13 +0100 Subject: [PATCH] Grouped all exit codes in one enumeration Signed-off-by: Louis Vallat --- .../java/xyz/vallat/louis/MovieQuoteBot.java | 7 ++++++- .../java/xyz/vallat/louis/codes/ExitCodes.java | 18 ++++++++++++++++++ .../xyz/vallat/louis/database/DBManager.java | 5 +++-- .../vallat/louis/database/LanguageManager.java | 3 ++- .../vallat/louis/database/PropertyManager.java | 3 ++- 5 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 src/main/java/xyz/vallat/louis/codes/ExitCodes.java diff --git a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java index d800e88..a9010e5 100644 --- a/src/main/java/xyz/vallat/louis/MovieQuoteBot.java +++ b/src/main/java/xyz/vallat/louis/MovieQuoteBot.java @@ -62,7 +62,12 @@ public class MovieQuoteBot { } private static void discordLogin(String token) { - discordClient = DiscordClientBuilder.create(token).build().login().block(); + discordClient = DiscordClientBuilder.create(token).build().login().retry(2) + .onErrorMap(throwable -> { + logger.error("Cannot login to Discord right now. Reason: {}", throwable.getMessage()); + return throwable; + } + ).block(); assert discordClient != null; discordClient.updatePresence(Presence.online(Activity.watching("some more films"))).subscribe(); discordClient.getEventDispatcher().on(ReadyEvent.class) diff --git a/src/main/java/xyz/vallat/louis/codes/ExitCodes.java b/src/main/java/xyz/vallat/louis/codes/ExitCodes.java new file mode 100644 index 0000000..13626a8 --- /dev/null +++ b/src/main/java/xyz/vallat/louis/codes/ExitCodes.java @@ -0,0 +1,18 @@ +package xyz.vallat.louis.codes; + +public enum ExitCodes { + CANNOT_CONNECT_TO_DB(1), + CANNOT_INITIALIZE_DB(2), + CANNOT_INITIALIZE_LANGUAGES(3), + CANNOT_GET_PROPERTY(4); + + private final int value; + + ExitCodes(int value) { + this.value = value; + } + + public int getValue() { + return this.value; + } +} diff --git a/src/main/java/xyz/vallat/louis/database/DBManager.java b/src/main/java/xyz/vallat/louis/database/DBManager.java index be68eab..90e451f 100644 --- a/src/main/java/xyz/vallat/louis/database/DBManager.java +++ b/src/main/java/xyz/vallat/louis/database/DBManager.java @@ -3,6 +3,7 @@ package xyz.vallat.louis.database; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import xyz.vallat.louis.MovieQuoteBot; +import xyz.vallat.louis.codes.ExitCodes; import xyz.vallat.louis.env.EnvironmentVariables; import java.sql.Connection; @@ -35,7 +36,7 @@ public final class DBManager { + DATABASE, USERNAME, PASSWORD); } catch (SQLException e) { logger.error("Could not connect to database. Reason: {}.", e.getMessage()); - System.exit(4); + System.exit(ExitCodes.CANNOT_CONNECT_TO_DB.getValue()); } return null; } @@ -57,7 +58,7 @@ public final class DBManager { importLanguageIfNeeded(connection); } catch (SQLException e) { logger.error("An error happened while initializing the database. Reason: {}", e.getMessage()); - System.exit(5); + System.exit(ExitCodes.CANNOT_INITIALIZE_DB.getValue()); } } diff --git a/src/main/java/xyz/vallat/louis/database/LanguageManager.java b/src/main/java/xyz/vallat/louis/database/LanguageManager.java index 9b9dbe2..022812f 100644 --- a/src/main/java/xyz/vallat/louis/database/LanguageManager.java +++ b/src/main/java/xyz/vallat/louis/database/LanguageManager.java @@ -5,6 +5,7 @@ import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVRecord; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import xyz.vallat.louis.codes.ExitCodes; import java.io.IOException; import java.io.InputStream; @@ -44,7 +45,7 @@ public final class LanguageManager { } } catch (SQLException | IOException | NoSuchAlgorithmException e) { logger.error("Cannot update the languages right now. Reason: {}", e.getMessage()); - if (storedHash == null || storedHash.isEmpty()) System.exit(7); + if (storedHash == null || storedHash.isEmpty()) System.exit(ExitCodes.CANNOT_INITIALIZE_LANGUAGES.getValue()); logger.warn("Using language already in database. Please contact this bot's administrator to fix this issue."); } } diff --git a/src/main/java/xyz/vallat/louis/database/PropertyManager.java b/src/main/java/xyz/vallat/louis/database/PropertyManager.java index 5d1ae87..7dfdcc9 100644 --- a/src/main/java/xyz/vallat/louis/database/PropertyManager.java +++ b/src/main/java/xyz/vallat/louis/database/PropertyManager.java @@ -2,6 +2,7 @@ package xyz.vallat.louis.database; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import xyz.vallat.louis.codes.ExitCodes; import java.sql.Connection; import java.sql.PreparedStatement; @@ -45,7 +46,7 @@ public final class PropertyManager { return null; } catch (SQLException e) { logger.error("Cannot get property right now. Reason: {}.", e.getMessage()); - System.exit(6); + System.exit(ExitCodes.CANNOT_GET_PROPERTY.getValue()); } return null; }