12

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 May 31 '18 at 17:50
  • 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 Jun 01 '18 at 02:17
  • 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 Jun 01 '18 at 13:22
  • Jup, having the same issue in Chrome + Metamask + local testing environment. Would be nice to get a Metamask developer in here. – Yuri van Geffen Jun 01 '18 at 13:42
  • 1
    Same, solutions? – imazzara Jun 04 '18 at 21:43

2 Answers2

2

Do this if you are using Ethers library:

contract.methodName(...params, { from: myAccount })
.then(tx => {
    //do whatever you want with tx
})
.catch(e => {
     if (e.code === 4001){
         //user rejected the transaction
     } 
});
Silvio Guedes
  • 1,134
  • 15
  • 16
0

I was also facing the same issue and I fixed it by using this library

https://github.com/enzoferey/ethers-error-parser

npm install @enzoferey/ethers-error-parser

import { getParsedEthersError } from "@enzoferey/ethers-error-parser";

try {
  const transaction = await someContract.someMethod();
  await transaction.wait();
} catch (error) {
  const parsedEthersError = getParsedEthersError(error);
  // parsedError.errorCode - contains a well defined error code (see full list below)
  // parsedError.context - contains a context based string providing additional information
  // profit ! 
}

If the user rejects the transaction then the error code will be REJECTED_TRANSACTION

You can find a complete list of error codes from official docs here: https://github.com/MetaMask/eth-rpc-errors/blob/main/src/error-constants.ts