Eric 933103ca6d
Refactor error bubbling
Ensure that all errors bubble to the main `convertError` proc.

Add websockets mocks and tests.

Formatting updates via nph.
2025-07-08 12:10:02 +10:00

45 lines
1.2 KiB
Nim

import std/strutils
import pkg/stew/byteutils
import ../../basics
import ../../errors
import ../../provider
import ./conversions
import pkg/websock/websock
export errors
{.push raises: [].}
type JsonRpcProviderError* = object of ProviderError
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 =
let error = (ref JsonRpcProviderError)()
if "message" in json:
error.msg = json{"message"}.getStr
error.data = extractErrorData(json)
error
proc raiseJsonRpcProviderError*(
error: ref CatchableError, message = error.msg
) {.raises: [JsonRpcProviderError].} =
if json =? JsonNode.fromJson(error.msg):
raise JsonRpcProviderError.new(json)
else:
raise newException(JsonRpcProviderError, message)
template convertError*(body) =
convertErrorsTo(JsonRpcProviderError):
body