11
votes

In javascript I run contract's method

contract[methodName](...params, { from: myAccount }, (err, response) => {
    console.log('get transaction', methodName, err, response);
    if (err) return reject(err);
    resolve(response);
});

and then reject transaction through MetaMask. In console get an error

MetaMask - RPC Error: Error: MetaMask Tx Signature: User denied transaction signature.

But I can't catch this error in my code. Callback not working.

How can i catch this error in JS?

1
Also having this problem currently..Noah Passalacqua
Same here. Doesn't work in Chrome (where I am running Metamask 4.7) but works in Firefox (running Metamask 3.x.x ) It completely breaks control flow - not only the exception seems not to be thrown, but neither is executed code which follows the web3 call.Patrik Stas
Same strange behavior, worked fine just few days ago. But now impossible to catch Metamask's exceptions... Looks like Chrome plugin problems. In Firefox still working well.Anton Pegov
Jup, having the same issue in Chrome + Metamask + local testing environment. Would be nice to get a Metamask developer in here.Yuri van Geffen
Same, solutions?imazzara

1 Answers

-1
votes

The following code example is tested on macOS with Metamask Plugin in Chrome (v78.0) and Firefox (v70.0) and gives you the expected error message in the case the transaction has been rejected by the user:

  createContract: function () {

        let myContract = new web3.eth.Contract(ABI, CONTRACT_ADDRESS, {
            from: account,
            gasLimit: web3.utils.toHex(7900000),
            gasPrice: web3.utils.toHex(1000000000)
        });
        myContract.methods.myMethodName().send().on("receipt", (receipt) => {

            // Transaction succeeded
            myContract.getPastEvents(
                "myEventName", {
                    fromBlock: receipt.blockNumber,
                    toBlock: receipt.blockNumber
                }, (errors, events) => {
                    for (let event in events) {
                        let returnValues = events[event].returnValues;
                        // Now do something with the event
                    }
                });

        }).catch(function (e) {
            // Transaction rejected or failed
            console.log(e);
        });
    }

The result in the Firefox console will be as expected (see last line app.js:218):

enter image description here

The result in the Chrome console will be as expected(see last line app.js:218):

enter image description here