Add support for `error` verb

This commit is contained in:
coffeepots 2018-06-26 17:15:28 +01:00
parent bed76ed00f
commit 68d428a0a9
1 changed files with 9 additions and 2 deletions

View File

@ -101,7 +101,7 @@ proc wrapReply*(id: JsonNode, value: JsonNode, error: JsonNode): string =
let node = %{"jsonrpc": %"2.0", "result": value, "error": error, "id": id} let node = %{"jsonrpc": %"2.0", "result": value, "error": error, "id": id}
return $node & "\c\l" return $node & "\c\l"
proc genErrorSending(name, writeCode: NimNode): NimNode = proc genErrorSending(name, writeCode, errorCode: NimNode): NimNode =
let let
res = newIdentNode("result") res = newIdentNode("result")
sendJsonErr = newIdentNode($name & "Json") sendJsonErr = newIdentNode($name & "Json")
@ -114,6 +114,7 @@ proc genErrorSending(name, writeCode: NimNode): NimNode =
template transport: untyped = clientTrans template transport: untyped = clientTrans
var value {.inject.} = wrapReply(id, newJNull(), error) var value {.inject.} = wrapReply(id, newJNull(), error)
`errorCode`
`res` = `writeCode` `res` = `writeCode`
proc `sendJsonErr`*(state: RpcJsonError, clientTrans: RpcClientTransport, id: JsonNode, proc `sendJsonErr`*(state: RpcJsonError, clientTrans: RpcClientTransport, id: JsonNode,
@ -215,6 +216,7 @@ macro defineRpcServerTransport*(procClientName: untyped, body: untyped = nil): u
closeCode = quote do: closeCode = quote do:
transport.close transport.close
afterReadCode = newStmtList() afterReadCode = newStmtList()
errorCode = newStmtList()
if body != nil: if body != nil:
body.expectKind nnkStmtList body.expectKind nnkStmtList
@ -245,6 +247,11 @@ macro defineRpcServerTransport*(procClientName: untyped, body: untyped = nil): u
# `transport`, the client transport # `transport`, the client transport
# `value`, which contains the data read by `readCode` # `value`, which contains the data read by `readCode`
afterReadCode = code afterReadCode = code
of "error":
# `transport`, the client transport
# `value`, the data returned from the invoked RPC
# Note: Update `value` so it's length can be sent afterwards
errorCode = code
else: error("Unknown RPC verb \"" & verb & "\"") else: error("Unknown RPC verb \"" & verb & "\"")
result = newStmtList() result = newStmtList()
@ -252,7 +259,7 @@ macro defineRpcServerTransport*(procClientName: untyped, body: untyped = nil): u
let let
sendErr = newIdentNode($procClientName & "SendError") sendErr = newIdentNode($procClientName & "SendError")
procMsgs = newIdentNode($procClientName & "ProcessMessages") procMsgs = newIdentNode($procClientName & "ProcessMessages")
result.add(genErrorSending(sendErr, writeCode)) result.add(genErrorSending(sendErr, writeCode, errorCode))
result.add(genProcessMessages(procMsgs, sendErr, writeCode)) result.add(genProcessMessages(procMsgs, sendErr, writeCode))
result.add(genProcessClient(procClientName, procMsgs, sendErr, readCode, afterReadCode, closeCode)) result.add(genProcessClient(procClientName, procMsgs, sendErr, readCode, afterReadCode, closeCode))