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

JsonRpcProvider giving UnhandledPromiseRejectionWarning when detectNetwork rejects #882

Closed
zemse opened this issue Jun 13, 2020 · 7 comments
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.

Comments

@zemse
Copy link
Collaborator

zemse commented Jun 13, 2020

Sorry if I'm interrupting too much. Running into issues :(

I am in a situation where ganache takes a while to start while my node js script loads up quickly. I am passing url into JsonRpcProvider constructor. But this throws up UnhandledPromiseRejectionWarning that I am not able to silence. It's breaking my pre-commit hook.

I digged in to the ethers.js code base and found that the networkOrReady promise is getting rejected in my case and there is no way to silence the warning.

networkOrReady = new Promise((resolve, reject) => {
setTimeout(() => {
this.detectNetwork().then((network) => {
resolve(network);
}, (error) => {
reject(error);
});
}, 0);
});

You can reproduce this with:

const ethers = require('ethers');

(async () => {
  try {
    const provider = new ethers.providers.JsonRpcProvider(
      'http://localhost:7545'
    );
  } catch (error) {
    console.log('Error is silenced');
  }
})();

Analyzing if this is a bug or not, if we take a web app example where user inputs a URL of node which can be valid or might not be. The developer should be able to catch the error without having it leak into the browser console.

Also, I started getting this when I updated from beta.189 to 5.0.1 (@ethersproject/providers moved from 5.0.0-beta.169 to 5.0.1).

@ricmoo ricmoo added the investigate Under investigation and may be a bug. label Jun 14, 2020
@ricmoo
Copy link
Member

ricmoo commented Jun 14, 2020

It should allow a deferred detectNetwork on error, but maybe I broke that when I added the "any" network support.

Looking into this now.

(keep bugging me; I expect some rough patches with the new release, so I'd like to take care of them as soon as possible ;))

@ricmoo
Copy link
Member

ricmoo commented Jun 14, 2020

Oh, also keep in mind the constructor won't throw if the network cannot be detected, since that can only be discovered asynchronously.

I think it makes sense to make the provider.ready promise stall (with exponential backoff) until the network has been successfully detected though.

So, after this change you'll be able to do:

(() => {
  const provider = new JsonRpcProvider();
  await provider.ready;

  // Now the provider is ready to go and you can call it normally
})();

@ricmoo
Copy link
Member

ricmoo commented Jun 14, 2020

Can you try out 5.0.2 and let me know if that solves your issue?

@ricmoo
Copy link
Member

ricmoo commented Jun 14, 2020

(keeping in mind you will need to await provider.ready)

@ricmoo ricmoo added bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published. and removed investigate Under investigation and may be a bug. labels Jun 14, 2020
zemse added a commit to KMPARDS/kami that referenced this issue Jun 14, 2020
@zemse
Copy link
Collaborator Author

zemse commented Jun 14, 2020

Thank you! This solves the problem.

@stoplion
Copy link

Is this still the way to go about fixing this error? I'm looking in the newest docs, not seeing any provider.ready in the new version.

@ricmoo
Copy link
Member

ricmoo commented Dec 22, 2020

If you are starting up a JsonRpcProvider before the node is running, this will still work exactly as you want. I’ll be sure to add it to the docs. I’m working on them this week. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified to be an issue. fixed/complete This Bug is fixed or Enhancement is complete and published.
Projects
None yet
Development

No branches or pull requests

3 participants