Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch listener exceptions #92

Merged
merged 2 commits into from
Nov 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import groovy.util.GroovyScriptEngine;
import groovy.util.ResourceException;
import groovy.util.ScriptException;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenHashSet;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.common.MinecraftForge;
Expand All @@ -25,13 +26,13 @@
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.Collection;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class GroovyScriptSandbox extends GroovySandbox {

private final ImportCustomizer importCustomizer = new ImportCustomizer();
private final Map<List<StackTraceElement>, AtomicInteger> storedExceptions;

private LoadStage currentLoadStage;

Expand Down Expand Up @@ -69,6 +70,7 @@ public GroovyScriptSandbox(URL... scriptEnvironment) {
"net.minecraft.util.ResourceLocation",
"net.minecraftforge.fml.common.eventhandler.EventPriority",
"com.cleanroommc.groovyscript.event.EventBusType");
this.storedExceptions = new Object2ObjectOpenHashMap<>();
}

public void checkSyntax() {
Expand Down Expand Up @@ -109,9 +111,12 @@ public <T> T runClosure(Closure<T> closure, Object... args) {
T result = null;
try {
result = closure.call(args);
} catch (Exception e) {
GroovyLog.get().error("An exception occurred while running a closure!");
GroovyLog.get().exception(e);
} catch (Throwable t) {
this.storedExceptions.computeIfAbsent(Arrays.asList(t.getStackTrace()), k -> {
GroovyLog.get().error("An exception occurred while running a closure!");
GroovyLog.get().exception(t);
return new AtomicInteger();
}).addAndGet(1);
} finally {
stopRunning();
}
Expand Down
Loading