Skip to content

Commit bb54335

Browse files
committed
log failing scripts and classes
1 parent 24102bc commit bb54335

4 files changed

Lines changed: 54 additions & 5 deletions

File tree

src/main/java/com/cleanroommc/groovyscript/core/mixin/groovy/MetaClassImplMixin.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package com.cleanroommc.groovyscript.core.mixin.groovy;
22

33
import com.cleanroommc.groovyscript.GroovyScript;
4+
import com.cleanroommc.groovyscript.sandbox.JavaBeanException;
45
import com.cleanroommc.groovyscript.sandbox.meta.ClassMetaClass;
56
import com.cleanroommc.groovyscript.sandbox.security.GroovySecurityManager;
7+
import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod;
8+
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
69
import groovy.lang.*;
710
import org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl;
811
import org.spongepowered.asm.mixin.*;
@@ -140,4 +143,13 @@ private Object invokePropertyOrMissing(Object object,
140143

141144
return invokeMissingMethod(object, methodName, originalArguments, null, isCallToSuper);
142145
}
146+
147+
@WrapMethod(method = "addProperties")
148+
public void addProperties(Operation<Void> original) {
149+
try {
150+
original.call();
151+
} catch (Throwable t) {
152+
throw new JavaBeanException(t, (MetaClassImpl) (Object) this);
153+
}
154+
}
143155
}

src/main/java/com/cleanroommc/groovyscript/sandbox/CustomGroovyScriptEngine.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,8 @@ protected Class<?> loadScriptClassInternal(File file, boolean isFileRelative) {
260260
Class<?> scriptClass = null;
261261
try {
262262
scriptClass = parseDynamicScript(file, isFileRelative);
263-
} catch (Exception e) {
264-
GroovyLog.get().exception("An error occurred while trying to load script class " + file.toString(), e);
263+
} catch (Throwable e) {
264+
GroovyLog.get().exception("An error occurred while trying to compile script class " + file.toString(), e);
265265
}
266266
return scriptClass;
267267
}

src/main/java/com/cleanroommc/groovyscript/sandbox/GroovyScriptSandbox.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ public void run(LoadStage currentLoadStage) {
128128
this.currentLoadStage = Objects.requireNonNull(currentLoadStage);
129129
try {
130130
load();
131+
} catch (ScriptRunException e) {
132+
GroovyLog.get().exception("Script '" + e.script.name + "' ran into an issue while executing.", e.parent);
131133
} catch (IOException | ScriptException | ResourceException e) {
132134
GroovyLog.get().exception("An exception occurred while trying to run groovy code! This is might be a internal groovy issue.", e);
133135
} catch (Throwable t) {
134-
GroovyLog.get().exception(t);
136+
GroovyLog.get().exception("An unknown error occurred while running scripts", t);
135137
} finally {
136138
GroovyLog.get().infoMC("Groovy scripts took {}ms to compile and {}ms to run in {}.", this.compileTime, this.runTime, currentLoadStage.getName());
137139
this.currentLoadStage = null;
@@ -257,7 +259,11 @@ protected void loadScripts(Binding binding, Set<String> executedClasses, boolean
257259
// script is a class
258260
if (run && shouldRunFile(compiledScript.path)) {
259261
t = System.currentTimeMillis();
260-
runClass(compiledScript.clazz);
262+
try {
263+
runClass(compiledScript.clazz);
264+
} catch (Throwable e) {
265+
throw new ScriptRunException(e, compiledScript);
266+
}
261267
this.runTime += System.currentTimeMillis() - t;
262268
}
263269
executedClasses.add(compiledScript.path);
@@ -266,7 +272,11 @@ protected void loadScripts(Binding binding, Set<String> executedClasses, boolean
266272
if (run && shouldRunFile(compiledScript.path)) {
267273
Script script = InvokerHelper.createScript(compiledScript.clazz, binding);
268274
t = System.currentTimeMillis();
269-
runScript(script);
275+
try {
276+
runScript(script);
277+
} catch (Throwable e) {
278+
throw new ScriptRunException(e, compiledScript);
279+
}
270280
this.runTime += System.currentTimeMillis() - t;
271281
}
272282
}
@@ -370,4 +380,16 @@ public long getLastCompileTime() {
370380
public long getLastRunTime() {
371381
return runTime;
372382
}
383+
384+
static class ScriptRunException extends RuntimeException {
385+
386+
Throwable parent;
387+
CompiledScript script;
388+
389+
ScriptRunException(Throwable parent, CompiledScript script) {
390+
super();
391+
this.parent = parent;
392+
this.script = script;
393+
}
394+
}
373395
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.cleanroommc.groovyscript.sandbox;
2+
3+
import groovy.lang.MetaClassImpl;
4+
5+
public class JavaBeanException extends RuntimeException {
6+
7+
public final Throwable parent;
8+
public final MetaClassImpl metaClass;
9+
10+
public JavaBeanException(Throwable parent, MetaClassImpl metaClass) {
11+
super("An error occurred while trying to gather java properties for class " + metaClass.getTheClass().getName());
12+
this.parent = parent;
13+
this.metaClass = metaClass;
14+
}
15+
}

0 commit comments

Comments
 (0)