Trying to handle file name parsing error in backup engine

This commit is contained in:
Marc Baloup 2022-12-14 19:18:43 +01:00
parent 0d94361d38
commit d11631a942
Signed by: marcbal
GPG Key ID: BBC0FE3ABC30B893
2 changed files with 12 additions and 4 deletions

View File

@ -2,6 +2,7 @@ package fr.pandacube.lib.paper.modules.backup;
import java.io.File;
import java.time.LocalDateTime;
import java.time.format.DateTimeParseException;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.TreeMap;
@ -80,7 +81,13 @@ public abstract class BackupCleaner implements UnaryOperator<TreeSet<LocalDateTi
}
String dateTimeStr = filename.substring(0, filename.length() - 4);
LocalDateTime ldt = LocalDateTime.parse(dateTimeStr, CompressProcess.dateFileNameFormatter);
LocalDateTime ldt;
try {
ldt = LocalDateTime.parse(dateTimeStr, CompressProcess.dateFileNameFormatter);
} catch (DateTimeParseException e) {
Log.warning("Unable to parse file name to a date-time: " + file, e);
continue;
}
datedFiles.put(ldt, file);
}

View File

@ -18,7 +18,6 @@ import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Calendar;
import java.util.Date;
import java.util.function.BiPredicate;
@ -153,9 +152,11 @@ public abstract class CompressProcess implements Comparable<CompressProcess>, Ru
static DateTimeFormatter dateFileNameFormatter = new DateTimeFormatterBuilder()
.append(DateTimeFormatter.BASIC_ISO_DATE)
.appendValue(ChronoField.YEAR, 4)
.appendValue(ChronoField.MONTH_OF_YEAR, 2)
.appendValue(ChronoField.DAY_OF_MONTH, 2)
.appendLiteral('-')
.appendValue(ChronoField.HOUR_OF_DAY, 2) // there is no DateTimeFormatter.BASIC_ISO_TIME
.appendValue(ChronoField.HOUR_OF_DAY, 2)
.appendValue(ChronoField.MINUTE_OF_HOUR, 2)
.appendValue(ChronoField.SECOND_OF_MINUTE, 2)
.toFormatter();