Skip to content

Commit

Permalink
Improved error reporting a bit, moved all error classes to .error pac…
Browse files Browse the repository at this point in the history
…kage
  • Loading branch information
LatvianModder committed Jul 6, 2024
1 parent 66d126f commit c77bccc
Show file tree
Hide file tree
Showing 35 changed files with 229 additions and 180 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.latvian.mods.kubejs.core.mixin;

import dev.latvian.mods.kubejs.recipe.RecipeExceptionJS;
import dev.latvian.mods.kubejs.error.EmptyTagException;
import dev.latvian.mods.kubejs.recipe.RecipesKubeEvent;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -29,7 +29,7 @@ public abstract class IngredientTagValueMixin {
var values = lookup.values(tag);

if (values.isEmpty()) {
throw new RecipeExceptionJS("Empty tag: " + tag.location());
throw new EmptyTagException(tag);
} else {
info.setReturnValue(values.stream().map(ItemStack::new).toList());
}
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/error/EmptyTagException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.latvian.mods.kubejs.error;

import net.minecraft.tags.TagKey;

public class EmptyTagException extends KubeRuntimeException {
public final TagKey<?> tagKey;

public EmptyTagException(TagKey<?> tagKey) {
super("Empty tag: " + tagKey.location());
this.tagKey = tagKey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.latvian.mods.kubejs.error;

public class EmptyTagTargetException extends KubeRuntimeException {
public EmptyTagTargetException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.latvian.mods.kubejs.error;

import dev.latvian.mods.kubejs.script.SourceLine;
import dev.latvian.mods.kubejs.util.MutedError;

public class KubeRuntimeException extends RuntimeException implements MutedError {
public SourceLine sourceLine;

public KubeRuntimeException(String m) {
super(m);
this.sourceLine = SourceLine.UNKNOWN;
}

public KubeRuntimeException(String m, Throwable cause) {
super(m, cause);
this.sourceLine = SourceLine.UNKNOWN;
}

@Override
public String toString() {
var sb = new StringBuilder();
sb.append(getMessage());

// append cause as well since RecipeExceptions can swallow underlying problems
if (getCause() != null) {
sb.append("\ncause: ");
sb.append(getCause());
}

return sb.toString();
}

public KubeRuntimeException source(SourceLine sourceLine) {
this.sourceLine = sourceLine;
return this;
}
}
12 changes: 12 additions & 0 deletions src/main/java/dev/latvian/mods/kubejs/error/LegacyError.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.latvian.mods.kubejs.error;

public class LegacyError extends KubeRuntimeException {
public LegacyError(String message) {
super(message);
}

@Override
public String toString() {
return getLocalizedMessage();
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package dev.latvian.mods.kubejs.recipe.component;
package dev.latvian.mods.kubejs.error;

import dev.latvian.mods.kubejs.recipe.RecipeExceptionJS;
import dev.latvian.mods.kubejs.recipe.RecipeKey;

import java.util.Collection;

public class MissingComponentException extends RecipeExceptionJS {
public class MissingComponentException extends KubeRuntimeException {
public final RecipeKey<?> key;
public final Collection<RecipeKey<?>> valid;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package dev.latvian.mods.kubejs.error;

public class UnknownRecipeTypeException extends KubeRuntimeException {
public final String recipeType;

public UnknownRecipeTypeException(String recipeType) {
super("Unknown recipe type: " + recipeType);
this.recipeType = recipeType;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ public static <T> EventTargetType<T> create(Class<T> type) {
return new EventTargetType<>(type);
}

public static final EventTargetType<String> STRING = create(String.class).transformer(EventTargetType::toString);
public static final EventTargetType<ResourceLocation> ID = create(ResourceLocation.class).transformer(EventTargetType::toResourceLocation);
public static final EventTargetType<ResourceKey<Registry<?>>> REGISTRY = Cast.to(create(ResourceKey.class).transformer(EventTargetType::toRegistryKey).identity());
public static final EventTargetType<String> STRING = create(String.class).transformer(EventTargetType::toString).describeType(TypeInfo.STRING);
public static final EventTargetType<ResourceLocation> ID = create(ResourceLocation.class).transformer(EventTargetType::toResourceLocation).describeType(TypeInfo.of(ResourceLocation.class));
public static final EventTargetType<ResourceKey<Registry<?>>> REGISTRY = Cast.to(create(ResourceKey.class).transformer(EventTargetType::toRegistryKey).identity().describeType(TypeInfo.of(ResourceKey.class).withParams(TypeInfo.of(Registry.class))));

public static <T> EventTargetType<ResourceKey<T>> registryKey(ResourceKey<Registry<T>> registry, Class<?> type) {
return Cast.to(create(ResourceKey.class).identity().transformer(o -> toKey(registry, o)).describeType(TypeInfo.of(ResourceKey.class).withParams(TypeInfo.of(type))));
Expand Down Expand Up @@ -58,7 +58,7 @@ public static <T extends Enum<T>> EventTargetType<T> fromEnum(Class<T> type) {
} else {
return null;
}
});
}).describeType(typeInfo);
}

private static String toString(Object object) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import dev.latvian.mods.kubejs.error.EmptyTagException;
import dev.latvian.mods.kubejs.recipe.CachedTagLookup;
import dev.latvian.mods.kubejs.recipe.RecipeExceptionJS;
import dev.latvian.mods.kubejs.recipe.RecipesKubeEvent;
import io.netty.buffer.ByteBuf;
import net.minecraft.core.component.DataComponents;
Expand Down Expand Up @@ -80,7 +80,7 @@ public Stream<ItemStack> getItems() {

if (set.isEmpty()) {
if (RecipesKubeEvent.TEMP_ITEM_TAG_LOOKUP.getValue() != null) {
throw new RecipeExceptionJS("Empty tag: " + tagKey.location());
throw new EmptyTagException(tagKey);
} else {
var error = new ItemStack(Items.BARRIER);
error.set(DataComponents.CUSTOM_NAME, Component.literal("Empty Tag: " + tagKey.location()));
Expand Down
10 changes: 0 additions & 10 deletions src/main/java/dev/latvian/mods/kubejs/item/EmptyItemError.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.latvian.mods.kubejs.recipe;

import dev.latvian.mods.kubejs.error.KubeRuntimeException;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponentValue;
import dev.latvian.mods.kubejs.recipe.match.ReplacementMatchInfo;
import dev.latvian.mods.kubejs.script.ConsoleJS;
Expand All @@ -22,11 +23,11 @@ public Object call(Context cx, Scriptable scope, Scriptable thisObj, Object[] ar
}
};

public ErroredKubeRecipe(RecipesKubeEvent event, RecipeExceptionJS rex) {
public ErroredKubeRecipe(RecipesKubeEvent event, KubeRuntimeException rex) {
this(event, rex.getMessage(), rex, null);
}

public ErroredKubeRecipe(RecipesKubeEvent event, String errorMessage, RecipeExceptionJS rex, @Nullable Pattern skipError) {
public ErroredKubeRecipe(RecipesKubeEvent event, String errorMessage, KubeRuntimeException rex, @Nullable Pattern skipError) {
this.context = errorMessage;
ConsoleJS.SERVER.error(errorMessage, rex, skipError);
event.failedCount.incrementAndGet();
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/dev/latvian/mods/kubejs/recipe/KubeRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import dev.latvian.mods.kubejs.CommonProperties;
import dev.latvian.mods.kubejs.DevProperties;
import dev.latvian.mods.kubejs.core.RecipeLikeKJS;
import dev.latvian.mods.kubejs.recipe.component.MissingComponentException;
import dev.latvian.mods.kubejs.error.KubeRuntimeException;
import dev.latvian.mods.kubejs.error.MissingComponentException;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponentBuilderMap;
import dev.latvian.mods.kubejs.recipe.component.RecipeComponentValue;
import dev.latvian.mods.kubejs.recipe.ingredientaction.ConsumeAction;
Expand All @@ -19,6 +20,7 @@
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchema;
import dev.latvian.mods.kubejs.recipe.special.KubeJSCraftingRecipe;
import dev.latvian.mods.kubejs.script.ConsoleJS;
import dev.latvian.mods.kubejs.script.SourceLine;
import dev.latvian.mods.kubejs.util.Cast;
import dev.latvian.mods.kubejs.util.SlotFilter;
import dev.latvian.mods.kubejs.util.UtilsJS;
Expand Down Expand Up @@ -46,6 +48,7 @@ public class KubeRecipe implements RecipeLikeKJS, CustomJavaToJsWrapper {
public RecipeTypeFunction type;
public boolean newRecipe;
public boolean removed;
public SourceLine sourceLine = SourceLine.UNKNOWN;
public String modifyResult = "";

private RecipeComponentBuilderMap valueMap = RecipeComponentBuilderMap.EMPTY;
Expand All @@ -69,7 +72,7 @@ public void deserialize(boolean merge) {
try {
v.key.component.readFromJson(this, Cast.to(v), json);
} catch (Exception ex) {
ConsoleJS.SERVER.error("Failed to read " + v.key + " from recipe " + this, ex, RecipesKubeEvent.POST_SKIP_ERROR);
ConsoleJS.SERVER.error("", new KubeRuntimeException("Failed to read " + v.key + " from recipe " + this, ex).source(sourceLine), RecipesKubeEvent.POST_SKIP_ERROR);
}

if (v.value != null) {
Expand All @@ -86,7 +89,7 @@ public void serialize() {
for (var v : valueMap.holders) {
if (v.shouldWrite()) {
if (v.value == null) {
throw new RecipeExceptionJS("Value not set for " + v.key + " in recipe " + this);
throw new KubeRuntimeException("Value not set for " + v.key + " in recipe " + this);
}

v.key.component.writeToJson(this, Cast.to(v), json);
Expand Down Expand Up @@ -186,7 +189,7 @@ public void afterLoaded() {
var e = v.checkEmpty();

if (!e.isEmpty()) {
throw new RecipeExceptionJS(e);
throw new KubeRuntimeException(e);
}
}
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package dev.latvian.mods.kubejs.recipe;

import com.google.gson.JsonObject;
import dev.latvian.mods.kubejs.error.KubeRuntimeException;
import dev.latvian.mods.kubejs.recipe.component.ComponentValueMap;
import dev.latvian.mods.kubejs.recipe.schema.RecipeSchemaType;
import dev.latvian.mods.kubejs.script.SourceLine;
import dev.latvian.mods.kubejs.util.MapJS;
import dev.latvian.mods.kubejs.util.WrappedJS;
import dev.latvian.mods.rhino.BaseFunction;
Expand Down Expand Up @@ -35,12 +37,10 @@ public RecipeTypeFunction(RecipesKubeEvent event, RecipeSchemaType schemaType) {
public KubeRecipe call(Context cx, Scriptable scope, Scriptable thisObj, Object[] args0) {
try {
return createRecipe(cx, args0);
} catch (RecipeExceptionJS rex) {
if (rex.error) {
throw rex;
} else {
return new ErroredKubeRecipe(event, "Failed to create recipe for type '%s'".formatted(idString), rex, SKIP_ERROR);
}
} catch (KubeRuntimeException rex) {
var recipe = new ErroredKubeRecipe(event, "Failed to create recipe for type '%s'".formatted(idString), rex, SKIP_ERROR);
recipe.sourceLine = rex.sourceLine;
return recipe;
}
}

Expand All @@ -56,13 +56,13 @@ public KubeRecipe createRecipe(Context cx, Object[] args) {

if (constructor == null) {
if (args.length == 1 && (args[0] instanceof Map<?, ?> || args[0] instanceof JsonObject)) {
var recipe = schemaType.schema.deserialize(this, null, MapJS.json(cx, args[0]));
var recipe = schemaType.schema.deserialize(SourceLine.of(cx), this, null, MapJS.json(cx, args[0]));
recipe.afterLoaded();
return event.addRecipe(recipe, true);
// throw new RecipeExceptionJS("Use event.custom(json) for json recipes!");
}

throw new RecipeExceptionJS("Constructor for " + id + " with " + args.length + " arguments not found!");
throw new KubeRuntimeException("Constructor for " + id + " with " + args.length + " arguments not found!");
}

/*
Expand All @@ -83,10 +83,10 @@ public KubeRecipe createRecipe(Context cx, Object[] args) {
var recipe = constructor.create(cx, this, schemaType, argMap);
recipe.afterLoaded();
return event.addRecipe(recipe, false);
} catch (RecipeExceptionJS rex) {
} catch (KubeRuntimeException rex) {
throw rex;
} catch (Throwable ex) {
throw new RecipeExceptionJS("Failed to create recipe for type '" + id + "' with args " + Arrays.stream(args).map(o -> o == null ? "null" : (o + ": " + o.getClass().getSimpleName())).collect(Collectors.joining(", ", "[", "]")), ex);
throw new KubeRuntimeException("Failed to create recipe for type '" + id + "' with args " + Arrays.stream(args).map(o -> o == null ? "null" : (o + ": " + o.getClass().getSimpleName())).collect(Collectors.joining(", ", "[", "]")), ex);
}
}

Expand Down
Loading

0 comments on commit c77bccc

Please sign in to comment.