Skip to content

Commit

Permalink
Gracefully handle corrupted manifest files (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
itzg authored Nov 10, 2023
1 parent 3674195 commit 878b292
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/java/me/itzg/helpers/files/Manifests.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.itzg.helpers.files;

import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -10,9 +11,11 @@
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import lombok.extern.slf4j.Slf4j;
import me.itzg.helpers.json.ObjectMappers;
import org.slf4j.Logger;

@Slf4j
public class Manifests {

private static final String SUFFIX = ".json";
Expand Down Expand Up @@ -112,13 +115,17 @@ public static boolean allFilesPresent(Path basePath, BaseManifest manifest, List
*
* @param outputDir directory where manifest and other module files are based
* @param id module identifier, such as "fabric"
* @return the loaded manifest file or null if it didn't exist or was invalid content
*/
public static <M extends BaseManifest> M load(Path outputDir, String id, Class<M> manifestClass) {
final Path manifestPath = buildManifestPath(outputDir, id);
if (Files.exists(manifestPath)) {
final M manifest;
try {
manifest = ObjectMappers.defaultMapper().readValue(manifestPath.toFile(), manifestClass);
} catch (JsonProcessingException e) {
log.error("Failed to parse existing manifest file {}", manifestPath, e);
return null;
} catch (IOException e) {
throw new ManifestException("Failed to load existing manifest from "+manifestPath, e);
}
Expand Down
36 changes: 36 additions & 0 deletions src/test/java/me/itzg/helpers/files/ManifestsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me.itzg.helpers.files;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.jackson.Jacksonized;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

class ManifestsTest {

@TempDir
Path tempDir;

@Getter @SuperBuilder @Jacksonized
static class EmptyManifest extends BaseManifest {

}

@Test
void loadFailsGracefullyWhenInvalid() throws IOException {
final String id = RandomStringUtils.randomAlphabetic(5);

final Path manifestFile = tempDir.resolve(String.format(".%s-manifest.json", id));
Files.write(manifestFile, Collections.singletonList("not json"));

final EmptyManifest manifest = Manifests.load(tempDir, id, EmptyManifest.class);
assertThat(manifest).isNull();
}
}

0 comments on commit 878b292

Please sign in to comment.