Skip to content

Commit

Permalink
feat: Pass full exceptions in Error messages
Browse files Browse the repository at this point in the history
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
  • Loading branch information
blackspherefollower committed Aug 23, 2023
1 parent 4ea6d50 commit 7d6b850
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -98,8 +98,7 @@ protected final CompletableFuture<ButtplugMessage> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -123,8 +123,7 @@ protected CompletableFuture<ButtplugMessage> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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,
Expand Down

0 comments on commit 7d6b850

Please sign in to comment.