rpcclient: check body.error first

This commit is contained in:
Node 2017-10-11 16:23:37 +04:00
parent b40ac6e081
commit d76ca37d0d
No known key found for this signature in database
GPG Key ID: 8E1B4DC29040BD90
2 changed files with 8 additions and 8 deletions

View File

@ -231,17 +231,17 @@ HTTPClient.prototype._request = async function _request(method, endpoint, json)
if (!res.body)
throw new Error('Bad response (no body).');
const 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);
if (res.statusCode !== 200)
throw new Error(`Status code: ${res.statusCode}.`);
const network = res.headers['x-bcoin-network'];
if (network && network !== this.network.type)
throw new Error('Bad response (wrong network).');
return res.body;
};

View File

@ -62,9 +62,6 @@ RPCClient.prototype.execute = async function execute(method, params) {
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).');
@ -74,6 +71,9 @@ RPCClient.prototype.execute = async function execute(method, params) {
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;
};