From 7d6b850678d007059092049376d2cb2ec7de07d1 Mon Sep 17 00:00:00 2001 From: blackspherefollower Date: Wed, 23 Aug 2023 09:05:23 +0100 Subject: [PATCH] feat: Pass full exceptions in Error messages This change causes internal exceptions that were previously converted into Error messages with just the first message text to include the full exception object for better debugging. Fixes #35 --- .../common/ButtplugClientWSEndpoint.java | 11 +++++----- .../client/ButtplugClientWSClient.java | 11 +++++----- .../buttplug4j/client/ButtplugClient.java | 10 ++++++++-- .../buttplug4j/protocol/messages/Error.java | 20 +++++++++++++++++++ 4 files changed, 38 insertions(+), 14 deletions(-) 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,