http/client: better unauth error message.

This commit is contained in:
Christopher Jeffrey 2017-05-13 02:48:15 -07:00
parent af0ab46f21
commit 4cf82c442f
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
2 changed files with 28 additions and 18 deletions

View File

@ -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;
});

View File

@ -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;
});