Use JsonRpcError instead of ValueError
Allows implementors of this library to distinguish between errors originating from this library.
This commit is contained in:
parent
184984a4fd
commit
7862ac4a5e
|
@ -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
|
||||
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue