jb55 / node-bitcoin

zero-dependency bitcoin core rpc lib for nodejs
The Unlicense
45 stars 127 forks source link

handle html responses gracefully #9

Closed freewil closed 12 years ago

freewil commented 12 years ago

Bitcoin will return HTML in some instances instead of JSON as expected currently. One such case is when invalid RPC credentials are sent to the bitcoin client resulting in the following response:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<HTML>
<HEAD>
<TITLE>Error</TITLE>
<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=ISO-8859-1'>
</HEAD>
<BODY><H1>401 Unauthorized.</H1></BODY>
</HTML>

Right now the following error occurs:


undefined:1

^
SyntaxError: Unexpected token <
    at Object.parse (native)
    at IncomingMessage.<anonymous> (lib/jsonrpc.js:62:28)
    at IncomingMessage.emit (events.js:88:20)
    at HTTPParser.onMessageComplete (http.js:137:23)
    at Socket.ondata (http.js:1150:24)
    at TCP.onread (net.js:354:27)
jb55 commented 12 years ago

This should be a pretty straightforward fix, would just be a matter of checking the http status code and returning a proper err in the callback. I'll take a look at this in the afternoon when I get home.