From 33360528d208aaa70a6ed914619fe76321760d54 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Wed, 13 Dec 2023 03:21:50 +0100 Subject: [PATCH] fix improper yield usage (#175) `yield` does not work with chronos futures --- json_rpc/client.nim | 6 ++---- json_rpc/servers/httpserver.nim | 34 ++++++++++++++++++--------------- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/json_rpc/client.nim b/json_rpc/client.nim index 89e26ad..ed6cb30 100644 --- a/json_rpc/client.nim +++ b/json_rpc/client.nim @@ -29,12 +29,10 @@ proc rpcCallNode*(path: string, params: JsonNode, id: ClientId): JsonNode = %{"jsonrpc": %"2.0", "method": %path, "params": params, "id": %id} method call*(client: RpcClient, name: string, - params: JsonNode): Future[Response] {. - base, async, gcsafe, raises: [Defect].} = + params: JsonNode): Future[Response] {.base, async.} = discard -method close*(client: RpcClient): Future[void] {. - base, async, gcsafe, raises: [Defect].} = +method close*(client: RpcClient): Future[void] {.base, async.} = discard template `or`(a: JsonNode, b: typed): JsonNode = diff --git a/json_rpc/servers/httpserver.nim b/json_rpc/servers/httpserver.nim index 15f2409..cdc81cf 100644 --- a/json_rpc/servers/httpserver.nim +++ b/json_rpc/servers/httpserver.nim @@ -39,23 +39,27 @@ proc processClientRpc(rpcServer: RpcHttpServer): HttpProcessCallback = if not res.isNil: return res - let body = await request.getBody() - - let headers = HttpTable.init([("Content-Type", + let + body = await request.getBody() + headers = HttpTable.init([("Content-Type", "application/json; charset=utf-8")]) - let future = rpcServer.route(string.fromBytes(body)) - yield future - if future.failed: - debug "Internal error while processing JSON-RPC call" - return await request.respond(Http503, - "Internal error while processing JSON-RPC call", - headers) - else: - var data = future.read() - let res = await request.respond(Http200, data, headers) - trace "JSON-RPC result has been sent" - return res + data = + try: + await rpcServer.route(string.fromBytes(body)) + except CancelledError as exc: + raise exc + except CatchableError as exc: + debug "Internal error while processing JSON-RPC call" + return await request.respond( + Http503, + "Internal error while processing JSON-RPC call: " & exc.msg, + headers) + + res = await request.respond(Http200, data, headers) + + trace "JSON-RPC result has been sent" + return res else: return dumbResponse()