2024-03-18 15:02:39 +00:00
|
|
|
import std/strutils
|
2024-03-18 13:52:13 +00:00
|
|
|
import pkg/stew/byteutils
|
2024-03-18 15:02:39 +00:00
|
|
|
import ../../basics
|
2024-11-28 13:48:10 +00:00
|
|
|
import ../../errors
|
2024-03-18 13:52:13 +00:00
|
|
|
import ../../provider
|
|
|
|
import ./conversions
|
|
|
|
|
2024-11-28 13:48:10 +00:00
|
|
|
export errors
|
|
|
|
|
2024-03-18 15:02:39 +00:00
|
|
|
{.push raises:[].}
|
|
|
|
|
2024-03-18 13:52:13 +00:00
|
|
|
type JsonRpcProviderError* = object of ProviderError
|
|
|
|
|
2024-03-18 15:02:39 +00:00
|
|
|
func extractErrorData(json: JsonNode): ?seq[byte] =
|
|
|
|
if json.kind == JObject:
|
|
|
|
if "message" in json and "data" in json:
|
|
|
|
let message = json{"message"}.getStr()
|
|
|
|
let hex = json{"data"}.getStr()
|
|
|
|
if "reverted" in message and hex.startsWith("0x"):
|
|
|
|
if data =? hexToSeqByte(hex).catch:
|
|
|
|
return some data
|
|
|
|
for key in json.keys:
|
|
|
|
if data =? extractErrorData(json{key}):
|
|
|
|
return some data
|
|
|
|
|
|
|
|
func new*(_: type JsonRpcProviderError, json: JsonNode): ref JsonRpcProviderError =
|
2024-03-18 13:52:13 +00:00
|
|
|
let error = (ref JsonRpcProviderError)()
|
|
|
|
if "message" in json:
|
|
|
|
error.msg = json{"message"}.getStr
|
2024-03-18 15:02:39 +00:00
|
|
|
error.data = extractErrorData(json)
|
2024-03-18 13:52:13 +00:00
|
|
|
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)
|
|
|
|
|