RPC server handle null return value correctly
This commit is contained in:
parent
08ceae27e0
commit
ab3dd0009b
|
@ -96,10 +96,11 @@ proc processMessage*(client: RpcClient, line: string): Result[void, string] =
|
|||
requestFut.fail(newException(JsonRpcError, error))
|
||||
return ok()
|
||||
|
||||
if response.result.isNone:
|
||||
# Up to this point, the result should contains something
|
||||
if response.result.string.len == 0:
|
||||
return err("missing or invalid response result")
|
||||
|
||||
requestFut.complete(response.result.get)
|
||||
requestFut.complete(response.result)
|
||||
return ok()
|
||||
|
||||
except CatchableError as exc:
|
||||
|
|
|
@ -103,7 +103,7 @@ type
|
|||
ResponseRx* = object
|
||||
jsonrpc*: results.Opt[JsonRPC2]
|
||||
id* : results.Opt[RequestId]
|
||||
result* : results.Opt[JsonString]
|
||||
result* : JsonString
|
||||
error* : results.Opt[ResponseError]
|
||||
|
||||
ReBatchKind* = enum
|
||||
|
|
|
@ -18,6 +18,13 @@ template sourceDir: string = currentSourcePath.rsplit(DirSep, 1)[0]
|
|||
|
||||
type
|
||||
Variant = int | bool | string
|
||||
RefObject = ref object
|
||||
name: string
|
||||
|
||||
template derefType(T: type): untyped =
|
||||
typeof(T()[])
|
||||
|
||||
derefType(RefObject).useDefaultSerializationIn JrpcConv
|
||||
|
||||
createRpcSigs(RpcClient, sourceDir & "/private/file_callsigs.nim")
|
||||
|
||||
|
@ -33,6 +40,7 @@ createRpcSigsFromNim(RpcClient):
|
|||
proc get_Name(id: int): string
|
||||
proc getJsonString(name: string): JsonString
|
||||
proc getVariant(id: Variant): bool
|
||||
proc getRefObject(shouldNull: bool): RefObject
|
||||
|
||||
proc installHandlers(s: RpcServer) =
|
||||
s.rpc("shh_uninstallFilter") do(id: int) -> bool:
|
||||
|
@ -78,6 +86,10 @@ proc installHandlers(s: RpcServer) =
|
|||
return "moo"
|
||||
return "meow"
|
||||
|
||||
s.rpc("getRefObject") do(shouldNull: bool) -> Refobject:
|
||||
if shouldNull: return nil
|
||||
return RefObject(name: "meow")
|
||||
|
||||
suite "test callsigs":
|
||||
var server = newRpcSocketServer(["127.0.0.1:0"])
|
||||
server.installHandlers()
|
||||
|
@ -130,5 +142,13 @@ suite "test callsigs":
|
|||
let res4 = waitFor client.getVariant(33)
|
||||
check res4 == true
|
||||
|
||||
test "Handle null return value correctly":
|
||||
let res = waitFor client.getRefObject(true)
|
||||
check res.isNil
|
||||
|
||||
let res2 = waitFor client.getRefObject(false)
|
||||
check res2.isNil.not
|
||||
check res2.name == "meow"
|
||||
|
||||
server.stop()
|
||||
waitFor server.closeWait()
|
||||
|
|
|
@ -174,8 +174,8 @@ suite "jrpc_sys conversion":
|
|||
rx.jsonrpc.isSome
|
||||
rx.id.isSome
|
||||
rx.id.get.num == 777
|
||||
rx.result.isSome
|
||||
rx.result.get == JsonString("true")
|
||||
rx.result.string.len > 0
|
||||
rx.result == JsonString("true")
|
||||
rx.error.isNone
|
||||
|
||||
test "ResponseTx -> ResponseRx: id(string), err: nodata":
|
||||
|
@ -186,7 +186,7 @@ suite "jrpc_sys conversion":
|
|||
rx.jsonrpc.isSome
|
||||
rx.id.isSome
|
||||
rx.id.get.str == "gum"
|
||||
rx.result.isNone
|
||||
rx.result.string.len == 0
|
||||
rx.error.isSome
|
||||
rx.error.get.code == 999
|
||||
rx.error.get.message == "fatal"
|
||||
|
@ -200,7 +200,7 @@ suite "jrpc_sys conversion":
|
|||
rx.jsonrpc.isSome
|
||||
rx.id.isSome
|
||||
rx.id.get.str == "gum"
|
||||
rx.result.isNone
|
||||
rx.result.string.len == 0
|
||||
rx.error.isSome
|
||||
rx.error.get.code == 999
|
||||
rx.error.get.message == "fatal"
|
||||
|
|
Loading…
Reference in New Issue