From 2d1b53258c13a8b4638b8974a86c7a21157f0550 Mon Sep 17 00:00:00 2001 From: gnembon Date: Tue, 9 Mar 2021 13:29:20 -0400 Subject: [PATCH] simplify error messages --- src/main/java/carpet/script/api/Auxiliary.java | 14 +++++++++----- src/main/java/carpet/script/api/Inventories.java | 2 +- src/main/java/carpet/script/api/Scoreboards.java | 2 +- src/main/java/carpet/script/api/WorldAccess.java | 12 ++++++------ .../carpet/script/exception/ThrowStatement.java | 8 ++++---- .../java/carpet/script/utils/ShapeDispatcher.java | 2 +- src/main/java/carpet/script/value/BlockValue.java | 2 +- .../carpet/script/value/NBTSerializableValue.java | 2 +- .../java/carpet/script/value/ValueConversions.java | 2 +- 9 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/main/java/carpet/script/api/Auxiliary.java b/src/main/java/carpet/script/api/Auxiliary.java index 63ebc142..39192581 100644 --- a/src/main/java/carpet/script/api/Auxiliary.java +++ b/src/main/java/carpet/script/api/Auxiliary.java @@ -162,10 +162,11 @@ public class Auxiliary { { expression.addLazyFunction("sound", -1, (c, t, lv) -> { CarpetContext cc = (CarpetContext)c; - Identifier soundName = new Identifier(lv.get(0).evalValue(c).getString()); + String rawString = lv.get(0).evalValue(c).getString(); + Identifier soundName = new Identifier(rawString); Vector3Argument locator = Vector3Argument.findIn(cc, lv, 1); if (Registry.SOUND_EVENT.get(soundName) == null) - throw new ThrowStatement("No such sound: "+soundName.getPath(), Throwables.UNKNOWN_SOUND); + throw new ThrowStatement(rawString, Throwables.UNKNOWN_SOUND); float volume = 1.0F; float pitch = 1.0F; SoundCategory mixer = SoundCategory.MASTER; @@ -861,7 +862,7 @@ public class Auxiliary { state = ((CarpetScriptHost) c.host).readFileTag(fdesc.getLeft(), fdesc.getRight()); } catch (CrashException e) { - throw new ThrowStatement("Couldn't read NBT data", Throwables.NBT_READ); + throw new ThrowStatement((fdesc.getRight()?"shared/":"")+ fdesc.getLeft(), Throwables.NBT_READ); } if (state == null) return LazyValue.NULL; @@ -879,7 +880,10 @@ public class Auxiliary { Throwable exc = e; if(e.getCause() != null) exc = e.getCause(); - throw new ThrowStatement("Failed to read JSON file: "+exc.getMessage(), Throwables.JSON_READ); + throw new ThrowStatement(MapValue.wrap(ImmutableMap.of( + StringValue.of("error"), StringValue.of(exc.getMessage()), + StringValue.of("path"), StringValue.of((fdesc.getRight()?"shared/":"")+ fdesc.getLeft()) + )), Throwables.JSON_READ); } Value parsedJson = GSON.fromJson(json, Value.class); if (parsedJson == null) @@ -973,7 +977,7 @@ public class Auxiliary { state = ((CarpetScriptHost)((CarpetContext)c).host).readFileTag(file, shared); } catch (CrashException e) { - throw new ThrowStatement("Failed to read App data", Throwables.NBT_READ); + throw new ThrowStatement("app data", Throwables.NBT_READ); } if (state == null) return (cc, tt) -> Value.NULL; diff --git a/src/main/java/carpet/script/api/Inventories.java b/src/main/java/carpet/script/api/Inventories.java index e3ad4353..96dd35e1 100644 --- a/src/main/java/carpet/script/api/Inventories.java +++ b/src/main/java/carpet/script/api/Inventories.java @@ -193,7 +193,7 @@ public class Inventories { String itemStr = lv.get(0).evalValue(c).getString(); Item item; Identifier id = new Identifier(itemStr); - item = Registry.ITEM.getOrEmpty(id).orElseThrow(() -> new ThrowStatement("Incorrect item: "+itemStr, Throwables.UNKNOWN_ITEM)); + item = Registry.ITEM.getOrEmpty(id).orElseThrow(() -> new ThrowStatement(itemStr, Throwables.UNKNOWN_ITEM)); if (!item.hasRecipeRemainder()) return LazyValue.NULL; Value ret = new StringValue(NBTSerializableValue.nameFromRegistryId(Registry.ITEM.getId(item.getRecipeRemainder()))); diff --git a/src/main/java/carpet/script/api/Scoreboards.java b/src/main/java/carpet/script/api/Scoreboards.java index c58f7c10..f0a2e598 100644 --- a/src/main/java/carpet/script/api/Scoreboards.java +++ b/src/main/java/carpet/script/api/Scoreboards.java @@ -133,7 +133,7 @@ public class Scoreboards { criterion = ScoreboardCriterion.createStatCriterion(critetionName).orElse(null); if (criterion==null) { - throw new ThrowStatement("Unknown scoreboard criterion: "+critetionName, Throwables.UNKNOWN_CRITERION); + throw new ThrowStatement(critetionName, Throwables.UNKNOWN_CRITERION); } } diff --git a/src/main/java/carpet/script/api/WorldAccess.java b/src/main/java/carpet/script/api/WorldAccess.java index 36652027..ff8640fb 100644 --- a/src/main/java/carpet/script/api/WorldAccess.java +++ b/src/main/java/carpet/script/api/WorldAccess.java @@ -339,7 +339,7 @@ public class WorldAccess { if (!"any".equals(poiType)) { PointOfInterestType type = Registry.POINT_OF_INTEREST_TYPE.getOrEmpty(new Identifier(poiType)) - .orElseThrow(() -> new ThrowStatement("Unknown POI type", Throwables.UNKNOWN_POI_TYPE)); + .orElseThrow(() -> new ThrowStatement(poiType, Throwables.UNKNOWN_POI_TYPE)); condition = (tt) -> tt == type; } if (locator.offset + 2 < lv.size()) @@ -394,7 +394,7 @@ public class WorldAccess { } String poiTypeString = poi.getString().toLowerCase(Locale.ROOT); PointOfInterestType type = Registry.POINT_OF_INTEREST_TYPE.getOrEmpty(new Identifier(poiTypeString)) - .orElseThrow(() -> new ThrowStatement("Unknown POI type: "+poiTypeString, Throwables.UNKNOWN_POI_TYPE)); + .orElseThrow(() -> new ThrowStatement(poiTypeString, Throwables.UNKNOWN_POI_TYPE)); int occupancy = 0; if (locator.offset + 1 < lv.size()) { @@ -874,7 +874,7 @@ public class WorldAccess { playerBreak = true; String itemString = val.getString(); item = Registry.ITEM.getOrEmpty(new Identifier(itemString)) - .orElseThrow(() -> new ThrowStatement("Incorrect item: " + itemString, Throwables.UNKNOWN_ITEM)); + .orElseThrow(() -> new ThrowStatement(itemString, Throwables.UNKNOWN_ITEM)); } } CompoundTag tag = null; @@ -1182,7 +1182,7 @@ public class WorldAccess { String biomeName = lv.get(locator.offset+0).evalValue(c).getString(); // from locatebiome command code Biome biome = cc.s.getMinecraftServer().getRegistryManager().get(Registry.BIOME_KEY).getOrEmpty(new Identifier(biomeName)) - .orElseThrow(() -> new ThrowStatement("Unknown biome: "+biomeName, Throwables.UNKNOWN_BIOME)); + .orElseThrow(() -> new ThrowStatement(biomeName, Throwables.UNKNOWN_BIOME)); boolean doImmediateUpdate = true; if (lv.size() > locator.offset+1) { @@ -1252,7 +1252,7 @@ public class WorldAccess { { String reqString = requested.getString(); structure = Registry.STRUCTURE_FEATURE.getOrEmpty(new Identifier(reqString)) - .orElseThrow(() -> new ThrowStatement("Unknown structure: " + reqString, Throwables.UNKNOWN_STRUCTURE)); + .orElseThrow(() -> new ThrowStatement(reqString, Throwables.UNKNOWN_STRUCTURE)); } if (lv.size() > locator.offset+1) { @@ -1332,7 +1332,7 @@ public class WorldAccess { throw new InternalExpressionException("'set_structure requires at least position and a structure name"); String structureName = lv.get(locator.offset).evalValue(c).getString().toLowerCase(Locale.ROOT); ConfiguredStructureFeature configuredStructure = FeatureGenerator.resolveConfiguredStructure(structureName, world, pos); - if (configuredStructure == null) throw new ThrowStatement("Unknown structure: "+structureName, Throwables.UNKNOWN_STRUCTURE); + if (configuredStructure == null) throw new ThrowStatement(structureName, Throwables.UNKNOWN_STRUCTURE); // good 'ol pointer Value[] result = new Value[]{Value.NULL}; // technically a world modification. Even if we could let it slide, we will still park it diff --git a/src/main/java/carpet/script/exception/ThrowStatement.java b/src/main/java/carpet/script/exception/ThrowStatement.java index 01bf9ccf..74dfa343 100644 --- a/src/main/java/carpet/script/exception/ThrowStatement.java +++ b/src/main/java/carpet/script/exception/ThrowStatement.java @@ -29,15 +29,15 @@ public class ThrowStatement extends InternalExpressionException * Conveniently creates a value from the {@code value} String * to be used easily in Java code * @param message The message to display when not handled - * @param thrownExceptionType An {@link Throwables} containing the inheritance data + * @param type An {@link Throwables} containing the inheritance data * for this exception. When throwing from Java, * those exceptions should be pre-registered. */ - public ThrowStatement(String message, Throwables thrownExceptionType) + public ThrowStatement(String message, Throwables type) { - super(message); + super(type.getId()); this.exceptionData = StringValue.of(message); - this.thrownExceptionType = thrownExceptionType; + this.thrownExceptionType = type; } @Override diff --git a/src/main/java/carpet/script/utils/ShapeDispatcher.java b/src/main/java/carpet/script/utils/ShapeDispatcher.java index 84aab23a..87a153f2 100644 --- a/src/main/java/carpet/script/utils/ShapeDispatcher.java +++ b/src/main/java/carpet/script/utils/ShapeDispatcher.java @@ -158,7 +158,7 @@ public class ShapeDispatcher } catch (CommandSyntaxException e) { - throw new ThrowStatement("No such particle: "+name, Throwables.UNKNOWN_PARTICLE); + throw new ThrowStatement(name, Throwables.UNKNOWN_PARTICLE); } particleCache.put(name, particle); return particle; diff --git a/src/main/java/carpet/script/value/BlockValue.java b/src/main/java/carpet/script/value/BlockValue.java index fe26f57f..27d21cdd 100644 --- a/src/main/java/carpet/script/value/BlockValue.java +++ b/src/main/java/carpet/script/value/BlockValue.java @@ -76,7 +76,7 @@ public class BlockValue extends Value catch (CommandSyntaxException ignored) { } - throw new ThrowStatement("Cannot parse block: "+str, Throwables.UNKNOWN_BLOCK); + throw new ThrowStatement(str, Throwables.UNKNOWN_BLOCK); } public static BlockPos locateBlockPos(CarpetContext c, int xpos, int ypos, int zpos) diff --git a/src/main/java/carpet/script/value/NBTSerializableValue.java b/src/main/java/carpet/script/value/NBTSerializableValue.java index 11595d06..9c26f252 100644 --- a/src/main/java/carpet/script/value/NBTSerializableValue.java +++ b/src/main/java/carpet/script/value/NBTSerializableValue.java @@ -299,7 +299,7 @@ public class NBTSerializableValue extends Value implements ContainerValueInterfa } catch (CommandSyntaxException e) { - throw new ThrowStatement("Incorrect item: "+itemString, Throwables.UNKNOWN_ITEM); + throw new ThrowStatement(itemString, Throwables.UNKNOWN_ITEM); } } diff --git a/src/main/java/carpet/script/value/ValueConversions.java b/src/main/java/carpet/script/value/ValueConversions.java index 320916ae..ba5bbf24 100644 --- a/src/main/java/carpet/script/value/ValueConversions.java +++ b/src/main/java/carpet/script/value/ValueConversions.java @@ -160,7 +160,7 @@ public class ValueConversions } } if (dim == null) - throw new ThrowStatement("Incorrect dimension string: "+dimString, Throwables.UNKNOWN_DIMENSION); + throw new ThrowStatement(dimString, Throwables.UNKNOWN_DIMENSION); return server.getWorld(dim); } }