ethers.js: How to catch Error: invalid response - 0
Hello, I’m trying to connect to a local ganache node. However, during development I forgot to start the node, so I expected ethers to complain about it.
My mve is
const ethers = require('ethers');
const provider = new ethers.providers.JsonRpcProvider('http://localhost:7545');
and of course I get a
(node:7136) UnhandledPromiseRejectionWarning: Error: invalid response - 0
at exports.XMLHttpRequest.request.onreadystatechange (D:\mve\node_modules\ethers\utils\web.js:84:29)
at exports.XMLHttpRequest.dispatchEvent (D:\mve\node_modules\xmlhttprequest\lib\XMLHttpRequest.js:591:25)
at setState (D:\mve\node_modules\xmlhttprequest\lib\XMLHttpRequest.js:610:14)
at exports.XMLHttpRequest.handleError (D:\mve\node_modules\xmlhttprequest\lib\XMLHttpRequest.js:532:)
at ClientRequest.errorHandler (D:\mve\node_modules\xmlhttprequest\lib\XMLHttpRequest.js:459:14)
at ClientRequest.emit (events.js:182:13)
at Socket.socketErrorListener (_http_client.js:391:9)
at Socket.emit (events.js:182:13)
at emitErrorNT (internal/streams/destroy.js:82:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
(node:7136) UnhandledPromiseRejectionWarning: Unhandled promise rejection. 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(). (rejection id: 1)
(node:7136) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
due to JsonRpcProvider
calling _this.send('net_version'...
However… how can i catch this error? The call is lauched in the constructor and I can’t see a promise returned to monitor the call status…
Thank you
(As a side note… how can i poll continuosly for the network status (connected/disconnected)? Is net_version a good way?)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 15 (6 by maintainers)
Commits related to this issue
- Squashed unhandled promise exception for Providers that are never used (#362). — committed to ethers-io/ethers.js by ricmoo 6 years ago
Oh, and as for catching this error (in general, on a provider) you can use the
getNetwork()
instance method, which will always call the_ready
promise. For example:Right, so if you try to do anything, it should cause an error, but simply instantiating an object shouldn’t raise an exception. 😃
I have a fix locally. I’ll test it and push it tomorrow. 😃
Thank you @Pris17, it makes the rejection disappear.
However, what about the more general problem of rejecting a promise asynchronously in the constructor of
JsonRpcProvider
?