From 3b559a90ded42fc1047086dc3daa7cb68d0cea85 Mon Sep 17 00:00:00 2001 From: Eric <5089238+emizzle@users.noreply.github.com> Date: Fri, 2 Feb 2024 18:43:47 +1100 Subject: [PATCH] handle exceptions during jsonrpc init There are too many exceptions to catch individually, including chronos raising CatchableError exceptions in await expansion. There are also many other errors captured inside of the new proc with CatchableError. Instead of making it more complicated and harder to read, I think sticking with excepting CatchableError inside of convertError is a sensible solution --- ethers/providers/jsonrpc.nim | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/ethers/providers/jsonrpc.nim b/ethers/providers/jsonrpc.nim index 3e097db..fc0e786 100644 --- a/ethers/providers/jsonrpc.nim +++ b/ethers/providers/jsonrpc.nim @@ -53,7 +53,7 @@ template convertError(body) = except JsonRpcError as error: raiseJsonRpcProviderError(error.msg) except CatchableError as error: - raise newException(JsonRpcProviderError, error.msg) + raiseJsonRpcProviderError(error.msg) # Provider @@ -72,20 +72,21 @@ proc new*( var client: RpcClient var subscriptions: JsonRpcSubscriptions - proc initialize {.async.} = - case parseUri(url).scheme - of "ws", "wss": - let websocket = newRpcWebSocketClient(getHeaders = jsonHeaders) - await websocket.connect(url) - client = websocket - subscriptions = JsonRpcSubscriptions.new(websocket) - else: - let http = newRpcHttpClient(getHeaders = jsonHeaders) - await http.connect(url) - client = http - subscriptions = JsonRpcSubscriptions.new(http, - pollingInterval = pollingInterval) - subscriptions.init() + proc initialize {.async: (raises:[JsonRpcProviderError]).} = + convertError: + case parseUri(url).scheme + of "ws", "wss": + let websocket = newRpcWebSocketClient(getHeaders = jsonHeaders) + await websocket.connect(url) + client = websocket + subscriptions = JsonRpcSubscriptions.new(websocket) + else: + let http = newRpcHttpClient(getHeaders = jsonHeaders) + await http.connect(url) + client = http + subscriptions = JsonRpcSubscriptions.new(http, + pollingInterval = pollingInterval) + subscriptions.init() proc awaitClient: Future[RpcClient] {.async:(raises:[JsonRpcProviderError]).} = convertError: @@ -97,9 +98,8 @@ proc new*( await initialized return subscriptions - convertError: - initialized = initialize() - return JsonRpcProvider(client: awaitClient(), subscriptions: awaitSubscriptions()) + initialized = initialize() + return JsonRpcProvider(client: awaitClient(), subscriptions: awaitSubscriptions()) proc callImpl( client: RpcClient,