mirror of
https://github.com/status-im/nim-json-rpc.git
synced 2025-02-24 02:08:28 +00:00
wrapReply
now returns string, includes terminator
This commit is contained in:
parent
53baf0bffc
commit
383654f1f5
@ -1,15 +1,14 @@
|
||||
import json, asyncdispatch, asyncnet, jsonutils, private / debugutils
|
||||
|
||||
proc wrapReply*(id: JsonNode, value: JsonNode, error: JsonNode): JsonNode =
|
||||
return %{"jsonrpc": %"2.0", "result": value, "error": error, "id": id}
|
||||
proc wrapReply*(id: JsonNode, value: JsonNode, error: JsonNode): string =
|
||||
let node = %{"jsonrpc": %"2.0", "result": value, "error": error, "id": id}
|
||||
return $node & "\c\l"
|
||||
|
||||
proc sendError*(client: AsyncSocket, code: int, msg: string, id: JsonNode, data: JsonNode = newJNull()) {.async.} =
|
||||
## Send error message to client
|
||||
let error = %{"code": %(code), "message": %msg, "data": data}
|
||||
ifDebug: echo "Send error json: ", wrapReply(newJNull(), error, id).pretty, "\c\l"
|
||||
# REVIEW: prefer in-place appending instead of string concatenation
|
||||
# (see the similar comment in clientdispatch.nim)
|
||||
result = client.send($wrapReply(id, newJNull(), error) & "\c\l")
|
||||
ifDebug: echo "Send error json: ", wrapReply(newJNull(), error, id)
|
||||
result = client.send(wrapReply(id, newJNull(), error))
|
||||
|
||||
proc sendJsonError*(state: RpcJsonError, client: AsyncSocket, id: JsonNode, data = newJNull()) {.async.} =
|
||||
## Send client response for invalid json state
|
||||
|
@ -21,7 +21,7 @@ proc processMessage(server: RpcServer, client: AsyncSocket, line: string) {.asyn
|
||||
await client.sendError(METHOD_NOT_FOUND, "Method not found", id, %(methodName & " is not a registered method."))
|
||||
else:
|
||||
let callRes = await server.procs[methodName](node["params"])
|
||||
await client.send($wrapReply(id, callRes, newJNull()) & "\c\l")
|
||||
await client.send(wrapReply(id, callRes, newJNull()))
|
||||
|
||||
proc processClient(server: RpcServer, client: AsyncSocket) {.async.} =
|
||||
while true:
|
||||
|
Loading…
x
Reference in New Issue
Block a user