Extract exact error message from wagmi reverts

95 views Asked by At

Stack:

"viem": "^2.7.1",
"wagmi": "^2.5.5"

Have smart contract that revert with custom errors under some circumstances

if(getStringLength(_firstName) == 0) {
            revert InvalidFirstName();
}

on the client:

const { data, error, status, failureReason } = useSimulateContract({
    abi: PersonWorldABI,
    address: contractAddr,
    functionName: 'addNewPerson',
    args: [firstName, lastName]
  });
console.log('reason', failureReason)

I always manage to see something like:

ContractFunctionExecutionError: The contract function "addNewPerson" reverted.

Error: InvalidFirstName()
 
Contract Call:
  address:   0xd249cFCda226F48950384C8BBcC0075B8CAE8438
  function:  addNewPerson(string _firstName, string _lastName)
  args:                  (, )
  sender:    0x77B4922Fcc0Fa745Bcd7d76025E682CFfFc9a149

or:

reason The contract function "addNewPerson" reverted.

Error: InvalidFirstName()
 
Contract Call:
  address:   0xd249cFCda226F48950384C8BBcC0075B8CAE8438
  function:  addNewPerson(string _firstName, string _lastName)
  args:                  (, )
  sender:    0x77B4922Fcc0Fa745Bcd7d76025E682CFfFc9a149

but now idea how to extract this Error prop, any ideas?

0

There are 0 answers