chore: fix async raises warnings (#100)

This commit is contained in:
Adam Uhlíř 2024-12-09 16:22:25 +01:00 committed by GitHub
parent 04d3548553
commit 037bef0256
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 34 deletions

View File

@ -8,10 +8,10 @@ requires "chronicles >= 0.10.3 & < 0.11.0"
requires "chronos >= 4.0.0 & < 4.1.0" requires "chronos >= 4.0.0 & < 4.1.0"
requires "contractabi >= 0.6.0 & < 0.7.0" requires "contractabi >= 0.6.0 & < 0.7.0"
requires "questionable >= 0.10.2 & < 0.11.0" requires "questionable >= 0.10.2 & < 0.11.0"
requires "https://github.com/codex-storage/nim-json-rpc >= 0.5.0 & < 0.6.0" requires "json_rpc >= 0.5.0 & < 0.6.0"
requires "serde >= 1.2.1 & < 1.3.0" requires "serde >= 1.2.1 & < 1.3.0"
requires "https://github.com/codex-storage/nim-stint-versioned >= 1.0.0 & < 2.0.0" requires "stint >= 0.8.0 & < 0.9.0"
requires "https://github.com/codex-storage/nim-stew-versioned >= 1.0.0 & < 2.0.0" requires "stew >= 0.2.0 & < 0.3.0"
requires "https://github.com/codex-storage/nim-eth-versioned >= 1.0.0 & < 2.0.0" requires "https://github.com/codex-storage/nim-eth-versioned >= 1.0.0 & < 2.0.0"
task test, "Run the test suite": task test, "Run the test suite":

View File

@ -24,6 +24,14 @@ type
{.push raises:[].} {.push raises:[].}
template convertErrorsToSubscriptionError(body) =
try:
body
except CancelledError as error:
raise error
except CatchableError as error:
raise error.toErr(SubscriptionError)
template `or`(a: JsonNode, b: typed): JsonNode = template `or`(a: JsonNode, b: typed): JsonNode =
if a.isNil: b else: a if a.isNil: b else: a
@ -56,22 +64,22 @@ proc setMethodHandler(
method subscribeBlocks*(subscriptions: JsonRpcSubscriptions, method subscribeBlocks*(subscriptions: JsonRpcSubscriptions,
onBlock: BlockHandler): onBlock: BlockHandler):
Future[JsonNode] Future[JsonNode]
{.async, base, raises: [CancelledError].} = {.async: (raises: [SubscriptionError, CancelledError]), base,.} =
raiseAssert "not implemented" raiseAssert "not implemented"
method subscribeLogs*(subscriptions: JsonRpcSubscriptions, method subscribeLogs*(subscriptions: JsonRpcSubscriptions,
filter: EventFilter, filter: EventFilter,
onLog: LogHandler): onLog: LogHandler):
Future[JsonNode] Future[JsonNode]
{.async, base.} = {.async: (raises: [SubscriptionError, CancelledError]), base.} =
raiseAssert "not implemented" raiseAssert "not implemented"
method unsubscribe*(subscriptions: JsonRpcSubscriptions, method unsubscribe*(subscriptions: JsonRpcSubscriptions,
id: JsonNode) id: JsonNode)
{.async, base.} = {.async: (raises: [CancelledError]), base.} =
raiseAssert "not implemented " raiseAssert "not implemented "
method close*(subscriptions: JsonRpcSubscriptions) {.async, base.} = method close*(subscriptions: JsonRpcSubscriptions) {.async: (raises: [SubscriptionError, CancelledError]), base.} =
let ids = toSeq subscriptions.callbacks.keys let ids = toSeq subscriptions.callbacks.keys
for id in ids: for id in ids:
await subscriptions.unsubscribe(id) await subscriptions.unsubscribe(id)
@ -102,7 +110,7 @@ proc new*(_: type JsonRpcSubscriptions,
method subscribeBlocks(subscriptions: WebSocketSubscriptions, method subscribeBlocks(subscriptions: WebSocketSubscriptions,
onBlock: BlockHandler): onBlock: BlockHandler):
Future[JsonNode] Future[JsonNode]
{.async, raises: [].} = {.async: (raises: [SubscriptionError, CancelledError]).} =
proc callback(id: JsonNode, argumentsResult: ?!JsonNode) {.raises: [].} = proc callback(id: JsonNode, argumentsResult: ?!JsonNode) {.raises: [].} =
without arguments =? argumentsResult, error: without arguments =? argumentsResult, error:
onBlock(failure(Block, error.toErr(SubscriptionError))) onBlock(failure(Block, error.toErr(SubscriptionError)))
@ -111,6 +119,7 @@ method subscribeBlocks(subscriptions: WebSocketSubscriptions,
let res = Block.fromJson(arguments{"result"}).mapFailure(SubscriptionError) let res = Block.fromJson(arguments{"result"}).mapFailure(SubscriptionError)
onBlock(res) onBlock(res)
convertErrorsToSubscriptionError:
let id = await subscriptions.client.eth_subscribe("newHeads") let id = await subscriptions.client.eth_subscribe("newHeads")
subscriptions.callbacks[id] = callback subscriptions.callbacks[id] = callback
return id return id
@ -119,7 +128,7 @@ method subscribeLogs(subscriptions: WebSocketSubscriptions,
filter: EventFilter, filter: EventFilter,
onLog: LogHandler): onLog: LogHandler):
Future[JsonNode] Future[JsonNode]
{.async.} = {.async: (raises: [SubscriptionError, CancelledError]).} =
proc callback(id: JsonNode, argumentsResult: ?!JsonNode) = proc callback(id: JsonNode, argumentsResult: ?!JsonNode) =
without arguments =? argumentsResult, error: without arguments =? argumentsResult, error:
onLog(failure(Log, error.toErr(SubscriptionError))) onLog(failure(Log, error.toErr(SubscriptionError)))
@ -128,15 +137,22 @@ method subscribeLogs(subscriptions: WebSocketSubscriptions,
let res = Log.fromJson(arguments{"result"}).mapFailure(SubscriptionError) let res = Log.fromJson(arguments{"result"}).mapFailure(SubscriptionError)
onLog(res) onLog(res)
convertErrorsToSubscriptionError:
let id = await subscriptions.client.eth_subscribe("logs", filter) let id = await subscriptions.client.eth_subscribe("logs", filter)
subscriptions.callbacks[id] = callback subscriptions.callbacks[id] = callback
return id return id
method unsubscribe*(subscriptions: WebSocketSubscriptions, method unsubscribe*(subscriptions: WebSocketSubscriptions,
id: JsonNode) id: JsonNode)
{.async.} = {.async: (raises: [CancelledError]).} =
try:
subscriptions.callbacks.del(id) subscriptions.callbacks.del(id)
discard await subscriptions.client.eth_unsubscribe(id) discard await subscriptions.client.eth_unsubscribe(id)
except CancelledError as e:
raise e
except CatchableError:
# Ignore if uninstallation of the subscribiton fails.
discard
# Polling # Polling
@ -235,7 +251,7 @@ method close*(subscriptions: PollingSubscriptions) {.async.} =
method subscribeBlocks(subscriptions: PollingSubscriptions, method subscribeBlocks(subscriptions: PollingSubscriptions,
onBlock: BlockHandler): onBlock: BlockHandler):
Future[JsonNode] Future[JsonNode]
{.async, raises:[CancelledError].} = {.async: (raises: [SubscriptionError, CancelledError]).} =
proc getBlock(hash: BlockHash) {.async: (raises:[]).} = proc getBlock(hash: BlockHash) {.async: (raises:[]).} =
try: try:
@ -255,6 +271,7 @@ method subscribeBlocks(subscriptions: PollingSubscriptions,
if hash =? BlockHash.fromJson(change): if hash =? BlockHash.fromJson(change):
asyncSpawn getBlock(hash) asyncSpawn getBlock(hash)
convertErrorsToSubscriptionError:
let id = await subscriptions.client.eth_newBlockFilter() let id = await subscriptions.client.eth_newBlockFilter()
subscriptions.callbacks[id] = callback subscriptions.callbacks[id] = callback
subscriptions.subscriptionMapping[id] = id subscriptions.subscriptionMapping[id] = id
@ -264,7 +281,7 @@ method subscribeLogs(subscriptions: PollingSubscriptions,
filter: EventFilter, filter: EventFilter,
onLog: LogHandler): onLog: LogHandler):
Future[JsonNode] Future[JsonNode]
{.async.} = {.async: (raises: [SubscriptionError, CancelledError]).} =
proc callback(id: JsonNode, argumentsResult: ?!JsonNode) = proc callback(id: JsonNode, argumentsResult: ?!JsonNode) =
without arguments =? argumentsResult, error: without arguments =? argumentsResult, error:
@ -274,6 +291,7 @@ method subscribeLogs(subscriptions: PollingSubscriptions,
let res = Log.fromJson(arguments).mapFailure(SubscriptionError) let res = Log.fromJson(arguments).mapFailure(SubscriptionError)
onLog(res) onLog(res)
convertErrorsToSubscriptionError:
let id = await subscriptions.client.eth_newFilter(filter) let id = await subscriptions.client.eth_newFilter(filter)
subscriptions.callbacks[id] = callback subscriptions.callbacks[id] = callback
subscriptions.logFilters[id] = filter subscriptions.logFilters[id] = filter
@ -282,7 +300,7 @@ method subscribeLogs(subscriptions: PollingSubscriptions,
method unsubscribe*(subscriptions: PollingSubscriptions, method unsubscribe*(subscriptions: PollingSubscriptions,
id: JsonNode) id: JsonNode)
{.async.} = {.async: (raises: [CancelledError]).} =
try: try:
subscriptions.logFilters.del(id) subscriptions.logFilters.del(id)
subscriptions.callbacks.del(id) subscriptions.callbacks.del(id)

View File

@ -1,5 +1,4 @@
import std/os import std/os
import std/sequtils
import std/importutils import std/importutils
import pkg/asynctest import pkg/asynctest
import pkg/serde import pkg/serde