fix missing req id in router exception handler

when an rpc method in server throw `InvalidRequest` using custom error code,
the router need to mention the request id too.

otherwise the client will throw error with confusing message.
This commit is contained in:
jangko 2022-02-28 09:39:49 +07:00
parent 0540afad4c
commit ad0c3fb6e0
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
2 changed files with 25 additions and 1 deletions

View File

@ -81,7 +81,7 @@ proc route*(router: RpcRouter, node: JsonNode): Future[StringOfJson] {.async, gc
let res = await rpcProc(if params == nil: newJArray() else: params) let res = await rpcProc(if params == nil: newJArray() else: params)
return wrapReply(id, res) return wrapReply(id, res)
except InvalidRequest as err: except InvalidRequest as err:
return wrapError(err.code, err.msg) return wrapError(err.code, err.msg, id)
except CatchableError as err: except CatchableError as err:
debug "Error occurred within RPC", methodName = methodName, err = err.msg debug "Error occurred within RPC", methodName = methodName, err = err.msg
return wrapError( return wrapError(

View File

@ -13,6 +13,9 @@ proc setupServer*(srv: RpcServer) =
srv.rpc("myError") do(input: string, data: array[0..3, int]): srv.rpc("myError") do(input: string, data: array[0..3, int]):
raise (ref ValueError)(msg: "someMessage") raise (ref ValueError)(msg: "someMessage")
srv.rpc("invalidRequest") do():
raise (ref InvalidRequest)(code: -32001, msg: "Unknown payload")
suite "Socket Server/Client RPC": suite "Socket Server/Client RPC":
var srv = newRpcSocketServer(["localhost:8545"]) var srv = newRpcSocketServer(["localhost:8545"])
var client = newRpcSocketClient() var client = newRpcSocketClient()
@ -33,6 +36,13 @@ suite "Socket Server/Client RPC":
expect(CatchableError): # The error type wont be translated expect(CatchableError): # The error type wont be translated
discard waitFor client.call("myError", %[%"abc", %[1, 2, 3, 4]]) discard waitFor client.call("myError", %[%"abc", %[1, 2, 3, 4]])
test "Invalid request exception":
try:
discard waitFor client.call("invalidRequest", %[])
check false
except CatchableError as e:
check e.msg == """{"code":-32001,"message":"Unknown payload","data":null}"""
srv.stop() srv.stop()
waitFor srv.closeWait() waitFor srv.closeWait()
@ -56,6 +66,13 @@ suite "Websocket Server/Client RPC":
expect(CatchableError): # The error type wont be translated expect(CatchableError): # The error type wont be translated
discard waitFor client.call("myError", %[%"abc", %[1, 2, 3, 4]]) discard waitFor client.call("myError", %[%"abc", %[1, 2, 3, 4]])
test "Invalid request exception":
try:
discard waitFor client.call("invalidRequest", %[])
check false
except CatchableError as e:
check e.msg == """{"code":-32001,"message":"Unknown payload","data":null}"""
srv.stop() srv.stop()
waitFor srv.closeWait() waitFor srv.closeWait()
@ -81,5 +98,12 @@ suite "Websocket Server/Client RPC with Compression":
expect(CatchableError): # The error type wont be translated expect(CatchableError): # The error type wont be translated
discard waitFor client.call("myError", %[%"abc", %[1, 2, 3, 4]]) discard waitFor client.call("myError", %[%"abc", %[1, 2, 3, 4]])
test "Invalid request exception":
try:
discard waitFor client.call("invalidRequest", %[])
check false
except CatchableError as e:
check e.msg == """{"code":-32001,"message":"Unknown payload","data":null}"""
srv.stop() srv.stop()
waitFor srv.closeWait() waitFor srv.closeWait()