diff --git a/lib/http/client.js b/lib/http/client.js index aa2695e3..cb7229bc 100644 --- a/lib/http/client.js +++ b/lib/http/client.js @@ -220,26 +220,31 @@ HTTPClient.prototype._request = co(function* _request(method, endpoint, json) { auth: { username: 'bitcoinrpc', password: this.apiKey || '' - }, - expect: 'json' + } }); - network = res.headers['x-bcoin-network']; - - if (network && network !== this.network.type) - throw new Error('Wrong network.'); - if (res.statusCode === 404) return; - if (!res.body) - throw new Error('No body.'); + if (res.statusCode === 401) + throw new Error('Unauthorized (bad API key).'); - if (res.statusCode !== 200) { - if (res.body.error) - throw new Error(res.body.error.message); + if (res.statusCode !== 200) throw new Error('Status code: ' + res.statusCode); - } + + if (res.type !== 'json') + throw new Error('Bad response (wrong content-type).'); + + if (!res.body) + throw new Error('Bad response (no body).'); + + network = res.headers['x-bcoin-network']; + + if (network && network !== this.network.type) + throw new Error('Bad response (wrong network).'); + + if (res.body.error) + throw new Error(res.body.error.message); return res.body; }); diff --git a/lib/http/rpcclient.js b/lib/http/rpcclient.js index fc15b93b..f687c933 100644 --- a/lib/http/rpcclient.js +++ b/lib/http/rpcclient.js @@ -58,19 +58,24 @@ RPCClient.prototype.execute = co(function* execute(method, params) { auth: { username: 'bitcoinrpc', password: this.apiKey || '' - }, - expect: 'json' + } }); + if (res.statusCode === 401) + throw new RPCError('Unauthorized (bad API key).', -1); + + if (res.statusCode !== 200) + throw new Error('Status code: ' + res.statusCode); + + if (res.type !== 'json') + throw new Error('Bad response (wrong content-type).'); + if (!res.body) throw new Error('No body for JSON-RPC response.'); if (res.body.error) throw new RPCError(res.body.error.message, res.body.error.code); - if (res.statusCode !== 200) - throw new Error('Status code: ' + res.statusCode); - return res.body.result; });