http/client: better unauth error message.
This commit is contained in:
parent
af0ab46f21
commit
4cf82c442f
@ -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;
|
||||
});
|
||||
|
||||
@ -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;
|
||||
});
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user