ruimarinho / bitcoin-core

A modern Bitcoin Core REST and RPC client.
477 stars 186 forks source link

uncaughtException because of JSON.parse #97

Closed vbrvk closed 4 years ago

vbrvk commented 5 years ago

For bad responses it throws the error, which cannot be handled:

uncaughtException   SyntaxError: Unexpected token i in JSON at position 335
  - request-obfuscator.js:60 obfuscateResponse
    [proxy]/[bitcoin-core]/dist/src/logging/request-obfuscator.js:60:9

  - request-obfuscator.js:144 obfuscate
    [proxy]/[bitcoin-core]/dist/src/logging/request-obfuscator.js:144:3

  - request-logger.js:24
    [proxy]/[bitcoin-core]/dist/src/logging/request-logger.js:24:36

  - index.js:33 Request.<anonymous>
    [proxy]/[@uphold]/request-logger/src/index.js:33:9

  - request.js:1161 Request.<anonymous>
    [proxy]/[request]/request.js:1161:10

  - request.js:1083 IncomingMessage.<anonymous>
    [proxy]/[request]/request.js:1083:12

  - next_tick.js:139 _combinedTickCallback
    internal/process/next_tick.js:139:11

  - next_tick.js:181 process._tickCallback
    internal/process/next_tick.js:181:9
nareshdavra302 commented 5 years ago

I'm also facing same issue

pedrobranco commented 5 years ago

If any of you can provide the actual response of the bitcoin daemon (by changing the code in node_modules or something to add a console.log right before the obfuscation to happen) would be a great help to decode this issue @nareshdavra302 @borovik96

pedrobranco commented 5 years ago

Which RPC / REST method are you calling @borovik96 @nareshdavra302?

nareshdavra302 commented 5 years ago

@pedrobranco
I'm calling getBlock method of RPC and getting an error from only when there is a network delay. So I think when it cannot send the whole block, it not able to parse it, and throws an error that not catching in a try-catch block. I saw error /dist/src/logging/request-obfuscator.js line number 60

pedrobranco commented 5 years ago

Local edit the file node_modules/bitcoin-core/dist/src/logging/request-obfuscator.js just before line 60 and call getBlock again:

diff --git i/dist/src/logging/request-obfuscator.js w/dist/src/logging/request-obfuscator.js
index bb8ca52..e41c727 100644
--- i/dist/src/logging/request-obfuscator.js
+++ w/dist/src/logging/request-obfuscator.js
@@ -57,6 +57,8 @@ function obfuscateResponse(request, instance) {
     return;
   }

+  console.log('Failing here:', request.body);
+
   request.body = JSON.parse(request.body);
   const requestBody = JSON.parse(instance.body);
chemuto commented 4 years ago

I'm having the same issue. It happens when the bitcoin node is down. The content of the response.body is: '<html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body bgcolor="white">\r\n<center><h1>502 Bad Gateway</h1></center>\r\n<hr><center>nginx/1.14.0 (Ubuntu)</center>\r\n</body>\r\n</html>\r\n'

pedrobranco commented 4 years ago

That's an error response from nginx (nginx/1.14.0 (Ubuntu)). The Bitcoin node itself gives an ECONNREFUSED error if it is down.

This client is not supposed to handle non JSON-RPC responses (in this case HTML).

pedrobranco commented 4 years ago

Closing since it's not an issue related with this project.

andrewda commented 3 years ago

@pedrobranco I know this is an old issue, but I'm running into the same issue with Bitcoin Core 0.20.0. The error I'm receiving from RPC is Work queue depth exceeded, which throws the following error that can't be caught:

/srv/node_modules/bitcoin-core/dist/src/logging/request-obfuscator.js:60
  request.body = JSON.parse(request.body);
                      ^
SyntaxError: Unexpected token W in JSON at position 0
    at JSON.parse (<anonymous>)
    at obfuscateResponse (/srv/node_modules/bitcoin-core/dist/src/logging/request-obfuscator.js:60:23)
    at obfuscate (/srv/node_modules/bitcoin-core/dist/src/logging/request-obfuscator.js:142:3)
    at /srv/node_modules/bitcoin-core/dist/src/logging/request-logger.js:24:36
    at Request.<anonymous> (/srv/node_modules/@uphold/request-logger/src/index.js:33:9)
    at Request.emit (events.js:322:22)
    at Request.EventEmitter.emit (domain.js:482:12)
    at Request.<anonymous> (/srv/node_modules/request/request.js:1154:10)
    at Request.emit (events.js:310:20)
    at Request.EventEmitter.emit (domain.js:482:12)
    at IncomingMessage.<anonymous> (/srv/node_modules/request/request.js:1076:12)
    at Object.onceWrapper (events.js:416:28)
    at IncomingMessage.emit (events.js:322:22)
    at IncomingMessage.EventEmitter.emit (domain.js:482:12)
    at endReadableNT (_stream_readable.js:1187:12)
    at processTicksAndRejections (internal/process/task_queues.js:84:21)

I can resolve this error by slowing the number of concurrent requests, but this library still shouldn't throw an unhandleable exception for a common error like that.

https://github.com/ruimarinho/bitcoin-core/pull/98 seems to resolve the issue, so something like that might be good to implement.

https://github.com/ruimarinho/bitcoin-core/pull/69 also looks related.

If the above PRs look roughly acceptable, I can work on fixing them up and hopefully getting them merged. :+1:

rondorkerin commented 3 years ago

Why is this issue closed? Why can we not try/catch any calls to the client to handle this issue? We have an overloaded bitcoin node and we get this issue on a regular basis crashing our processes. Even the process handlers for unhandled rejections / syntax errors can't stop this error from crashing us. Can we just make a try/catch in the obfuscator file? There has to be some reasonable hack to solve this.

andrewda commented 3 years ago

@sbryant31 I've been using https://github.com/andrewda/bitcoin-core#v3.0.1 for a couple months on a large-scale production site and haven't run into any issues. I'll try to get a PR opened tonight.