I am attempting to invoke a smart contract using ethers.contract and the signer from ethers.providers.web3Provider to leverage MetaMask. If the transaction fails, I want to capture the error and either retry or invoke a different function. However, my project fails to capture the error from MetaMask. When I reject the transaction in MetaMask, it works as expected. Below is a snippet of my code:
import { ethers } from "ethers";
import { SignedChannel } from "../api/contractTypes/channel";
import { Web3Provider } from "../client/Provider";
export async function register(
params: SignedChannel,
adj: ethers.Contract,
provider: ethers.providers.WebSocketProvider | Web3Provider
) {
const registerParams = [params.serialize(), []];
console.log("Calling register");
const gasPrice = await provider.getGasPrice();
const tx = await adj.functions["register"](...registerParams, {
from: params.params.participants[0],
gasPrice: gasPrice.mul(2),
gasLimit: 30000000,
});
console.log("Result:", tx);
tx.wait().then(async (receipt: any) => {
console.log("Transaction confirmed in block:", receipt.blockNumber);
const updatedReceipt = await provider.getTransactionReceipt(tx.hash);
console.log(updatedReceipt);
});
return tx.hash;
}
try {
//register channel
await register(signedState, adj!, this.provider);
this.listenToChannelUpdate(adj!);
} catch (err) {
console.log("Error while register:", err);
process.status = "failedRegister";
process.error = new Error(
("Error while register: Invalid Signed State: " + err) as string
);
this.state.emitUpdateProcess(process.id);
}
If the Transaction fails, i would expect to see the Error with the added string: "Error while register..." but instead i get:
Uncaught (in promise) Error: transaction failed [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (transactionHash="0x829228b6c66278019247c93885b703958c0d7187b4c3307fe1a4cd03c5c495e1", transaction={"hash":"0x829228b6c66278019247c93885b703958c0d7187b4c3307fe1a4cd03c5c495e1","type":2,"accessList":null,"blockHash":null,"blockNumber":null,"transactionIndex":null,"confirmations":0,"from":"0xA298Fc05bccff341f340a11FffA30567a00e651f","gasPrice":{"type":"BigNumber","hex":"0xee6b2800"},"maxPriorityFeePerGas":{"type":"B...