diff --git a/buttplug4j.connectors.javax.websocket.common/src/main/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/common/ButtplugClientWSEndpoint.java b/buttplug4j.connectors.javax.websocket.common/src/main/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/common/ButtplugClientWSEndpoint.java index 3c3b63e..cabe27e 100644 --- a/buttplug4j.connectors.javax.websocket.common/src/main/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/common/ButtplugClientWSEndpoint.java +++ b/buttplug4j.connectors.javax.websocket.common/src/main/java/io/github/blackspherefollower/buttplug4j/connectors/javax/websocket/common/ButtplugClientWSEndpoint.java @@ -37,8 +37,7 @@ public final void onMessage(final Session sess, final String message) { onMessage(msgs); } catch (ButtplugProtocolException e) { if (getErrorReceived() != null) { - getErrorReceived().errorReceived(new Error(e.getMessage(), - Error.ErrorClass.ERROR_UNKNOWN, ButtplugConsts.SYSTEM_MSG_ID)); + getErrorReceived().errorReceived(new Error(e)); } else { e.printStackTrace(); } @@ -81,8 +80,9 @@ public void run() { @OnError public final void onWebSocketError(final Throwable cause) { if (getErrorReceived() != null) { - getErrorReceived().errorReceived(new Error(cause.getMessage(), Error.ErrorClass.ERROR_UNKNOWN, - ButtplugConsts.SYSTEM_MSG_ID)); + getErrorReceived().errorReceived(new Error(cause)); + } else { + cause.printStackTrace(); } disconnect(); } @@ -98,8 +98,7 @@ protected final CompletableFuture sendMessage(final ButtplugMes try { session.getAsyncRemote().sendText(getParser().formatJson(msg)).get(); } catch (Exception e) { - return CompletableFuture.completedFuture(new Error(e.getMessage(), - Error.ErrorClass.ERROR_UNKNOWN, msg.getId())); + return CompletableFuture.completedFuture(new Error(e, msg.getId())); } return promise; } diff --git a/buttplug4j.connectors.jetty.websocket.client/src/main/java/io/github/blackspherefollower/buttplug4j/connectors/jetty/websocket/client/ButtplugClientWSClient.java b/buttplug4j.connectors.jetty.websocket.client/src/main/java/io/github/blackspherefollower/buttplug4j/connectors/jetty/websocket/client/ButtplugClientWSClient.java index cdffd29..31be5e0 100644 --- a/buttplug4j.connectors.jetty.websocket.client/src/main/java/io/github/blackspherefollower/buttplug4j/connectors/jetty/websocket/client/ButtplugClientWSClient.java +++ b/buttplug4j.connectors.jetty.websocket.client/src/main/java/io/github/blackspherefollower/buttplug4j/connectors/jetty/websocket/client/ButtplugClientWSClient.java @@ -95,8 +95,7 @@ public void onMessage(final Session sess, final String message) { onMessage(msgs); } catch (ButtplugProtocolException e) { if (getErrorReceived() != null) { - getErrorReceived().errorReceived(new Error(e.getMessage(), - Error.ErrorClass.ERROR_UNKNOWN, ButtplugConsts.SYSTEM_MSG_ID)); + getErrorReceived().errorReceived(new Error(e)); } else { e.printStackTrace(); } @@ -106,8 +105,9 @@ public void onMessage(final Session sess, final String message) { @OnWebSocketError public void onWebSocketError(final Throwable cause) { if (getErrorReceived() != null) { - getErrorReceived().errorReceived(new Error(cause.getMessage(), Error.ErrorClass.ERROR_UNKNOWN, - ButtplugConsts.SYSTEM_MSG_ID)); + getErrorReceived().errorReceived(new Error(cause)); + } else { + cause.printStackTrace(); } disconnect(); } @@ -123,8 +123,7 @@ protected CompletableFuture sendMessage(final ButtplugMessage m try { session.getRemote().sendStringByFuture(getParser().formatJson(msg)).get(); } catch (Exception e) { - return CompletableFuture.completedFuture(new Error(e.getMessage(), - Error.ErrorClass.ERROR_UNKNOWN, msg.getId())); + return CompletableFuture.completedFuture(new Error(e, msg.getId())); } return promise; } diff --git a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClient.java b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClient.java index 60087df..138485a 100644 --- a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClient.java +++ b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/client/ButtplugClient.java @@ -115,7 +115,11 @@ public void run() { try { onPingTimer(); } catch (Exception e) { - e.printStackTrace(); + if (errorReceived != null) { + errorReceived.errorReceived(new Error(e)); + } else { + e.printStackTrace(); + } } } }, 0, Math.round(((double) ((ServerInfo) res).getMaxPingTime()) / 2)); @@ -131,7 +135,9 @@ public void run() { } } catch (ButtplugClientException | InterruptedException | ExecutionException e) { if (getErrorReceived() != null) { - getErrorReceived().errorReceived(new Error(e.getMessage(), Error.ErrorClass.ERROR_UNKNOWN, -1)); + getErrorReceived().errorReceived(new Error(e)); + } else { + e.printStackTrace(); } } diff --git a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/Error.java b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/Error.java index 5b131b9..081979b 100644 --- a/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/Error.java +++ b/buttplug4j/src/main/java/io/github/blackspherefollower/buttplug4j/protocol/messages/Error.java @@ -1,6 +1,7 @@ package io.github.blackspherefollower.buttplug4j.protocol.messages; import com.fasterxml.jackson.annotation.JsonFormat; +import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; import io.github.blackspherefollower.buttplug4j.protocol.ButtplugConsts; import io.github.blackspherefollower.buttplug4j.protocol.ButtplugMessage; @@ -12,6 +13,8 @@ public final class Error extends ButtplugMessage { private ErrorClass errorCode; @JsonProperty(value = "ErrorMessage", required = true) private String errorMessage; + @JsonIgnore + private Throwable exception = null; public Error(final String errorMessage, final ErrorClass errorCode, final long id) { super(id); @@ -26,6 +29,19 @@ private Error() { this.setErrorCode(ErrorClass.ERROR_UNKNOWN); } + public Error(Throwable e) { + super(ButtplugConsts.SYSTEM_MSG_ID); + this.setErrorMessage(e.getMessage()); + this.setErrorCode(ErrorClass.ERROR_UNKNOWN); + this.exception = e; + } + public Error(Throwable e, final long id) { + super(id); + this.setErrorMessage(e.getMessage()); + this.setErrorCode(ErrorClass.ERROR_UNKNOWN); + this.exception = e; + } + public ErrorClass getErrorCode() { return errorCode; } @@ -42,6 +58,10 @@ public void setErrorMessage(final String errorMessage) { this.errorMessage = errorMessage; } + public Throwable getException() { + return exception; + } + public enum ErrorClass { ERROR_UNKNOWN, ERROR_INIT,