[SOLVED]Catching error from Metamask to display

Hi guys,

I use useWeb3Contract to interact with my solidity functions. When I click on a button I execute the function and I catch the message success and message error :

                onClick={async () =>
                    await voting({
                        onSuccess: (mess) => {
                            setArrayAddress((prevArray) =>[...prevArray, address])
                            handleSuccess(mess, 'info', `L\'adresse ${address} a bien été ajouté`, 'Ajout d\'une nouvelle adresse', 'bell')
                        onError: (err) => {
                            handleError(err, `${err}`, 'Erreur', 'x')

The result of the console.log :

Capture d’écran 2022-07-17 à 00.10.19

As you can see. it’s a huge text and just above there is the message error from metamask which is more readable and I would like to get the message from this error to display it but don’t know how to do it.

Any idea ?

In your console.log you can do console.log(err.message).

Already tried but same result

You are getting the error in a string. Maybe try to parse the original error with JSON.parse(err)

You can also use the error property from the useWeb3Contract hook to get the error.

const { error } = useWeb3Contract
useEffect(() => {
    if (error) {
  }, [error]);

I’m using the error directly here :

const {
        runContractFunction: voting,
        data: enterTxResponse,
    } = useWeb3Contract({
        abi: [
                "inputs": [
                        "internalType": "address",
                        "name": "_addr",
                        "type": "address"
                "name": "addVoter",
                "outputs": [],
                "stateMutability": "nonpayable",
                "type": "function"
        contractAddress: 'MyAddress',
        functionName: "addVoter",
        params: {
            _addr: address,

But same result :cry:

I tried to parse but I’ve got error because it’s not a valid JSON. The error start like that :

cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (error={"code":-32603,"message":"execution reverted: Already registered","data":{"originalError":{"code":3,"data":"0x08c379a000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000012416c726561647920726567697374657265640000000000000000000000000000","message":"execution reverted: Already registered"}}}, 

But there is a good thing is, I found how to do it :slight_smile:

I if console.log err.error.message I get only the message !