Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit a8ee84d

Browse files
author
Aleksi Salmela
committed
Handle exceptions in SettingsIo.
1 parent 7d13b97 commit a8ee84d

1 file changed

Lines changed: 20 additions & 15 deletions

File tree

src/main/java/fi/helsinki/cs/tmc/cli/backend/SettingsIo.java

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,32 +117,37 @@ protected static Path getConfigDirectory() {
117117
return configPath.resolve(CONFIG_DIR);
118118
}
119119

120-
//TODO handle exceptions
121120
private static Path getAccountsFile(Path configRoot) {
122121
Path file = configRoot.resolve(ACCOUNTS_CONFIG);
123-
if (!Files.exists(configRoot)) {
124-
try {
125-
Files.createDirectories(configRoot).getParent();
126-
} catch (Exception e) { }
127-
try {
128-
Files.createFile(configRoot);
129-
} catch (Exception e) { }
122+
if (!requireConfigDirectory(configRoot)) {
123+
return null;
130124
}
131125
return file;
132126
}
133127

134-
//TODO handle exceptions
135128
private static Path getPropertiesFile(Path configRoot) {
136129
Path file = configRoot.resolve(PROPERTIES_CONFIG);
130+
if (!requireConfigDirectory(configRoot)) {
131+
return null;
132+
}
133+
return file;
134+
}
135+
136+
/**
137+
* Create the config directory if it doesn't already exist.
138+
* @param configRoot config directory
139+
* @return false if directory can't be created.
140+
*/
141+
private static boolean requireConfigDirectory(Path configRoot) {
137142
if (!Files.exists(configRoot)) {
138143
try {
139-
Files.createDirectories(configRoot).getParent();
140-
} catch (Exception e) { }
141-
try {
142-
Files.createFile(configRoot);
143-
} catch (Exception e) { }
144+
Files.createDirectories(configRoot);
145+
} catch (Exception e) {
146+
logger.error("Could not create config directory", e);
147+
return false;
148+
}
144149
}
145-
return file;
150+
return true;
146151
}
147152

148153
private static AccountList getHolderFromJson(Path file) {

0 commit comments

Comments
 (0)