jsonrpc: move error handling to separate module

This commit is contained in:
Mark Spanbroek 2024-03-18 14:52:13 +01:00 committed by markspanbroek
parent 027b5c37ad
commit 52d7d3dbed
2 changed files with 29 additions and 18 deletions

View File

@ -11,10 +11,12 @@ import ../signer
import ./jsonrpc/rpccalls import ./jsonrpc/rpccalls
import ./jsonrpc/conversions import ./jsonrpc/conversions
import ./jsonrpc/subscriptions import ./jsonrpc/subscriptions
import ./jsonrpc/errors
export basics export basics
export provider export provider
export chronicles export chronicles
export errors.JsonRpcProviderError
{.push raises: [].} {.push raises: [].}
@ -26,7 +28,6 @@ type
client: Future[RpcClient] client: Future[RpcClient]
subscriptions: Future[JsonRpcSubscriptions] subscriptions: Future[JsonRpcSubscriptions]
JsonRpcProviderError* = object of ProviderError
JsonRpcSubscription* = ref object of Subscription JsonRpcSubscription* = ref object of Subscription
subscriptions: JsonRpcSubscriptions subscriptions: JsonRpcSubscriptions
id: JsonNode id: JsonNode
@ -37,23 +38,6 @@ type
address: ?Address address: ?Address
JsonRpcSignerError* = object of SignerError 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 # Provider
const defaultUrl = "http://localhost:8545" const defaultUrl = "http://localhost:8545"

View File

@ -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)