Skip to content

Commit 2b1cd66

Browse files
committed
Net: unwrap the exception if it came from the user code
1 parent 0d12c46 commit 2b1cd66

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

java/libraries/net/src/processing/net/Client.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ public void stop() {
155155
try {
156156
disconnectEventMethod.invoke(parent, this);
157157
} catch (Exception e) {
158-
e.printStackTrace();
158+
Throwable cause = e;
159+
// unwrap the exception if it came from the user code
160+
if (e instanceof InvocationTargetException && e.getCause() != null) {
161+
cause = e.getCause();
162+
}
163+
cause.printStackTrace();
159164
disconnectEventMethod = null;
160165
}
161166
}
@@ -266,7 +271,12 @@ public void run() {
266271
clientEventMethod.invoke(parent, this);
267272
} catch (Exception e) {
268273
System.err.println("error, disabling clientEvent() for " + host);
269-
e.printStackTrace();
274+
Throwable cause = e;
275+
// unwrap the exception if it came from the user code
276+
if (e instanceof InvocationTargetException && e.getCause() != null) {
277+
cause = e.getCause();
278+
}
279+
cause.printStackTrace();
270280
clientEventMethod = null;
271281
}
272282
}

java/libraries/net/src/processing/net/Server.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,12 @@ public void run() {
308308
serverEventMethod.invoke(parent, this, client);
309309
} catch (Exception e) {
310310
System.err.println("Disabling serverEvent() for port " + port);
311-
e.printStackTrace();
311+
Throwable cause = e;
312+
// unwrap the exception if it came from the user code
313+
if (e instanceof InvocationTargetException && e.getCause() != null) {
314+
cause = e.getCause();
315+
}
316+
cause.printStackTrace();
312317
serverEventMethod = null;
313318
}
314319
}

0 commit comments

Comments
 (0)