2

I am using Alchemy as my RPC URL node provider, my connected address currently has 0 ETH for gas.

When I try to run a transaction that requires gas in a try-catch block, I error I catch is

Error: Internal JSON-RPC error

However, when I look at Alchemy's dashboard for details, it gives a more detailed error of

Code: -32000 Message: insufficient funds for gas * price + value

which is actually the detailed error message I am looking for so that I can handle it in my frontend app.

Is there a way to get the detailed error message in my frontend app using ethersJS instead of the generic Internal JSON-RPC error?

3
  • can you share some details on the code you're using to run the transaction? Jul 31, 2022 at 4:56
  • Also, I wonder if this is a change that needs to happen at the ethers.js level in order to support the more detailed error message.. Jul 31, 2022 at 4:56
  • started an issue to track/discuss in the ethers.js github repo here: github.com/ethers-io/ethers.js/issues/3216 Jul 31, 2022 at 5:08

1 Answer 1

2

If you're using ethers.js, you can use ethers-decode-error to handle the detailed error message in your frontend quite easily.

Install ethers-decode-error:

npm install ethers-decode-error --save

And then just decode the error in your try/catch block, for example:

import { ErrorDecoder } from 'ethers-decode-error'

const errorDecoder = ErrorDecoder.create()

const WETH = new ethers.Contract('0xC02aa...756Cc2', abi, provider)
try {
  const tx = await WETH.transfer('0x0', amount)
  await tx.wait()
} catch (err) {
  const { type, name, reason } = await errorDecoder.decode(err)

  // Prints "Error type: RpcError"
  console.log('Error type:', type)
  // Prints "Error name: -32000"
  console.log('Error name:', name)
  // Prints "Error reason: insufficient funds for gas * price + value"
  console.log('Error reason:', reason)
}

It's quite straightforward. You can get the actual error message from the reason property of the decoded error. There are other information related to the error that can be used for handling the error too.

You can see more examples of handling errors here.

Playground Demo

Here's an actual code sandbox demo of using ethers-decode-error to handle errors when calling against the DAI contract on Ethereum mainnet using the Alchemy RPC: https://codesandbox.io/p/sandbox/youthful-drake-f5lfcp

Click the "Run Erroneous Transaction" button to send a transaction that will revert to see the error obtained.

Screenshot of the actual demo

And if you were to proceed with sending the transaction without sufficient funds as what the asker had probably done, you will get the clean detailed error like so:

enter image description here

4
  • This library requires the field that the Alchemy RPC doesn't include in the error so this wont work
    – Sid
    Feb 2 at 7:35
  • 1
    @Sid I have used the Alchemy RPC and it seems to work. I have updated the answer to include a codesandbox demo and screenshot using Alchemy RPC to extract the detailed error message. codesandbox.io/p/sandbox/youthful-drake-f5lfcp
    – toybox
    Feb 3 at 15:55
  • I spoke to the Alchemy team and they confirmed that they don't include the custom errors as it's optional in the RPC spec
    – Sid
    Mar 14 at 12:56
  • @Sid Would you mind sharing which field you are referring to? I’ve used Alchemy RPC in the demo above and it worked fine.
    – toybox
    Mar 15 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.