Skip to content

Commit 878b292

Browse files
authored
Gracefully handle corrupted manifest files (#337)
1 parent 3674195 commit 878b292

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/main/java/me/itzg/helpers/files/Manifests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.itzg.helpers.files;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
34
import java.io.IOException;
45
import java.nio.file.Files;
56
import java.nio.file.Path;
@@ -10,9 +11,11 @@
1011
import java.util.function.Consumer;
1112
import java.util.stream.Collectors;
1213
import java.util.stream.Stream;
14+
import lombok.extern.slf4j.Slf4j;
1315
import me.itzg.helpers.json.ObjectMappers;
1416
import org.slf4j.Logger;
1517

18+
@Slf4j
1619
public class Manifests {
1720

1821
private static final String SUFFIX = ".json";
@@ -112,13 +115,17 @@ public static boolean allFilesPresent(Path basePath, BaseManifest manifest, List
112115
*
113116
* @param outputDir directory where manifest and other module files are based
114117
* @param id module identifier, such as "fabric"
118+
* @return the loaded manifest file or null if it didn't exist or was invalid content
115119
*/
116120
public static <M extends BaseManifest> M load(Path outputDir, String id, Class<M> manifestClass) {
117121
final Path manifestPath = buildManifestPath(outputDir, id);
118122
if (Files.exists(manifestPath)) {
119123
final M manifest;
120124
try {
121125
manifest = ObjectMappers.defaultMapper().readValue(manifestPath.toFile(), manifestClass);
126+
} catch (JsonProcessingException e) {
127+
log.error("Failed to parse existing manifest file {}", manifestPath, e);
128+
return null;
122129
} catch (IOException e) {
123130
throw new ManifestException("Failed to load existing manifest from "+manifestPath, e);
124131
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package me.itzg.helpers.files;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.io.IOException;
6+
import java.nio.file.Files;
7+
import java.nio.file.Path;
8+
import java.util.Collections;
9+
import lombok.Getter;
10+
import lombok.experimental.SuperBuilder;
11+
import lombok.extern.jackson.Jacksonized;
12+
import org.apache.commons.lang3.RandomStringUtils;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.io.TempDir;
15+
16+
class ManifestsTest {
17+
18+
@TempDir
19+
Path tempDir;
20+
21+
@Getter @SuperBuilder @Jacksonized
22+
static class EmptyManifest extends BaseManifest {
23+
24+
}
25+
26+
@Test
27+
void loadFailsGracefullyWhenInvalid() throws IOException {
28+
final String id = RandomStringUtils.randomAlphabetic(5);
29+
30+
final Path manifestFile = tempDir.resolve(String.format(".%s-manifest.json", id));
31+
Files.write(manifestFile, Collections.singletonList("not json"));
32+
33+
final EmptyManifest manifest = Manifests.load(tempDir, id, EmptyManifest.class);
34+
assertThat(manifest).isNull();
35+
}
36+
}

0 commit comments

Comments
 (0)