EatTheBlocks Forum

DAPP error handling in React

I’ve found that the only way to get the Revert or Require error thrown by a Smart Contract is to call the contract function with a Call not a Send. Send does not appear to return the error message.

Here is an example of what is returned from a Call:

Error: [object Object]
{
“message”: “VM Exception while processing transaction: revert Error: Partner already registered for this user”,
“code”: -32000,
“data”: {
“0x4bdefb5f01febe4d36b733b0284479e7b7154a8fa7fd0cb84faecf7baa4c0c19”: {
“error”: “revert”,
“program_counter”: 20449,
“return”: “0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002f4572726f723a20506172746e657220616c7265616479207265676973746572656420666f72207468697320757365720000000000000000000000000000000000”,
“reason”: “Error: Partner already registered for this user”
},
“stack”: “RuntimeError: VM Exception while processing transaction: revert Error: Partner already registered for this user\n at Function.RuntimeError.fromResults (/Users/paulcullen/.nvm/versions/node/v12.18.2/lib/node_modules/truffle/build/webpack:/node_modules/ganache-core/lib/utils/runtimeerror.js:89:1)\n at /Users/paulcullen/.nvm/versions/node/v12.18.2/lib/node_modules/truffle/build/webpack:/node_modules/ganache-core/lib/blockchain_double.js:573:1”,
“name”: “RuntimeError”
}
}

I can see the error message with the “message” tag. The problem is that there is a prefix of Error: [object Object] so if I call JSON.parse I get a runtime error.

I tried using substring but then I get a further error saying that substring is not a function. I think it’s something to do with binding the function but I can’t find a solution.

I guess this is more a question about how to call functions internally in React.

My components are functional components not class based components. Calling function below.

Any clues appreciated.

Paul

async function RegPartner(e) {
e.preventDefault()
const pacct = e.target.elements[0].value
const pname = e.target.elements[1].value
const plimit = e.target.elements[2].value
const pcanreg = e.target.elements[3].value
console.log(‘Accounts:’, accounts[0])
try {
await contract1.methods
.regPartner(pacct, pname, plimit, pcanreg)
.call({ from: accounts[0] })
} catch (err) {
console.log(err)
const x = JSON.parse(err.subtring(22))
console.log(x)
}
setpAcct(pacct)
setpName(pname)
setpLimit(plimit)
setpCanReg(pcanreg)
}

in your catch block, do console.log(err.message)