FileConfigStore: Simplify error handling
Signed-off-by: Samuel Holland <samuel@sholland.org>
This commit is contained in:
parent
dfa4a2eb9d
commit
5ce7eba2bf
@ -34,10 +34,8 @@ public final class FileConfigStore implements ConfigStore {
|
|||||||
public Config create(final String name, final Config config) throws IOException {
|
public Config create(final String name, final Config config) throws IOException {
|
||||||
Log.d(TAG, "Creating configuration for tunnel " + name);
|
Log.d(TAG, "Creating configuration for tunnel " + name);
|
||||||
final File file = fileFor(name);
|
final File file = fileFor(name);
|
||||||
if (!file.createNewFile()) {
|
if (!file.createNewFile())
|
||||||
final String message = "Configuration file " + file.getName() + " already exists";
|
throw new IOException("Configuration file " + file.getName() + " already exists");
|
||||||
throw new IllegalStateException(message);
|
|
||||||
}
|
|
||||||
try (FileOutputStream stream = new FileOutputStream(file, false)) {
|
try (FileOutputStream stream = new FileOutputStream(file, false)) {
|
||||||
stream.write(config.toString().getBytes(StandardCharsets.UTF_8));
|
stream.write(config.toString().getBytes(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
@ -75,10 +73,8 @@ public final class FileConfigStore implements ConfigStore {
|
|||||||
public Config save(final String name, final Config config) throws IOException {
|
public Config save(final String name, final Config config) throws IOException {
|
||||||
Log.d(TAG, "Saving configuration for tunnel " + name);
|
Log.d(TAG, "Saving configuration for tunnel " + name);
|
||||||
final File file = fileFor(name);
|
final File file = fileFor(name);
|
||||||
if (!file.isFile()) {
|
if (!file.isFile())
|
||||||
final String message = "Configuration file " + file.getName() + " not found";
|
throw new FileNotFoundException("Configuration file " + file.getName() + " not found");
|
||||||
throw new FileNotFoundException(message);
|
|
||||||
}
|
|
||||||
try (FileOutputStream stream = new FileOutputStream(file, false)) {
|
try (FileOutputStream stream = new FileOutputStream(file, false)) {
|
||||||
stream.write(config.toString().getBytes(StandardCharsets.UTF_8));
|
stream.write(config.toString().getBytes(StandardCharsets.UTF_8));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user