Grouped all exit codes in one enumeration

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2020-10-28 00:01:13 +01:00
parent 485abe29e3
commit ef85f0cdd6
5 changed files with 31 additions and 5 deletions

View File

@ -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)

View File

@ -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;
}
}

View File

@ -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());
}
}

View File

@ -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.");
}
}

View File

@ -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;
}