Skip to content
Snippets Groups Projects
Commit 213aede7 authored by Martin Vogel's avatar Martin Vogel
Browse files

refactor(config): change thrown exception on failed config loading to InitializationException

parent a9102368
No related branches found
No related tags found
No related merge requests found
package dev.fitko.fitconnect.client.factory; package dev.fitko.fitconnect.client.factory;
import dev.fitko.fitconnect.api.config.ApplicationConfig; import dev.fitko.fitconnect.api.config.ApplicationConfig;
import dev.fitko.fitconnect.api.exceptions.InitializationException;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.constructor.Constructor;
...@@ -18,16 +19,20 @@ public final class ApplicationConfigLoader { ...@@ -18,16 +19,20 @@ public final class ApplicationConfigLoader {
* @param configPath path to config file * @param configPath path to config file
* *
* @return ApplicationConfig * @return ApplicationConfig
* @throws IOException if the default config file could not be loaded * @throws InitializationException if the config file could not be loaded
*/ */
public static ApplicationConfig loadConfig(final Path configPath) throws IOException { public static ApplicationConfig loadConfig(final Path configPath) {
return loadConfig(Files.readString(configPath)); try {
return loadConfig(Files.readString(configPath));
} catch (final IOException e) {
throw new InitializationException(e.getMessage(), e);
}
} }
/** /**
* Load ApplicationConfig from string. * Load ApplicationConfig from string.
* *
* @param configYaml string of the yaml config file * @param configYaml string content of the yaml config file
* *
* @return ApplicationConfig * @return ApplicationConfig
*/ */
......
...@@ -2,6 +2,7 @@ package dev.fitko.fitconnect.client.factory; ...@@ -2,6 +2,7 @@ package dev.fitko.fitconnect.client.factory;
import dev.fitko.fitconnect.api.config.ApplicationConfig; import dev.fitko.fitconnect.api.config.ApplicationConfig;
import dev.fitko.fitconnect.api.config.SubscriberConfig; import dev.fitko.fitconnect.api.config.SubscriberConfig;
import dev.fitko.fitconnect.api.exceptions.InitializationException;
import dev.fitko.fitconnect.api.services.Sender; import dev.fitko.fitconnect.api.services.Sender;
import dev.fitko.fitconnect.api.services.Subscriber; import dev.fitko.fitconnect.api.services.Subscriber;
import dev.fitko.fitconnect.api.services.auth.OAuthService; import dev.fitko.fitconnect.api.services.auth.OAuthService;
...@@ -214,8 +215,9 @@ public final class ClientFactory { ...@@ -214,8 +215,9 @@ public final class ClientFactory {
final ApplicationConfig applicationConfig = ApplicationConfigLoader.loadConfig(pathToConfig); final ApplicationConfig applicationConfig = ApplicationConfigLoader.loadConfig(pathToConfig);
LOGGER.info("Using sdk environment config {} with {}", applicationConfig.getCurrentEnvironment(), applicationConfig); LOGGER.info("Using sdk environment config {} with {}", applicationConfig.getCurrentEnvironment(), applicationConfig);
return applicationConfig; return applicationConfig;
} catch (final IOException e) { } catch (final InitializationException e) {
throw new IllegalStateException("Config could not be loaded, please provide a 'config.yml' in the root path or in the ENV-var 'FIT_CONNECT_CONFIG'", e); LOGGER.error("Config could not be loaded, please provide a 'config.yml' in the root path or in the ENV-var 'FIT_CONNECT_CONFIG'");
throw e;
} }
} }
......
...@@ -25,7 +25,7 @@ class ApplicationConfigLoaderTest { ...@@ -25,7 +25,7 @@ class ApplicationConfigLoaderTest {
@Test @Test
void testLoadMissingConfigFile() { void testLoadMissingConfigFile() {
Assertions.assertThrows(IOException.class, () -> ApplicationConfigLoader.loadConfig(Path.of("/no/path"))); Assertions.assertThrows(InitializationException.class, () -> ApplicationConfigLoader.loadConfig(Path.of("/no/path")));
} }
@Test @Test
......
...@@ -6,6 +6,7 @@ import dev.fitko.fitconnect.api.config.EnvironmentName; ...@@ -6,6 +6,7 @@ import dev.fitko.fitconnect.api.config.EnvironmentName;
import dev.fitko.fitconnect.api.config.ResourcePaths; import dev.fitko.fitconnect.api.config.ResourcePaths;
import dev.fitko.fitconnect.api.config.SenderConfig; import dev.fitko.fitconnect.api.config.SenderConfig;
import dev.fitko.fitconnect.api.config.SubscriberConfig; import dev.fitko.fitconnect.api.config.SubscriberConfig;
import dev.fitko.fitconnect.api.exceptions.InitializationException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Map; import java.util.Map;
...@@ -17,8 +18,8 @@ class ClientFactoryTest { ...@@ -17,8 +18,8 @@ class ClientFactoryTest {
@Test @Test
void testMissingConfiguration() { void testMissingConfiguration() {
assertThrows(IllegalStateException.class, () -> ClientFactory.senderClient()); assertThrows(InitializationException.class, () -> ClientFactory.senderClient());
assertThrows(IllegalStateException.class, () -> ClientFactory.subscriberClient()); assertThrows(InitializationException.class, () -> ClientFactory.subscriberClient());
} }
@Test @Test
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment