Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ERR_UNHANDLED_REJECTION occurs in the method PosClient.isCheckPointed() #394

Open
Spider-yuchen opened this issue May 5, 2023 · 1 comment

Comments

@Spider-yuchen
Copy link

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 "#".
] {
code: 'ERR_UNHANDLED_REJECTION'
}
Node.js v18.16.0

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()

const polygonConstructorArguments : any = {
  privateKeys: ['...........................'],
  providerOrUrl: '...................'
}

const etherConstructorArguments : any = {
  privateKeys: ['........................'],
  providerOrUrl: '...........'
}

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
    }
  }
});

}

return bridgeClient
}

@DebasishBlockchain
Copy link

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:

  • Node.js v18.16.0

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:

  1. I wrapped the code that uses await inside a try/catch block to catch any promise rejections or exceptions.

  2. If any promise rejection or exception occurs within the try block, it will be caught and logged to the console.

  3. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants