mirror of
https://github.com/logos-storage/nim-json-rpc.git
synced 2026-02-12 01:33:07 +00:00
clientdispatch -> client, serverdispatch -> server, more error handling
This commit is contained in:
parent
179a6f6024
commit
53baf0bffc
@ -149,14 +149,15 @@ proc createRpcFromSig*(rpcDecl: NimNode): NimNode =
|
||||
|
||||
let
|
||||
rpcResult = genSym(nskLet, "res") # temporary variable to hold `Response` from rpc call
|
||||
clientIdent = newIdentNode("client")
|
||||
procRes = ident"result" # proc return variable
|
||||
jsonRpcResult = # actual return value, `rpcResult`.result
|
||||
nnkDotExpr.newTree(rpcResult, newIdentNode("result"))
|
||||
|
||||
# perform rpc call
|
||||
callBody.add(quote do:
|
||||
let `rpcResult` = await client.call(`pathStr`, `jsonParamIdent`) # `rpcResult` is of type `Response`
|
||||
if `rpcResult`.error: raise newException(ValueError, $`rpcResult`.result) # TODO: is raise suitable here?
|
||||
let `rpcResult` = await `clientIdent`.call(`pathStr`, `jsonParamIdent`) # `rpcResult` is of type `Response`
|
||||
if `rpcResult`.error: raise newException(ValueError, $`rpcResult`.result)
|
||||
)
|
||||
|
||||
if customReturnType:
|
||||
@ -39,8 +39,11 @@ proc processClient(server: RpcServer, client: AsyncSocket) {.async.} =
|
||||
if future.readError of RpcProcError:
|
||||
let err = future.readError.RpcProcError
|
||||
await client.sendError(err.code, err.msg, err.data)
|
||||
elif future.readError of ValueError:
|
||||
let err = future.readError[].ValueError
|
||||
await client.sendError(INVALID_PARAMS, err.msg, %"")
|
||||
else:
|
||||
await client.sendError(SERVER_ERROR, "Error", %getCurrentExceptionMsg())
|
||||
await client.sendError(SERVER_ERROR, "Error: Unknown error occurred", %"")
|
||||
|
||||
proc serve*(server: RpcServer) {.async.} =
|
||||
server.socket.bindAddr(server.port, server.address)
|
||||
@ -1,3 +1,3 @@
|
||||
import eth-rpc / client / clientdispatch
|
||||
export clientdispatch
|
||||
import eth-rpc / client / client
|
||||
export client
|
||||
|
||||
|
||||
@ -1,2 +1,2 @@
|
||||
import r"eth-rpc/server" / [servertypes, rpcconsts, serverdispatch]
|
||||
export servertypes, rpcconsts, serverdispatch
|
||||
import r"eth-rpc/server" / [servertypes, rpcconsts, server]
|
||||
export servertypes, rpcconsts, server
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user