Open Spider-yuchen opened 1 year ago
The error you're encountering is an "UnhandledPromiseRejection" error in Node.js, indicating that a promise rejection occurred but was not handled with a .catch()
or try/catch
block. The error message you provided also includes the following hint:
To address this issue, you should properly handle promise rejections in your code to prevent unhandled promise rejection errors. Here's a modified version of your code with added error handling:
const posClient = await getBridgeClient();
console.log(transactionReceipt.transactionHash);
try {
const isCheckPointed = await posClient.isCheckPointed(transactionReceipt.transactionHash);
console.log(`Hash: ${transactionReceipt.transactionHash} isCheckPointed: ${isCheckPointed}`);
if (isCheckPointed) {
const erc20RootToken = await getERC20RootToken();
await erc20RootToken.withdrawExit(transactionReceipt.transactionHash);
console.log(`${transactionReceipt.transactionHash} withdraw, txHash : ${transactionReceipt.transactionHash}`);
}
} catch (error) {
console.error("An error occurred:", error);
}
async function getBridgeClient() {
if (bridgeClient === null) {
bridgeClient = new POSClient();
const polygonConstructorArguments = {
privateKeys: ['...........................'],
providerOrUrl: '...................'
};
const etherConstructorArguments = {
privateKeys: ['........................'],
providerOrUrl: '...........'
};
try {
await bridgeClient.init({
network: 'testnet',
version: 'mumbai',
parent: {
provider: new HDWalletProvider(polygonConstructorArguments),
defaultConfig: {
from: Parent.from
}
},
child: {
provider: new HDWalletProvider(etherConstructorArguments),
defaultConfig: {
from: Child.from
}
}
});
} catch (error) {
console.error("An error occurred while initializing bridgeClient:", error);
}
}
return bridgeClient;
}
Here's what I've done:
I wrapped the code that uses await
inside a try/catch
block to catch any promise rejections or exceptions.
If any promise rejection or exception occurs within the try
block, it will be caught and logged to the console.
I added error handling to the getBridgeClient
function as well, so if there are any errors during the initialization of bridgeClient
, they will be caught and logged.
By adding these error handling blocks, you should be able to avoid the "UnhandledPromiseRejection" error and get more information about the specific error that's occurring in your code. Make sure to replace the console.error
statements with appropriate error handling actions, such as logging, reporting, or taking corrective actions based on your application's requirements.
Describe the bug 0x9cb0b04171ca692a156c174381c00f4ccebe543fcfcfbd865254f950f6778259 node:internal/process/promises:288 triggerUncaughtException(err, true / fromPromise /); ^ [UnhandledPromiseRejection: This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). The promise rejected with the reason "#
Hint
My code: const posClient = await getBridgeClient() console.log(transactionReceipt.transactionHash) const isCheckPointed = await posClient.isCheckPointed(transactionReceipt.transactionHash); console.log(
hash: ${transactionReceipt.transactionHash} isCheckPointed: ${isCheckPointed}
) if(isCheckPointed){ const erc20RootToken = await getERC20RootToken() await erc20RootToken.withdrawExit(transactionReceipt.transactionHash) console.log(${transactionReceipt.transactionHash} withdraw, txHash : ${transactionReceipt.transactionHash}
) }const getBridgeClient = async () : Promise => {
if (bridgeClient === null) {
bridgeClient = new POSClient()
}
return bridgeClient }