diff --git a/ethers/providers/jsonrpc.nim b/ethers/providers/jsonrpc.nim index 9419224..4083364 100644 --- a/ethers/providers/jsonrpc.nim +++ b/ethers/providers/jsonrpc.nim @@ -28,7 +28,12 @@ type JsonRpcProviderError* = object of EthersError SubscriptionHandler = proc(id, arguments: JsonNode): Future[void] {.gcsafe, upraises:[].} -template raiseProviderError(message: string) = +proc raiseProviderError(message: string) {.upraises: [JsonRpcProviderError].} = + var message = message + try: + message = parseJson(message){"message"}.getStr + except Exception: + discard raise newException(JsonRpcProviderError, message) template convertError(body) = diff --git a/ethers/testing.nim b/ethers/testing.nim index 30631c0..cc8674c 100644 --- a/ethers/testing.nim +++ b/ethers/testing.nim @@ -4,8 +4,7 @@ import pkg/ethers proc revertReason*(e: ref JsonRpcProviderError): string = try: - let json = parseJson(e.msg) - var msg = json{"message"}.getStr + var msg = e.msg const revertPrefixes = @[ # hardhat "Error: VM Exception while processing transaction: reverted with " & @@ -28,7 +27,7 @@ proc reverts*[T](call: Future[T]): Future[bool] {.async.} = discard await call # TODO test this return false except JsonRpcProviderError: - return true # TODO: check that error started with revert prefix + return true proc reverts*[T](call: Future[T], reason: string): Future[bool] {.async.} = try: diff --git a/testmodule/testTesting.nim b/testmodule/testTesting.nim index 3aa9a6d..2af51c4 100644 --- a/testmodule/testTesting.nim +++ b/testmodule/testTesting.nim @@ -1,4 +1,3 @@ -import std/json import std/strformat import pkg/asynctest import pkg/chronos @@ -9,10 +8,8 @@ import ./hardhat suite "Testing helpers": let revertReason = "revert reason" - let rpcResponse = %* { - "message": "Error: VM Exception while processing transaction: " & - fmt"reverted with reason string '{revertReason}'" - } + let rpcResponse = "Error: VM Exception while processing transaction: " & + fmt"reverted with reason string '{revertReason}'" test "checks that call reverts": proc call() {.async.} = @@ -58,9 +55,8 @@ suite "Testing helpers": test "revert handles non-standard revert prefix": let nonStdMsg = fmt"Provider VM Exception: reverted with {revertReason}" - let nonStdRpcResponse = %* { "message": nonStdMsg } proc call() {.async.} = - raise newException(JsonRpcProviderError, $nonStdRpcResponse) + raise newException(JsonRpcProviderError, nonStdMsg) check await call().reverts(nonStdMsg)