Enhance error handling

This commit is contained in:
Rene Pfeuffer
2019-12-05 10:10:26 +01:00
parent b2bbd1d9b5
commit 503bd8fa16

View File

@@ -56,14 +56,12 @@ public final class CopyOnWrite {
Path backupFile = backupOriginalFile(targetFile);
try {
Files.move(temporaryFile, targetFile);
if (backupFile != null) {
Files.delete(backupFile);
}
} catch (IOException e) {
LOG.error("Error renaming temporary file {} to target file {}", temporaryFile, targetFile);
restoreBackup(targetFile, backupFile);
throw new StoreException("could rename temporary file to target file", e);
}
deleteBackupFile(backupFile);
}
private static Path backupOriginalFile(Path targetFile) {
@@ -82,6 +80,17 @@ public final class CopyOnWrite {
}
}
private static void deleteBackupFile(Path backupFile) {
if (backupFile != null) {
try {
Files.delete(backupFile);
} catch (IOException e) {
LOG.warn("Could not delete backup file {}", backupFile);
throw new StoreException("could not delete backup file", e);
}
}
}
private static void restoreBackup(Path targetFile, Path backupFile) {
if (backupFile != null) {
try {