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?
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?
Share Improve this question asked May 31, 2018 at 10:16 Угуйлук ДжагатамскиУгуйлук Джагатамски 1291 gold badge1 silver badge4 bronze badges 5- 1 Also having this problem currently.. – Noah Passalacqua Commented May 31, 2018 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 pletely breaks control flow - not only the exception seems not to be thrown, but neither is executed code which follows the web3 call. – Patrik Stas Commented Jun 1, 2018 at 2: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 Commented Jun 1, 2018 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 Commented Jun 1, 2018 at 13:42
- 1 Same, solutions? – imazzara Commented Jun 4, 2018 at 21:43
2 Answers
Reset to default 2Do 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
}
});
I was also facing the same issue and I fixed it by using this library
https://github./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 plete list of error codes from official docs here: https://github./MetaMask/eth-rpc-errors/blob/main/src/error-constants.ts