matic.js icon indicating copy to clipboard operation
matic.js copied to clipboard

ERR_UNHANDLED_REJECTION occurs in the method PosClient.isCheckPointed()

Open Spider-yuchen opened this issue 2 years ago • 1 comments

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 "#<Object>". ] { 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<POSClient> => { 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 }

Spider-yuchen avatar May 05 '23 08:05 Spider-yuchen

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.

Sahu-Debasish avatar Oct 03 '23 02:10 Sahu-Debasish