i am trying to call a smartcontract with ethers.contract and the signer from ethers.providers.web3Provider to use MetaMask.In case the transaction fails i want to catch the error and try again or call a different function but my project just fails without catching the error from metamask. In case i reject the transaction on metaMask it works fine. Here is part 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...
I found the Error,it was because i tried to catch an error thrown inside of a promise with a try/catch-block which apparently i cannot do. After i changed the conclude function to throw the errors outside of the promise, it worked fine.