From dc65b19a8f7da7eec349494fa9c7b4a8b120c905 Mon Sep 17 00:00:00 2001 From: akastijn Date: Sun, 12 Oct 2025 21:04:59 +0200 Subject: [PATCH] Add error handling and logging improvements for database settings loading process. --- .../alttd/altitudeweb/setup/Connection.java | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/database/src/main/java/com/alttd/altitudeweb/setup/Connection.java b/database/src/main/java/com/alttd/altitudeweb/setup/Connection.java index 9019d06..4ef04a4 100644 --- a/database/src/main/java/com/alttd/altitudeweb/setup/Connection.java +++ b/database/src/main/java/com/alttd/altitudeweb/setup/Connection.java @@ -17,6 +17,7 @@ import org.jetbrains.annotations.NotNull; import java.util.HashMap; import java.util.UUID; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; import java.util.function.Consumer; @Slf4j @@ -58,25 +59,46 @@ public class Connection { if (database == Databases.DEFAULT) { return loadDefaultDatabase(addMappers); } + + log.debug("Loading settings for database {}", database.getInternalName()); CompletableFuture settingsFuture = new CompletableFuture<>(); + getConnection(Databases.DEFAULT, (mapper -> mapper.addMapper(SettingsMapper.class))).thenApply(connection -> { - log.debug("Loading settings for database {}", database.getInternalName()); connection.runQuery(session -> { - log.debug("Running query to load settings for database"); - DatabaseSettings loadedSettings = session.getMapper(SettingsMapper.class).getSettings(database.getInternalName()); - if (loadedSettings == null) { - log.error("Failed to load settings for database {}", database.getInternalName()); + try { + log.debug("Running query to load settings for database {}", database.getInternalName()); + DatabaseSettings loadedSettings = session.getMapper(SettingsMapper.class).getSettings(database.getInternalName()); + + if (loadedSettings == null) { + log.error("Failed to load settings for database {}. No settings found in db_connection_settings table.", + database.getInternalName()); + settingsFuture.completeExceptionally(new IllegalStateException( + "Database settings for " + database.getInternalName() + " not found in db_connection_settings table")); + } else { + log.debug("Loaded settings for database {}: host={}, port={}, name={}", + database.getInternalName(), loadedSettings.host(), loadedSettings.port(), loadedSettings.name()); + settingsFuture.complete(loadedSettings); + } + } catch (Exception e) { + log.error("Error occurred while loading database settings for {}", database.getInternalName(), e); + settingsFuture.completeExceptionally(e); } - log.debug("Loaded settings {}", loadedSettings); - settingsFuture.complete(loadedSettings); }); return null; + }).exceptionally(ex -> { + log.error("Failed to access DEFAULT database to load settings for {}", database.getInternalName(), ex); + settingsFuture.completeExceptionally(ex); + return null; }); + return settingsFuture.thenApply(loadedSettings -> { log.debug("Storing connection for database {}", database.getInternalName()); Connection connection = new Connection(loadedSettings, addMappers); connections.put(database, connection); return connection; + }).exceptionally(ex -> { + log.error("Failed to create connection for database {}", database.getInternalName(), ex); + throw new CompletionException("Failed to initialize database connection for " + database.getInternalName(), ex); }); }