A module resource that fails to read throws with the reason

ModuleResourceFiles.read reports the file as before and then throws an IllegalStateException carrying the decoder's
message, so validators can say what is wrong instead of pointing at the log.
This commit is contained in:
piexel committed 2026-10-19 15:29:45 +02:00
1 parent 302166f115
commit 37851fee0c
3 files changed
+34 -6

No files matched your search

@@ -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<T> {
private static final String JSON_EXTENSION = ".json";
@@ -77,7 +77,10 @@ public final class ModuleResourceFiles<T> {
}
}
/** 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<T> {
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()) {
@@ -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)));
}
@@ -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