Skip to content

Commit 9043d45

Browse files
committed
Basic error catcher.
1 parent a3d1ed7 commit 9043d45

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package mx.kenzie.skript.runtime.threading;
2+
3+
public class ScriptExceptionHandler implements Thread.UncaughtExceptionHandler {
4+
5+
@Override
6+
public void uncaughtException(Thread source, Throwable throwable) {
7+
if (throwable instanceof ThreadDeath) return;
8+
if (source instanceof ScriptThread thread) {
9+
final Class<?> start = thread.initiator;
10+
11+
System.err.println("An error has occurred.");
12+
if (start != null)
13+
System.err.println("Source: " + start.getName());
14+
throwable.printStackTrace(System.err);
15+
16+
} else {
17+
System.err.print("Exception in thread \""
18+
+ source.getName() + "\" ");
19+
throwable.printStackTrace(System.err);
20+
}
21+
}
22+
}

src/main/java/mx/kenzie/skript/runtime/threading/SkriptThreadProvider.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
public class SkriptThreadProvider implements ThreadFactory {
88

99
int counter;
10+
private final ScriptExceptionHandler handler = new ScriptExceptionHandler();
1011

1112
public Thread newThread(final OperationController controller, Runnable runnable, boolean inheritLocals) {
1213
final Thread thread = new ScriptThread(controller, Skript.THREAD_GROUP, runnable, Skript.THREAD_GROUP.getName() + "-" + counter++, 0, inheritLocals);
@@ -20,7 +21,7 @@ public Thread newThread(Runnable r) {
2021
}
2122

2223
public Thread.UncaughtExceptionHandler getHandler() {
23-
return Thread.getDefaultUncaughtExceptionHandler();
24+
return handler;
2425
}
2526

2627
}

0 commit comments

Comments
 (0)