2024-03-18 16:02:39 +01:00
|
|
|
import std/strutils
|
2024-03-18 14:52:13 +01:00
|
|
|
import pkg/stew/byteutils
|
2024-03-18 16:02:39 +01:00
|
|
|
import ../../basics
|
2024-11-28 14:48:10 +01:00
|
|
|
import ../../errors
|
2024-03-18 14:52:13 +01:00
|
|
|
import ../../provider
|
|
|
|
|
import ./conversions
|
2025-07-08 12:10:02 +10:00
|
|
|
import pkg/websock/websock
|
2024-03-18 14:52:13 +01:00
|
|
|
|
2024-11-28 14:48:10 +01:00
|
|
|
export errors
|
|
|
|
|
|
2025-07-08 12:10:02 +10:00
|
|
|
{.push raises: [].}
|
2024-03-18 16:02:39 +01:00
|
|
|
|
2024-03-18 14:52:13 +01:00
|
|
|
type JsonRpcProviderError* = object of ProviderError
|
|
|
|
|
|
2024-03-18 16:02:39 +01: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 14:52:13 +01:00
|
|
|
let error = (ref JsonRpcProviderError)()
|
|
|
|
|
if "message" in json:
|
|
|
|
|
error.msg = json{"message"}.getStr
|
2024-03-18 16:02:39 +01:00
|
|
|
error.data = extractErrorData(json)
|
2024-03-18 14:52:13 +01:00
|
|
|
error
|
|
|
|
|
|
|
|
|
|
proc raiseJsonRpcProviderError*(
|
2025-07-08 12:10:02 +10:00
|
|
|
error: ref CatchableError, message = error.msg
|
|
|
|
|
) {.raises: [JsonRpcProviderError].} =
|
2025-05-27 18:03:19 +10:00
|
|
|
if json =? JsonNode.fromJson(error.msg):
|
2024-03-18 14:52:13 +01:00
|
|
|
raise JsonRpcProviderError.new(json)
|
|
|
|
|
else:
|
|
|
|
|
raise newException(JsonRpcProviderError, message)
|
|
|
|
|
|
|
|
|
|
template convertError*(body) =
|
2025-07-08 12:10:02 +10:00
|
|
|
convertErrorsTo(JsonRpcProviderError):
|
|
|
|
|
body
|