From 6871810c7c82ebd7ae2bae2a6a98c7912f0dff14 Mon Sep 17 00:00:00 2001 From: Craig Raw Date: Thu, 1 Dec 2022 09:39:47 +0200 Subject: [PATCH] improve display of json rpc error exceptions --- .../sparrow/event/ConnectionFailedEvent.java | 7 +++++++ .../preferences/ServerPreferencesController.java | 13 +++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/sparrowwallet/sparrow/event/ConnectionFailedEvent.java b/src/main/java/com/sparrowwallet/sparrow/event/ConnectionFailedEvent.java index 244765b2..aee2f7fa 100644 --- a/src/main/java/com/sparrowwallet/sparrow/event/ConnectionFailedEvent.java +++ b/src/main/java/com/sparrowwallet/sparrow/event/ConnectionFailedEvent.java @@ -1,5 +1,6 @@ package com.sparrowwallet.sparrow.event; +import com.github.arteam.simplejsonrpc.client.exception.JsonRpcException; import com.sparrowwallet.sparrow.net.TlsServerException; public class ConnectionFailedEvent { @@ -20,6 +21,12 @@ public class ConnectionFailedEvent { Throwable cause = (exception.getCause() != null ? exception.getCause() : exception); cause = (cause.getCause() != null ? cause.getCause() : cause); + + if(cause instanceof JsonRpcException jsonRpcException && jsonRpcException.getErrorMessage() != null) { + return jsonRpcException.getErrorMessage().getMessage() + + (jsonRpcException.getErrorMessage().getData() != null ? " (" + jsonRpcException.getErrorMessage().getData().asText() + ")" : ""); + } + String message = splitCamelCase(cause.getClass().getSimpleName().replace("Exception", "Error")); return message + " (" + cause.getMessage() + ")"; } diff --git a/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java b/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java index cb7ff26f..4365925f 100644 --- a/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java +++ b/src/main/java/com/sparrowwallet/sparrow/preferences/ServerPreferencesController.java @@ -1,5 +1,7 @@ package com.sparrowwallet.sparrow.preferences; +import com.github.arteam.simplejsonrpc.client.exception.JsonRpcException; +import com.google.common.base.Throwables; import com.google.common.eventbus.Subscribe; import com.google.common.net.HostAndPort; import com.sparrowwallet.drongo.Network; @@ -623,7 +625,14 @@ public class ServerPreferencesController extends PreferencesDetailController { private void showConnectionFailure(Throwable exception) { log.error("Connection error", exception); - String reason = exception.getCause() != null ? exception.getCause().getMessage() : exception.getMessage(); + + String reason; + if(Throwables.getRootCause(exception) instanceof JsonRpcException jsonRpcException && jsonRpcException.getErrorMessage() != null) { + reason = jsonRpcException.getErrorMessage().getMessage() + (jsonRpcException.getErrorMessage().getData() != null ? " (" + jsonRpcException.getErrorMessage().getData().asText() + ")" : ""); + } else { + reason = exception.getCause() != null ? exception.getCause().getMessage() : exception.getMessage(); + } + if(exception instanceof TlsServerException && exception.getCause() != null) { TlsServerException tlsServerException = (TlsServerException)exception; if(exception.getCause().getMessage().contains("PKIX path building failed")) { @@ -649,7 +658,7 @@ public class ServerPreferencesController extends PreferencesDetailController { reason += ". Check if the proxy server is running."; } else if(exception instanceof TorServerAlreadyBoundException) { reason += "\nIs a Tor proxy already running on port " + TorService.PROXY_PORT + "?"; - } else if(reason != null && reason.contains("Check if Bitcoin Core is running")) { + } else if(reason != null && (reason.contains("Check if Bitcoin Core is running") || reason.contains("Could not connect to Bitcoin Core RPC"))) { reason += "\n\nSee https://sparrowwallet.com/docs/connect-node.html"; }