diff --git a/ethers/providers/jsonrpc.nim b/ethers/providers/jsonrpc.nim index e6efcfa..ca6d40a 100644 --- a/ethers/providers/jsonrpc.nim +++ b/ethers/providers/jsonrpc.nim @@ -11,10 +11,12 @@ import ../signer import ./jsonrpc/rpccalls import ./jsonrpc/conversions import ./jsonrpc/subscriptions +import ./jsonrpc/errors export basics export provider export chronicles +export errors.JsonRpcProviderError {.push raises: [].} @@ -26,7 +28,6 @@ type client: Future[RpcClient] subscriptions: Future[JsonRpcSubscriptions] - JsonRpcProviderError* = object of ProviderError JsonRpcSubscription* = ref object of Subscription subscriptions: JsonRpcSubscriptions id: JsonNode @@ -37,23 +38,6 @@ type address: ?Address JsonRpcSignerError* = object of SignerError -proc raiseJsonRpcProviderError( - message: string) {.raises: [JsonRpcProviderError].} = - - var message = message - if json =? JsonNode.fromJson(message): - if "message" in json: - message = json{"message"}.getStr - raise newException(JsonRpcProviderError, message) - -template convertError(body) = - try: - body - except JsonRpcError as error: - raiseJsonRpcProviderError(error.msg) - except CatchableError as error: - raiseJsonRpcProviderError(error.msg) - # Provider const defaultUrl = "http://localhost:8545" diff --git a/ethers/providers/jsonrpc/errors.nim b/ethers/providers/jsonrpc/errors.nim new file mode 100644 index 0000000..e6da61e --- /dev/null +++ b/ethers/providers/jsonrpc/errors.nim @@ -0,0 +1,27 @@ +import pkg/stew/byteutils +import ../../provider +import ./conversions + +type JsonRpcProviderError* = object of ProviderError + +func new(_: type JsonRpcProviderError, json: JsonNode): ref JsonRpcProviderError = + let error = (ref JsonRpcProviderError)() + if "message" in json: + error.msg = json{"message"}.getStr + error + +proc raiseJsonRpcProviderError*( + message: string) {.raises: [JsonRpcProviderError].} = + if json =? JsonNode.fromJson(message): + raise JsonRpcProviderError.new(json) + else: + raise newException(JsonRpcProviderError, message) + +template convertError*(body) = + try: + body + except JsonRpcError as error: + raiseJsonRpcProviderError(error.msg) + except CatchableError as error: + raiseJsonRpcProviderError(error.msg) +