RPC server handle null return value correctly

This commit is contained in:
jangko 2024-01-11 13:57:18 +07:00
parent 08ceae27e0
commit ab3dd0009b
No known key found for this signature in database
GPG Key ID: 31702AE10541E6B9
4 changed files with 28 additions and 7 deletions

View File

@ -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:

View File

@ -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

View File

@ -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()

View File

@ -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"