This is an automated email from the ASF dual-hosted git repository.

toulmean pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-tuweni.git


The following commit(s) were added to refs/heads/main by this push:
     new 193b037  respond with an error if the backend is unavailable
     new a55a18f  Merge pull request #327 from atoulme/respond_with_an_error
193b037 is described below

commit 193b037ce4921257a6359aeac15a943539d99fb0
Author: Antoine Toulme <anto...@lunar-ocean.com>
AuthorDate: Fri Jul 30 20:29:31 2021 -0700

    respond with an error if the backend is unavailable
---
 .../org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt    |  8 +++++-
 .../org/apache/tuweni/jsonrpc/JSONRPCClient.kt     | 29 +++++++---------------
 2 files changed, 16 insertions(+), 21 deletions(-)

diff --git 
a/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt 
b/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt
index fc4748c..c835a9e 100644
--- a/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt
+++ b/jsonrpc-app/src/main/kotlin/org/apache/tuweni/jsonrpc/app/JSONRPCApp.kt
@@ -21,6 +21,7 @@ import io.vertx.core.VertxOptions
 import io.vertx.tracing.opentelemetry.OpenTelemetryOptions
 import kotlinx.coroutines.asCoroutineDispatcher
 import kotlinx.coroutines.runBlocking
+import org.apache.tuweni.eth.internalError
 import org.apache.tuweni.jsonrpc.JSONRPCClient
 import org.apache.tuweni.jsonrpc.JSONRPCServer
 import org.apache.tuweni.jsonrpc.methods.MeteredHandler
@@ -79,7 +80,12 @@ class JSONRPCApplication(
 
     val allowListHandler = MethodAllowListHandler(config.allowedMethods()) { 
req ->
       runBlocking {
-        client.sendRequest(req).await()
+        try {
+          client.sendRequest(req).await()
+        } catch (e: Exception) {
+          logger.error("Error sending JSON-RPC request", e)
+          internalError.copy(id = req.id)
+        }
       }
     }
 
diff --git a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt 
b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt
index 25ece27..045f28d 100644
--- a/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt
+++ b/jsonrpc/src/main/kotlin/org/apache/tuweni/jsonrpc/JSONRPCClient.kt
@@ -91,26 +91,15 @@ class JSONRPCClient(
    * @throws ConnectException if it cannot dial the remote client
    */
   suspend fun getBalance_latest(address: Address): UInt256 {
-    val body = mapOf(
-      Pair("jsonrpc", "2.0"),
-      Pair("method", "eth_getBalance"),
-      Pair("id", 1),
-      Pair("params", listOf(address.toHexString(), "latest"))
-    )
-    val deferred = CompletableDeferred<UInt256>()
-
-    client.post(serverPort, serverHost, "/")
-      .putHeader("Content-Type", "application/json")
-      .sendBuffer(Buffer.buffer(mapper.writeValueAsBytes(body))) { response ->
-        if (response.failed()) {
-          deferred.completeExceptionally(response.cause())
-        } else {
-          val jsonResponse = response.result().bodyAsJsonObject()
-          
deferred.complete(UInt256.fromHexString(jsonResponse.getString("result")))
-        }
-      }
-
-    return deferred.await()
+    val body = JSONRPCRequest(nextId(), "eth_getBalance", 
arrayOf(address.toHexString(), "latest"))
+    val jsonResponse = sendRequest(body).await()
+    val err = jsonResponse.error
+    if (err != null) {
+      val errorMessage = "Code ${err.code}: ${err.message}"
+      throw ClientRequestException(errorMessage)
+    } else {
+      return UInt256.fromHexString(jsonResponse.result.toString())
+    }
   }
 
   /**

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@tuweni.apache.org
For additional commands, e-mail: commits-h...@tuweni.apache.org

Reply via email to