From 7a57a453918abbcafb213a300734adab90c76306 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20Uhl=C3=AD=C5=99?= Date: Wed, 27 Nov 2024 12:38:52 +0100 Subject: [PATCH] chore: feedback implementation --- ethers/errors.nim | 3 -- ethers/provider.nim | 12 +++--- ethers/providers/jsonrpc/subscriptions.nim | 43 ++++++++++++---------- ethers/signer.nim | 3 ++ 4 files changed, 32 insertions(+), 29 deletions(-) diff --git a/ethers/errors.nim b/ethers/errors.nim index e90f6b3..0151c93 100644 --- a/ethers/errors.nim +++ b/ethers/errors.nim @@ -8,9 +8,6 @@ type ProviderError* = object of EthersError data*: ?seq[byte] -template raiseSignerError*(message: string, parent: ref ProviderError = nil) = - raise newException(SignerError, message, parent) - {.push raises:[].} proc toErr*[E1: ref CatchableError, E2: EthersError]( diff --git a/ethers/provider.nim b/ethers/provider.nim index e9372cf..9c489ef 100644 --- a/ethers/provider.nim +++ b/ethers/provider.nim @@ -277,14 +277,14 @@ proc confirm*( blockEvent.clear() if blockSubscriptionResult.isErr: - let err = blockSubscriptionResult.error() + let error = blockSubscriptionResult.error() - if err of SubscriptionError: - raise cast[ref SubscriptionError](err) - elif err of CancelledError: - raise cast[ref CancelledError](err) + if error of SubscriptionError: + raise cast[ref SubscriptionError](error) + elif error of CancelledError: + raise cast[ref CancelledError](error) else: - raise err.toErr(ProviderError) + raise error.toErr(ProviderError) if blockNumber >= finish: await subscription.unsubscribe() diff --git a/ethers/providers/jsonrpc/subscriptions.nim b/ethers/providers/jsonrpc/subscriptions.nim index fbb6909..c060cc9 100644 --- a/ethers/providers/jsonrpc/subscriptions.nim +++ b/ethers/providers/jsonrpc/subscriptions.nim @@ -162,7 +162,7 @@ proc new*(_: type JsonRpcSubscriptions, let subscriptions = PollingSubscriptions(client: client) - proc resubscribe(id: JsonNode) {.async: (raises: [CancelledError, SubscriptionError]).} = + proc resubscribe(id: JsonNode): Future[?!void] {.async: (raises: [CancelledError]).} = try: var newId: JsonNode # Log filters are stored in logFilters, block filters are not persisted @@ -174,19 +174,23 @@ proc new*(_: type JsonRpcSubscriptions, else: newId = await subscriptions.client.eth_newBlockFilter() subscriptions.subscriptionMapping[id] = newId - except CancelledError as error: - raise error + except CancelledError as e: + raise e except CatchableError as e: - raise newException(SubscriptionError, "HTTP polling: There was an exception while getting subscription changes: " & e.msg, e) + return failure(void, e.toErr(SubscriptionError, "HTTP polling: There was an exception while getting subscription changes: " & e.msg)) - proc getChanges(id: JsonNode): Future[JsonNode] {.async: (raises: [CancelledError, SubscriptionError]).} = + return success() + + proc getChanges(id: JsonNode): Future[?!JsonNode] {.async: (raises: [CancelledError]).} = if mappedId =? subscriptions.subscriptionMapping.?[id]: try: let changes = await subscriptions.client.eth_getFilterChanges(mappedId) if changes.kind == JArray: - return changes + return success(changes) except JsonRpcError as e: - await resubscribe(id) + if error =? (await resubscribe(id)).errorOption: + return failure(JsonNode, error) + # TODO: we could still miss some events between losing the subscription # and resubscribing. We should probably adopt a strategy like ethers.js, # whereby we keep track of the latest block number that we've seen @@ -194,26 +198,25 @@ proc new*(_: type JsonRpcSubscriptions, # https://github.com/ethers-io/ethers.js/blob/f97b92bbb1bde22fcc44100af78d7f31602863ab/packages/providers/src.ts/base-provider.ts#L977 if not ("filter not found" in e.msg): - raise newException(SubscriptionError, "HTTP polling: There was an exception while getting subscription changes: " & e.msg, e) + return failure(JsonNode, e.toErr(SubscriptionError, "HTTP polling: There was an exception while getting subscription changes: " & e.msg)) except CancelledError as e: raise e except SubscriptionError as e: - raise e + return failure(JsonNode, e) except CatchableError as e: - raise newException(SubscriptionError, "HTTP polling: There was an exception while getting subscription changes: " & e.msg, e) - return newJArray() + return failure(JsonNode, e.toErr(SubscriptionError, "HTTP polling: There was an exception while getting subscription changes: " & e.msg)) + return success(newJArray()) proc poll(id: JsonNode) {.async: (raises: [CancelledError]).} = without callback =? subscriptions.getCallback(id): return - try: - for change in await getChanges(id): - callback(id, success(change)) - except CancelledError as e: - raise e - except CatchableError as e: - callback(id, failure(JsonNode, e)) + without changes =? (await getChanges(id)), error: + callback(id, failure(JsonNode, error)) + return + + for change in changes: + callback(id, success(change)) proc poll {.async: (raises: []).} = try: @@ -244,8 +247,8 @@ method subscribeBlocks(subscriptions: PollingSubscriptions, except CancelledError as e: discard except CatchableError as e: - let err = e.toErr(SubscriptionError, "HTTP polling: There was an exception while getting subscription's block: " & e.msg) - onBlock(failure(Block, err)) + let error = e.toErr(SubscriptionError, "HTTP polling: There was an exception while getting subscription's block: " & e.msg) + onBlock(failure(Block, error)) proc callback(id: JsonNode, changeResult: ?!JsonNode) {.raises:[].} = without change =? changeResult, e: diff --git a/ethers/signer.nim b/ethers/signer.nim index cf09794..76ee245 100644 --- a/ethers/signer.nim +++ b/ethers/signer.nim @@ -12,6 +12,9 @@ type Signer* = ref object of RootObj populateLock: AsyncLock +template raiseSignerError*(message: string, parent: ref ProviderError = nil) = + raise newException(SignerError, message, parent) + template convertError(body) = try: body