From 7862ac4a5ec8a7bc7d77036b28647c71d339df26 Mon Sep 17 00:00:00 2001 From: Eric Mastro Date: Thu, 15 Sep 2022 20:31:03 +1000 Subject: [PATCH] Use JsonRpcError instead of ValueError Allows implementors of this library to distinguish between errors originating from this library. --- json_rpc/client.nim | 11 ++++++----- json_rpc/clients/socketclient.nim | 5 +++-- json_rpc/clients/websocketclient.nim | 4 ++-- tests/testhttp.nim | 4 ++-- tests/testhttps.nim | 4 ++-- 5 files changed, 15 insertions(+), 13 deletions(-) diff --git a/json_rpc/client.nim b/json_rpc/client.nim index 54246ff..b2233fe 100644 --- a/json_rpc/client.nim +++ b/json_rpc/client.nim @@ -1,6 +1,7 @@ import std/[tables, macros], chronos, + ./errors, ./jsonmarshal from strutils import toLowerAscii, replace @@ -46,24 +47,24 @@ proc processMessage*(self: RpcClient, line: string) = let node = try: parseJson(line) except CatchableError as exc: raise exc # TODO https://github.com/status-im/nimbus-eth2/issues/2430 - except Exception as exc: raise (ref ValueError)(msg: exc.msg, parent: exc) + except Exception as exc: raise (ref JsonRpcError)(msg: exc.msg, parent: exc) if "id" in node: let id = node{"id"} or newJNull() var requestFut: Future[Response] if not self.awaiting.pop(id.getInt(-1), requestFut): - raise newException(ValueError, "Cannot find message id \"" & $id & "\"") + raise newException(JsonRpcError, "Cannot find message id \"" & $id & "\"") let version = node{"jsonrpc"}.getStr() if version != "2.0": - requestFut.fail(newException(ValueError, + requestFut.fail(newException(JsonRpcError, "Unsupported version of JSON, expected 2.0, received \"" & version & "\"")) else: let result = node{"result"} if result.isNil: let error = node{"error"} or newJNull() - requestFut.fail(newException(ValueError, $error)) + requestFut.fail(newException(JsonRpcError, $error)) else: requestFut.complete(result) elif "method" in node: @@ -73,7 +74,7 @@ proc processMessage*(self: RpcClient, line: string) = if not handler.isNil: handler(node{"params"} or newJArray()) else: - raise newException(ValueError, "Invalid jsonrpc message: " & $node) + raise newException(JsonRpcError, "Invalid jsonrpc message: " & $node) # Signature processing diff --git a/json_rpc/clients/socketclient.nim b/json_rpc/clients/socketclient.nim index 77dc643..de62bbf 100644 --- a/json_rpc/clients/socketclient.nim +++ b/json_rpc/clients/socketclient.nim @@ -1,7 +1,8 @@ import std/tables, chronos, - ../client + ../client, + ../errors {.push raises: [Defect].} @@ -28,7 +29,7 @@ method call*(self: RpcSocketClient, name: string, let id = self.getNextId() var value = $rpcCallNode(name, params, id) & "\r\n" if self.transport.isNil: - raise newException(ValueError, + raise newException(JsonRpcError, "Transport is not initialised (missing a call to connect?)") # completed by processMessage. diff --git a/json_rpc/clients/websocketclient.nim b/json_rpc/clients/websocketclient.nim index 1921613..89418ae 100644 --- a/json_rpc/clients/websocketclient.nim +++ b/json_rpc/clients/websocketclient.nim @@ -1,7 +1,7 @@ import pkg/[chronos, chronos/apps/http/httptable, chronicles], stew/byteutils, - ../client, ./config + ../client, ../errors, ./config export client @@ -48,7 +48,7 @@ method call*(self: RpcWebSocketClient, name: string, let id = self.getNextId() var value = $rpcCallNode(name, params, id) & "\r\n" if self.transport.isNil: - raise newException(ValueError, + raise newException(JsonRpcError, "Transport is not initialised (missing a call to connect?)") # completed by processMessage. diff --git a/tests/testhttp.nim b/tests/testhttp.nim index 4aabe8d..07c4983 100644 --- a/tests/testhttp.nim +++ b/tests/testhttp.nim @@ -28,12 +28,12 @@ proc invalidTest(address: string, port: Port): Future[bool] {.async.} = try: var r = await client.call("invalidProcA", %[]) discard r - except ValueError: + except JsonRpcError: invalidA = true try: var r = await client.call("invalidProcB", %[1, 2, 3]) discard r - except ValueError: + except JsonRpcError: invalidB = true if invalidA and invalidB: result = true diff --git a/tests/testhttps.nim b/tests/testhttps.nim index 0a7202e..39fa001 100644 --- a/tests/testhttps.nim +++ b/tests/testhttps.nim @@ -89,12 +89,12 @@ proc invalidTest(address: string, port: Port): Future[bool] {.async.} = try: var r = await client.call("invalidProcA", %[]) discard r - except ValueError: + except JsonRpcError: invalidA = true try: var r = await client.call("invalidProcB", %[1, 2, 3]) discard r - except ValueError: + except JsonRpcError: invalidB = true if invalidA and invalidB: result = true