Ethers.js: How to catch Error: invalid response - 0

Created on 4 Dec 2018  路  14Comments  路  Source: ethers-io/ethers.js

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

investigate

Most helpful comment

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:

// If you REALLY need to be connected before doing something
provider.getNetwork().then((network) => {
    // Here you are guaranteed that the provider is ready to go...
});

All 14 comments

@thundo Try adding chainId in following line:
const provider = new ethers.providers.JsonRpcProvider('http://localhost:7545', { chainId: your chainID });

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?

Heya! This is something I've been planning to fix and will get to it soon. I don't think there is a need to query for the network ID until a request is actually made.

I think this is caused by trying to auto-lookup the network ID when the network is not up yet.

Can you confirm? Was your node up and running when you constructed the provider?

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:

// If you REALLY need to be connected before doing something
provider.getNetwork().then((network) => {
    // Here you are guaranteed that the provider is ready to go...
});

Heya! This is something I've been planning to fix and will get to it soon. I don't think there is a need to query for the network ID until a request is actually made.

I think this is caused by trying to auto-lookup the network ID when the network is not up yet.

Can you confirm? Was your node up and running when you constructed the provider?

I confirm the opposite. My node was down, so I totally expect some kind of error by the library that I can catch.

In addition, my goal was to check if the node is connected/reachable, so this could be a useful test.

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. :)

This should be fixed in 4.0.18. The unhandled promise exception is squashed internally.

Thanks! :)

Thanks @ricmoo. This fixes the error in the constructor... however I still have the same problem on first call.

Scenario: eth node is down, trying to call a contract method. Provider is created without net id.

Two Error: invalid response - 0 are thrown (one for the method and one for net_version), but I can only catch one of them.

Is Error: invalid response - 0 the generic error given if the provider is unreachable? I'm getting this error now inside a headless electron test environment and can't figure out why. Anything I could do to troubleshoot the connection to the provider?

Using ethers v4.0.26 btw

@bohendo check the protocol, too. ws isn't supported right now, try http.

I use ethers v4.0.33 and watch for contract events. The contract emits 5 events, but I get only 3 of them. I have 2 same errors in the logs. For the provider, I use blockdaemon. In event handlers, I use additional call to the contract. Maybe these errors are generated when events come to me or when I make additional requests after these events. I haven鈥檛 figured it out yet because it happened for the first time. What should I know to deal with this problem?

I鈥檓 not sure what blockdaemon is (link?), but it may be you are exceeding your request quota for the service. What are the errors you are getting?

@ricmoo blockdaemon is a shared node in cloud, seems there is no limits, but this node used by many people.

My error is:

Error: invalid response - 0
    at exports.XMLHttpRequest.request.onreadystatechange (/root/dev/bot/node_modules/ethers/utils/web.js:84:29)
    at exports.XMLHttpRequest.dispatchEvent (/root/dev/bot/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:591:25)
    at setState (/root/dev/bot/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:610:14)
    at exports.XMLHttpRequest.handleError (/root/dev/bot/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:532:5)
    at ClientRequest.errorHandler (/root/dev/bot/node_modules/xmlhttprequest/lib/XMLHttpRequest.js:459:14)
    at ClientRequest.emit (events.js:310:20)
    at TLSSocket.socketErrorListener (_http_client.js:426:9)
    at TLSSocket.emit (events.js:310:20)
    at emitErrorNT (internal/streams/destroy.js:92:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:60:3)
    at processTicksAndRejections (internal/process/task_queues.js:84:21) {
  statusCode: 0,
  responseText: 'Error: getaddrinfo EAI_AGAIN ethshared.bdnodes.net\n' +
    '    at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:64:26)'
}

@mishell-trickster That error usually indicates that the node was down or the internet had an issue (network link, DNS, wifi, etc) when the request was made or when the JsonRpcProvider was created.

If you try v5, it can be a little more resilient in the case of the node not responding when the JsonRpcProvider is created though.

It may be a throttling issue for the events though. Most services impose one to prevent abuse.

Was this page helpful?
0 / 5 - 0 ratings