diff --git a/core/src/main/java/art/arcane/iris/core/modules/ModuleResourceFiles.java b/core/src/main/java/art/arcane/iris/core/modules/ModuleResourceFiles.java index 9c92b95fe..a07439752 100644 --- a/core/src/main/java/art/arcane/iris/core/modules/ModuleResourceFiles.java +++ b/core/src/main/java/art/arcane/iris/core/modules/ModuleResourceFiles.java @@ -42,8 +42,8 @@ import java.util.stream.Stream; * A file is read with the pack's module decoder, which hands Iris's own classes to the pack's Gson, so a built-in * kind such as structures reads exactly as Iris's loaders read. A resource that is one of Iris's registrants gets its * key, file and pack data, as a loader would set them. A file that fails to read is reported with the line at fault, - * like a loader's, and gives no resource. Unknown keys are reported for Iris's own classes only: the check caches - * field names per class, which would keep a pack module's classes loaded after the pack closes. + * like a loader's, and the read throws with the reason. Unknown keys are reported for Iris's own classes only: the + * check caches field names per class, which would keep a pack module's classes loaded after the pack closes. */ public final class ModuleResourceFiles { private static final String JSON_EXTENSION = ".json"; @@ -77,7 +77,10 @@ public final class ModuleResourceFiles { } } - /** The file of {@code key} read into a new resource, or null when it fails to read, which is reported. */ + /** + * The file of {@code key} read into a new resource. A file that fails to read is reported, and the call throws an + * {@link IllegalStateException} whose message says what is wrong with it. + */ T read(String key) { File file = folder.resolve(key + JSON_EXTENSION).toFile(); String json = null; @@ -89,10 +92,15 @@ public final class ModuleResourceFiles { return resource; } catch (IOException | RuntimeException failure) { JsonSchemaValidator.reportLoadFailure(file, json, typeName, failure); - return null; + throw new IllegalStateException(reasonOf(failure), failure); } } + private static String reasonOf(Throwable failure) { + String message = failure.getMessage(); + return message == null || message.isBlank() ? failure.getClass().getSimpleName() : message; + } + private String keyOf(Path file) { String relative = folder.relativize(file).toString().replace(file.getFileSystem().getSeparator(), String.valueOf(FOLDER_SEPARATOR)); if (!relative.endsWith(JSON_EXTENSION) || relative.length() == JSON_EXTENSION.length()) { diff --git a/core/src/test/java/art/arcane/iris/core/modules/LootAndSpawnPackDecodingTest.java b/core/src/test/java/art/arcane/iris/core/modules/LootAndSpawnPackDecodingTest.java index f9528168d..b3b10f41a 100644 --- a/core/src/test/java/art/arcane/iris/core/modules/LootAndSpawnPackDecodingTest.java +++ b/core/src/test/java/art/arcane/iris/core/modules/LootAndSpawnPackDecodingTest.java @@ -31,6 +31,8 @@ import art.arcane.iris.builtin.spawn.SpawnGroup; import art.arcane.iris.builtin.spawn.SpawnWeather; import art.arcane.iris.builtin.spawn.Spawner; import art.arcane.iris.builtin.spawn.SpawnerSettings; +import art.arcane.iris.core.loader.IrisData; +import art.arcane.iris.engine.loot.LootResources; import art.arcane.volmlib.util.json.JSONObject; import com.google.gson.Gson; import com.volmit.FeatureAttachment.Target; @@ -56,6 +58,9 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * Files of the overworld pack exactly as they were before the loot and spawn fields became module settings (copied from @@ -270,6 +275,21 @@ public class LootAndSpawnPackDecodingTest { } } + @Test + public void aLootTableOfTheWrongShapeFailsToReadSayingWhy() throws IOException { + Path brokenPack = folder.newFolder("broken-pack").toPath(); + Files.createDirectories(brokenPack.resolve("loot")); + Files.writeString(brokenPack.resolve("loot/broken.json"), "{\"rarity\": [1], \"loot\": [{\"type\": \"stone\"}]}"); + IrisData data = mock(IrisData.class, RETURNS_DEEP_STUBS); + when(data.getDataFolder()).thenReturn(brokenPack.toFile()); + when(data.getModules().settings()).thenReturn(decoder); + + IllegalStateException failure = assertThrows(IllegalStateException.class, + () -> new ModuleResourceFiles<>(data, LootResources.TABLES).read("broken")); + + assertTrue(failure.getMessage(), failure.getMessage().contains("$.rarity")); + } + private JSONObject file(String relative) throws IOException { return new JSONObject(Files.readString(packFolder.resolve(relative))); } diff --git a/core/src/test/java/art/arcane/iris/engine/loot/LootValidatorTest.java b/core/src/test/java/art/arcane/iris/engine/loot/LootValidatorTest.java index c945ee979..f28f5a934 100644 --- a/core/src/test/java/art/arcane/iris/engine/loot/LootValidatorTest.java +++ b/core/src/test/java/art/arcane/iris/engine/loot/LootValidatorTest.java @@ -155,9 +155,9 @@ public class LootValidatorTest { public void aTableThatFailsToLoadIsReportedWithoutStoppingTheCheck() { table("fine", "{\"loot\":[{\"type\":\"stone\"}]}"); table("broken", "{\"loot\":[{\"type\":\"stone\"}]}"); - when(resources.find(LootResources.TABLES.key(new ResourceId("test", "broken")))).thenThrow(new IllegalStateException("unreadable")); + when(resources.find(LootResources.TABLES.key(new ResourceId("test", "broken")))).thenThrow(new IllegalStateException("Expected an int but was STRING at line 1 column 12 path $.rarity")); - assertEquals(List.of("Loot table 'broken' cannot be read; the log says what is wrong with the file."), errors()); + assertEquals(List.of("Loot table 'broken' cannot be read: Expected an int but was STRING at line 1 column 12 path $.rarity"), errors()); } @Test