Parsing correctly errors from metamask, and other wallets

Hi, I’m coding at Swaap a quite standard web3 application. I sometimes have errors because of user (like “transaction underpriced”), sometimes because of the server (execution reverted).

My code is close to Uniswap frontend, but after an error, I’m not able to have a clear object. It’s a kind of string with text then escaped JSON

try {
      newShareWei = await api.viewOneAssetJoinShare(token, amount)
    } catch (error: any) {
     // error is not a good object

// I do nothing fancy in the function
async viewOneAssetJoinShare(token: Token, amount: Wei): Promise<Wei> {
    return await this.contract.methods
      .getJoinswapExternAmountInMMM(token.address, amount)
      .call({ from: this.account })

I receive error in this form (note the negative error code :smile:)

  "code": -32603,
  "message": "[ethjs-query] while formatting outputs from RPC '{\"value\":{\"code\":-32603,\"data\":{\"code\":-32000,\"message\":\"transaction underpriced\"}}}'",
  "stack": "Error: [ethjs-query] while formatting outputs from RPC '{\"value\":{\"code\":-32603,\"data\":{\"code\":-32000,\"message\":\"transaction underpriced\"}}}'"

or this one where all I want is the 44 code sent by solidity:

"Internal JSON-RPC error.\n{\n  \"code\": 3,\n  \"message\": \"execution reverted: 44\",\n  \"data\": \"0x08c379a0000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000023434000000000000000000000000000000000000000000000000000000000000\"\n}"

Do you have any idea on how to have correctly the object directly ?

I’m also afraid that it would be different with Ledger, Coinbase Wallet or others. And on top of that, reponse is different from Rinkeby and Polygon probably because JSON-RPC endpoints are slightly differents.

If you have an article to get best practices … :pray:

Hi @nicorama ,

Welcome to the MetaMask community.

Check the link to the MetaMask docs here : Introduction | MetaMask Docs

There is a table on the left side of page that may be able to get you where you need.

I already went there : /guide/ethereum-provider.html#errors

My problem is that I don’t receive always a clear object.

If I try error.code or , I often have undefined, when metamask message display this code or data in the console from inpage.js - unfortunately I can’t show pic to make it clear

console.log('>>>', error.code)

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.