Solidity – How to Properly Format RPC Error Messages from MetaMask

javascriptsolidity

Hello I'm interacting with a smart contract and I have require function in the solidity smart contract, I would like to show these messages properly formatted in the console when I log the error I get type object but I cannot acces properties can someone shed light, thanks ahead!

console.log(e.message)

Error: execution reverted: Already claimed
{
  "originalError": {
    "code": 3,
    "data": "0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000f416c726561647920636c61696d65640000000000000000000000000000000000",
    "message": "execution reverted: Already claimed"
  }
}
    at Object._fireError (index.js?5ba3:49)
    at sendTxCallback (index.js?7789:539)
    at cb (util.js?3022:689)
    at callbackifyOnRejected (util.js?3022:666)
    at eval (process.js?4362:5)

enter image description here

Best Answer

I am working on a similar issue, not sure if you have figured this out, but some of the actual error message data comes back as a stringified JSON object. In my experience those errors usually come with code: and message: keys inside. You would to take say error.message, and JSON.parse() it.

For me the issue is I need to build an error handler to handle the 3 object formats i've encountered. Ideally I'm going to build it to handle RPC errors plus others. It kind of depends where the tx reverts too. Hope that helps someone.