mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-12-01 18:24:06 +00:00
Stop pack loading from looping on bad list entries
The snippet-type adapter fell back to defaults without consuming the broken value, so a list retried the same entry forever. Read the whole JSON value first, then convert it, and name the type, path and exception message in the error. v3: 4b437207c
This commit is contained in:
1 parent
23bed57728
commit
843dcf702f
2 files changed
+93
-2
No files matched your search
@@ -22,6 +22,7 @@ import com.google.gson.ExclusionStrategy;
|
|||||||
import com.google.gson.FieldAttributes;
|
import com.google.gson.FieldAttributes;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.GsonBuilder;
|
import com.google.gson.GsonBuilder;
|
||||||
|
import com.google.gson.JsonElement;
|
||||||
import com.google.gson.Strictness;
|
import com.google.gson.Strictness;
|
||||||
import com.google.gson.TypeAdapter;
|
import com.google.gson.TypeAdapter;
|
||||||
import com.google.gson.TypeAdapterFactory;
|
import com.google.gson.TypeAdapterFactory;
|
||||||
@@ -727,10 +728,12 @@ public class IrisData implements ExclusionStrategy, TypeAdapterFactory {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
String path = reader.getPath();
|
||||||
|
JsonElement value = gson.getAdapter(JsonElement.class).read(reader);
|
||||||
try {
|
try {
|
||||||
return adapter.read(reader);
|
return adapter.fromJsonTree(value);
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
IrisLogging.error("Failed to read " + typeToken.getRawType().getCanonicalName() + "... faking objects a little to load the file at least.");
|
IrisLogging.error("Failed to read %s at %s (%s), using defaults", typeToken.getRawType().getSimpleName(), path, e.getMessage());
|
||||||
IrisLogging.reportError(e);
|
IrisLogging.reportError(e);
|
||||||
try {
|
try {
|
||||||
return (T) typeToken.getRawType().getConstructor().newInstance();
|
return (T) typeToken.getRawType().getConstructor().newInstance();
|
||||||
|
|||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package art.arcane.iris.core.loader;
|
||||||
|
|
||||||
|
import art.arcane.iris.engine.framework.PreservationRegistry;
|
||||||
|
import art.arcane.iris.engine.object.noise.IrisStyledRange;
|
||||||
|
import art.arcane.iris.spi.IrisPlatform;
|
||||||
|
import art.arcane.iris.spi.IrisPlatforms;
|
||||||
|
import art.arcane.iris.spi.IrisServices;
|
||||||
|
import art.arcane.iris.spi.LogLevel;
|
||||||
|
import com.google.gson.reflect.TypeToken;
|
||||||
|
import org.junit.After;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Rule;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.rules.TemporaryFolder;
|
||||||
|
|
||||||
|
import java.lang.reflect.Type;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.ArgumentMatchers.anyString;
|
||||||
|
import static org.mockito.Mockito.doAnswer;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
|
||||||
|
public class IrisDataSnippetListReadTest {
|
||||||
|
private static final Type STYLED_RANGES = new TypeToken<List<IrisStyledRange>>() {
|
||||||
|
}.getType();
|
||||||
|
private static final long READ_TIMEOUT_MILLIS = 10_000;
|
||||||
|
|
||||||
|
@Rule
|
||||||
|
public TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||||
|
|
||||||
|
private final List<String> errors = new ArrayList<>();
|
||||||
|
private IrisPlatform previousPlatform;
|
||||||
|
private IrisData data;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void openDataAndCaptureErrors() throws Exception {
|
||||||
|
IrisServices.register(PreservationRegistry.class, mock(PreservationRegistry.class));
|
||||||
|
previousPlatform = IrisPlatforms.isBound() ? IrisPlatforms.get() : null;
|
||||||
|
IrisPlatforms.unbind();
|
||||||
|
IrisPlatform platform = mock(IrisPlatform.class);
|
||||||
|
doAnswer(invocation -> {
|
||||||
|
if (invocation.getArgument(0, LogLevel.class) == LogLevel.ERROR) {
|
||||||
|
errors.add(invocation.getArgument(1, String.class));
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}).when(platform).log(any(LogLevel.class), anyString());
|
||||||
|
IrisPlatforms.bind(platform);
|
||||||
|
data = IrisData.openRuntime(temporaryFolder.newFolder("pack"));
|
||||||
|
errors.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
public void closeDataAndRestorePlatform() {
|
||||||
|
data.close();
|
||||||
|
IrisPlatforms.unbind();
|
||||||
|
if (previousPlatform != null) {
|
||||||
|
IrisPlatforms.bind(previousPlatform);
|
||||||
|
}
|
||||||
|
IrisServices.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(timeout = READ_TIMEOUT_MILLIS)
|
||||||
|
public void badListEntryFallsBackToDefaultsAndTheRestOfTheListLoads() {
|
||||||
|
String json = "[{\"min\": 1, \"max\": 2}, {\"min\": \"not a number\"}, {\"min\": 3, \"max\": 4}]";
|
||||||
|
|
||||||
|
List<IrisStyledRange> ranges = data.getGson().fromJson(json, STYLED_RANGES);
|
||||||
|
|
||||||
|
assertEquals(3, ranges.size());
|
||||||
|
assertEquals(1, ranges.get(0).getMin(), 0);
|
||||||
|
assertEquals(new IrisStyledRange().getMin(), ranges.get(1).getMin(), 0);
|
||||||
|
assertEquals(3, ranges.get(2).getMin(), 0);
|
||||||
|
assertEquals(4, ranges.get(2).getMax(), 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(timeout = READ_TIMEOUT_MILLIS)
|
||||||
|
public void badEntryErrorNamesTheTypeThePathAndTheException() {
|
||||||
|
data.getGson().fromJson("[{\"min\": 1}, {\"min\": \"not a number\"}]", STYLED_RANGES);
|
||||||
|
|
||||||
|
String error = errors.getFirst();
|
||||||
|
assertTrue(error, error.contains("IrisStyledRange"));
|
||||||
|
assertTrue(error, error.contains("$[1]"));
|
||||||
|
assertTrue(error, error.contains("not a number"));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user