web3 / web3.js

Collection of comprehensive TypeScript libraries for Interaction with the Ethereum JSON RPC API and utility functions.
https://web3js.org/
Other
19.24k stars 4.93k forks source link

Error: Invalid JSON RPC response: "" #4482

Closed francesco-clementi-92 closed 2 years ago

francesco-clementi-92 commented 2 years ago

Is there an existing issue for this?

Current Behavior

I'm using a websocket to subscribe to all pending transaction.

For each transaction hash received from the socket I execute web3.eth.getTransaction(transactionHash) such as I can filter to specific account address.

The code works for the first 30/40 seconds, than I start to receive the error Error: Invalid JSON RPC response: "".

Expected Behavior

Do not receive the error.

Steps to Reproduce

web3Ws.eth.subscribe('pendingTransactions', function (error, result) {
    }).on("data", async function (transactionHash) {
        try {
              const transaction = await web3.eth.getTransaction(transactionHash)
        } catch(e){
            console.error(e)
            process.exit();
        }
    })

The error came from web3-provider-http/lib/index.js:92

result = JSON.parse(result);

with: SyntaxError: Unexpected end of JSON input, result is an empty string.

Web3.js Version

1.6.0

Environment

Anything Else?

No response

jdevcs commented 2 years ago

Thanks @francesco-clementi-92 for reporting this, could you confirm which provider you were using? Its mentioned in error above that you are using websocket and error came from "web3-provider-http/lib/index.js:92", if its ws provider this might be code line

francesco-clementi-92 commented 2 years ago

The problem is when I'm using web3.eth.getTransaction(transactionHash), so it's right that the error is from the http provider. For the http provider I'm using moralis.

I have the same error "Error: Invalid JSON RPC response" with https://bsc-dataseed1.binance.org, but with a different message: <html>\r\n<head><title>403 Forbidden</title></head>\r\n<body>\r\n<center><h1>403 Forbidden</h1></center>\r\n</body>\r\n</html>\r\n

nazarhussain commented 2 years ago

I suspect its because of some rate limiting that you request start failing after some time. And as its clearly stated that response we get from the provider service is not valid JSON-PRC so web3 library can't parse it and you get that error.

I am not sure if that's some bug related to web3. Try using with geth client and if you came across same problem then share with us.

francesco-clementi-92 commented 2 years ago

I confirm that it's a rate limiting problem, but it would be awesome if the library could handle it better instead of a generic message of wrong JSON parse.

nazarhussain commented 2 years ago

@francesco-clementi-92 Thanks for clarifying. The library relies on JSON-RPC specification. If the error message is provided by the backend then you will get that human readable error message. But in this case it seems the endpoint returns a valid response but with invalid response body.

You can debug further by investigating the raw response body and response headers to confirm.

I am closing this issue, if you found that raw response was valid JSON-RPC error response but the web3 didn't show that error message feel free to reopen this issue.