feat(error): simplify error management regarding data-related operations
This commit is contained in:
parent
df988dbf87
commit
5197fefdaf
9 changed files with 24 additions and 32 deletions
|
@ -8,6 +8,10 @@ public class ActionResult {
|
|||
return new ActionResult();
|
||||
}
|
||||
|
||||
public static ActionResult error() {
|
||||
return new ActionResult(null);
|
||||
}
|
||||
|
||||
public static ActionResult error(String errorMessage) {
|
||||
return new ActionResult(errorMessage);
|
||||
}
|
||||
|
|
|
@ -129,7 +129,7 @@ public class AppearanceManager {
|
|||
reset();
|
||||
return ActionResult.error(I18NDict.Error.MOJANG_NAME);
|
||||
} catch (InterruptedException e) {
|
||||
return ActionResult.error(I18NDict.Error.GENERIC);
|
||||
return ActionResult.error("Unknown error");
|
||||
}
|
||||
}
|
||||
return ActionResult.ok();
|
||||
|
|
|
@ -4,13 +4,10 @@ public class I18NDict {
|
|||
public static class Error {
|
||||
public static final String ERROR_KEY = "error.";
|
||||
|
||||
public static final String GENERIC = ERROR_KEY + "generic";
|
||||
public static final String PERMISSION = ERROR_KEY + "permission";
|
||||
public static final String CACHE = ERROR_KEY + "cache";
|
||||
public static final String MOJANG_NAME = ERROR_KEY + "mojang_name";
|
||||
public static final String MOJANG_SKIN = ERROR_KEY + "mojang_skin";
|
||||
public static final String DATABASE = ERROR_KEY + "database";
|
||||
public static final String JSON = ERROR_KEY + "json";
|
||||
}
|
||||
|
||||
public static class Event {
|
||||
|
|
|
@ -77,12 +77,12 @@ public class PlayerDataStore {
|
|||
}
|
||||
|
||||
public ActionResult saveData(Player player) {
|
||||
if (storage.isError()) return ActionResult.error(I18NDict.Error.DATABASE);
|
||||
if (cache.isError()) return ActionResult.error(I18NDict.Error.CACHE);
|
||||
if (!cache.isCached(player.getUniqueId())) return ActionResult.error(I18NDict.Error.CACHE);
|
||||
if (storage.isError()) return ActionResult.error();
|
||||
if (cache.isError()) return ActionResult.error();
|
||||
if (!cache.isCached(player.getUniqueId())) return ActionResult.error();
|
||||
|
||||
final Optional<NickoProfile> cachedProfile = cache.retrieve(player.getUniqueId());
|
||||
if (cachedProfile.isEmpty()) return ActionResult.error(I18NDict.Error.CACHE);
|
||||
if (cachedProfile.isEmpty()) return ActionResult.error();
|
||||
|
||||
cache.delete(player.getUniqueId());
|
||||
return storage.store(player.getUniqueId(), cachedProfile.get());
|
||||
|
|
|
@ -5,7 +5,6 @@ import com.google.gson.GsonBuilder;
|
|||
import xyz.ineanto.nicko.NickoBukkit;
|
||||
import xyz.ineanto.nicko.appearance.ActionResult;
|
||||
import xyz.ineanto.nicko.profile.NickoProfile;
|
||||
import xyz.ineanto.nicko.i18n.I18NDict;
|
||||
import xyz.ineanto.nicko.storage.Storage;
|
||||
import xyz.ineanto.nicko.storage.StorageProvider;
|
||||
|
||||
|
@ -42,12 +41,12 @@ public class JSONStorage extends Storage {
|
|||
}
|
||||
} catch (IOException e) {
|
||||
logger.warning("Could not write to file.");
|
||||
return ActionResult.error(I18NDict.Error.JSON);
|
||||
return ActionResult.error();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
logger.warning("Could not create file.");
|
||||
return ActionResult.error(I18NDict.Error.JSON);
|
||||
return ActionResult.error();
|
||||
}
|
||||
|
||||
return ActionResult.ok();
|
||||
|
@ -81,7 +80,7 @@ public class JSONStorage extends Storage {
|
|||
if (file.delete() || !file.exists()) {
|
||||
return ActionResult.ok();
|
||||
}
|
||||
return ActionResult.error(I18NDict.Error.JSON);
|
||||
return ActionResult.error();
|
||||
}
|
||||
|
||||
private boolean checkFileExists(File file) throws IOException {
|
||||
|
|
|
@ -2,7 +2,6 @@ package xyz.ineanto.nicko.storage.mariadb;
|
|||
|
||||
import xyz.ineanto.nicko.appearance.ActionResult;
|
||||
import xyz.ineanto.nicko.config.Configuration;
|
||||
import xyz.ineanto.nicko.i18n.I18NDict;
|
||||
import xyz.ineanto.nicko.i18n.Locale;
|
||||
import xyz.ineanto.nicko.profile.NickoProfile;
|
||||
import xyz.ineanto.nicko.storage.Storage;
|
||||
|
@ -36,7 +35,7 @@ public class MariaDBStorage extends Storage {
|
|||
@Override
|
||||
public ActionResult store(UUID uuid, NickoProfile profile) {
|
||||
final Connection connection = getProvider().getConnection();
|
||||
if (connection == null) return ActionResult.error(I18NDict.Error.DATABASE);
|
||||
if (connection == null) return ActionResult.error();
|
||||
|
||||
try {
|
||||
final PreparedStatement statement = isStored(uuid) ?
|
||||
|
@ -45,7 +44,7 @@ public class MariaDBStorage extends Storage {
|
|||
return ActionResult.ok();
|
||||
} catch (SQLException e) {
|
||||
logger.warning("Couldn't send SQL Request: " + e.getMessage());
|
||||
return ActionResult.error(I18NDict.Error.DATABASE);
|
||||
return ActionResult.error();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -103,17 +102,17 @@ public class MariaDBStorage extends Storage {
|
|||
@Override
|
||||
public ActionResult delete(UUID uuid) {
|
||||
final Connection connection = getProvider().getConnection();
|
||||
if (connection == null) return ActionResult.error(I18NDict.Error.DATABASE);
|
||||
if (connection == null) return ActionResult.error();
|
||||
|
||||
try {
|
||||
final String sql = "DELETE FROM nicko.DATA WHERE uuid = ?";
|
||||
final PreparedStatement statement = connection.prepareStatement(sql);
|
||||
statement.setString(1, uuid.toString());
|
||||
int rows = statement.executeUpdate();
|
||||
return (rows == 1 ? ActionResult.ok() : ActionResult.error(I18NDict.Error.DATABASE));
|
||||
return (rows == 1 ? ActionResult.ok() : ActionResult.error());
|
||||
} catch (SQLException e) {
|
||||
logger.warning("Couldn't delete profile: " + e.getMessage());
|
||||
return ActionResult.error(I18NDict.Error.DATABASE);
|
||||
return ActionResult.error();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,7 +2,6 @@ package xyz.ineanto.nicko.storage.mysql;
|
|||
|
||||
import xyz.ineanto.nicko.appearance.ActionResult;
|
||||
import xyz.ineanto.nicko.config.Configuration;
|
||||
import xyz.ineanto.nicko.i18n.I18NDict;
|
||||
import xyz.ineanto.nicko.i18n.Locale;
|
||||
import xyz.ineanto.nicko.profile.NickoProfile;
|
||||
import xyz.ineanto.nicko.storage.Storage;
|
||||
|
@ -36,7 +35,7 @@ public class MySQLStorage extends Storage {
|
|||
@Override
|
||||
public ActionResult store(UUID uuid, NickoProfile profile) {
|
||||
final Connection connection = getProvider().getConnection();
|
||||
if (connection == null) return ActionResult.error(I18NDict.Error.DATABASE);
|
||||
if (connection == null) return ActionResult.error();
|
||||
|
||||
try {
|
||||
final PreparedStatement statement = isStored(uuid) ?
|
||||
|
@ -45,7 +44,7 @@ public class MySQLStorage extends Storage {
|
|||
return ActionResult.ok();
|
||||
} catch (SQLException e) {
|
||||
logger.warning("Couldn't send SQL Request: " + e.getMessage());
|
||||
return ActionResult.error(I18NDict.Error.DATABASE);
|
||||
return ActionResult.error();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -103,17 +102,17 @@ public class MySQLStorage extends Storage {
|
|||
@Override
|
||||
public ActionResult delete(UUID uuid) {
|
||||
final Connection connection = getProvider().getConnection();
|
||||
if (connection == null) return ActionResult.error(I18NDict.Error.DATABASE);
|
||||
if (connection == null) return ActionResult.error();
|
||||
|
||||
try {
|
||||
final String sql = "DELETE FROM nicko.DATA WHERE uuid = ?";
|
||||
final PreparedStatement statement = connection.prepareStatement(sql);
|
||||
statement.setString(1, uuid.toString());
|
||||
int rows = statement.executeUpdate();
|
||||
return (rows == 1 ? ActionResult.ok() : ActionResult.error(I18NDict.Error.DATABASE));
|
||||
return (rows == 1 ? ActionResult.ok() : ActionResult.error());
|
||||
} catch (SQLException e) {
|
||||
logger.warning("Couldn't delete profile: " + e.getMessage());
|
||||
return ActionResult.error(I18NDict.Error.DATABASE);
|
||||
return ActionResult.error();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,17 +1,14 @@
|
|||
# Nicko ${version} - Language File:
|
||||
|
||||
# Specifies the configuration version, don't change.
|
||||
version: "1.1.3"
|
||||
version: "1.1.4"
|
||||
|
||||
error:
|
||||
generic: "An unknown error occurred."
|
||||
permission: "§cYou do not have the required permission."
|
||||
invalid_username: "§cThis is not a valid Minecraft username."
|
||||
mojang_name: "There is no Minecraft account with this name."
|
||||
mojang_skin: "This Minecraft account has no skin."
|
||||
cache: "Unable to get skin from the cache."
|
||||
sql: "SQL Error"
|
||||
json: "JSON Error"
|
||||
|
||||
event:
|
||||
settings:
|
||||
|
|
|
@ -1,17 +1,14 @@
|
|||
# Nicko ${version} - Fichier de langue:
|
||||
|
||||
# Précise la version de la configuration, ne pas changer.
|
||||
version: "1.1.3"
|
||||
version: "1.1.4"
|
||||
|
||||
error:
|
||||
generic: "Une erreur inconnue c'est produite."
|
||||
permission: "§cVous ne possédez pas la permission."
|
||||
invalid_username: "§cLe pseudo n'est pas un pseudo Minecraft valide."
|
||||
mojang_name: "Compte Minecraft inexistant"
|
||||
mojang_skin: "Skin Minecraft invalide"
|
||||
cache: "Erreur du cache"
|
||||
database: "Erreur SQL"
|
||||
json: "Erreur JSON"
|
||||
|
||||
event:
|
||||
settings:
|
||||
|
|
Loading…
Reference in a new issue