2

How do you parse returned json rpc errors from a failed transaction to get the underying revert message if any? I'm on frontend.

example:

try {
    await contract.methods.someMethod().send({from: account})
} catch(e) {
    console.log(e)
}

the error looks like this:

Error: Internal JSON-RPC error.
{
  "code": 3,
  "message": "execution reverted: blah blah",
  "data": "0x12313532492somethingsomething"
}

I need to get the "execution reverted: blah blah" message


  • I tried:
console.log(e.message)

Internal JSON-RPC error.
{
  "code": 3,
  "message": "execution reverted: blah blah",
  "data": "0x12313532492somethingsomething"
}
console.log(JSON.parse(e))

JSON.parse: unexpected character at line 1 column 1 of the JSON data
3
  • See this docs.soliditylang.org/en/v0.8.11/…. Solidity generates Error(string) and Panic(uint256). You could define your own errors types.
    – Ismael
    Dec 30, 2021 at 6:34
  • 1
    looks like you got me wrong. I'm trying to parse json rpc errors on the frontend that are thrown on failed transaction.
    – Yash
    Dec 30, 2021 at 10:51
  • Normally the web3 libraries generate custom Error classes, therefore it depends what library you are using.
    – Richard
    Dec 30, 2021 at 11:14

2 Answers 2

1

My way of resolving this issue is:

Convert the error to string using e.toString() and then removing some chars and converting it to JSON with JSON.parse()

let errore = e.toString() 
if (errore.indexOf('Internal JSON-RPC error.') > -1) {
      errore = errore.replace('\n', '').replace("Error: ", '').replace('Internal JSON-RPC error.', '')
      errore = JSON.parse(errore)
     }
0

Metamask has an official NPM package for this: https://www.npmjs.com/package/eth-rpc-errors

It is supposed to work like this:

import { serializeError } from "eth-rpc-errors";

try {...} catch (metamaskError) {
    const serializedError = serializeError(metamaskError);
    console.log({ serializedError });
}

However it is currently buggy, the error object is empty: https://github.com/MetaMask/eth-rpc-errors/issues/51

Will update this answer if and when the problem is fixed.

Until then, I also had to manipulate the error string. In my case, the error looked like this:

Error: execution reverted
{
  "originalError": {
    "code": 3,
    "data": "0x233067ae",
    "message": "execution reverted"
  }
}

I managed to format the error data nicely like this (using Ethers.js):

const decodeError = (errRaw) => {
  const errorObject = JSON.parse(
    errRaw.toString().replace("Error: execution reverted", "")
  );

  const abi = require("../pathToAbi.json");

  const iface = new ethers.utils.Interface(abi);

  const errorFragment = iface.getError(errorObject.originalError.data);

  return errorFragment.name;
};

Your Answer

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

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