From 2b1cd66f834c97f7e1f30aa7cfc646e51bc791a5 Mon Sep 17 00:00:00 2001 From: Jakub Valtar Date: Mon, 22 Jan 2018 13:44:43 +0100 Subject: [PATCH] Net: unwrap the exception if it came from the user code --- java/libraries/net/src/processing/net/Client.java | 14 ++++++++++++-- java/libraries/net/src/processing/net/Server.java | 7 ++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/java/libraries/net/src/processing/net/Client.java b/java/libraries/net/src/processing/net/Client.java index f179b6d2e..2a779f96c 100644 --- a/java/libraries/net/src/processing/net/Client.java +++ b/java/libraries/net/src/processing/net/Client.java @@ -155,7 +155,12 @@ public class Client implements Runnable { try { disconnectEventMethod.invoke(parent, this); } catch (Exception e) { - e.printStackTrace(); + Throwable cause = e; + // unwrap the exception if it came from the user code + if (e instanceof InvocationTargetException && e.getCause() != null) { + cause = e.getCause(); + } + cause.printStackTrace(); disconnectEventMethod = null; } } @@ -266,7 +271,12 @@ public class Client implements Runnable { clientEventMethod.invoke(parent, this); } catch (Exception e) { System.err.println("error, disabling clientEvent() for " + host); - e.printStackTrace(); + Throwable cause = e; + // unwrap the exception if it came from the user code + if (e instanceof InvocationTargetException && e.getCause() != null) { + cause = e.getCause(); + } + cause.printStackTrace(); clientEventMethod = null; } } diff --git a/java/libraries/net/src/processing/net/Server.java b/java/libraries/net/src/processing/net/Server.java index 74345800c..f77b2e0c8 100644 --- a/java/libraries/net/src/processing/net/Server.java +++ b/java/libraries/net/src/processing/net/Server.java @@ -308,7 +308,12 @@ public class Server implements Runnable { serverEventMethod.invoke(parent, this, client); } catch (Exception e) { System.err.println("Disabling serverEvent() for port " + port); - e.printStackTrace(); + Throwable cause = e; + // unwrap the exception if it came from the user code + if (e instanceof InvocationTargetException && e.getCause() != null) { + cause = e.getCause(); + } + cause.printStackTrace(); serverEventMethod = null; } }