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: {
|
auth: {
|
||||||
username: 'bitcoinrpc',
|
username: 'bitcoinrpc',
|
||||||
password: this.apiKey || ''
|
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)
|
if (res.statusCode === 404)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (!res.body)
|
if (res.statusCode === 401)
|
||||||
throw new Error('No body.');
|
throw new Error('Unauthorized (bad API key).');
|
||||||
|
|
||||||
if (res.statusCode !== 200) {
|
if (res.statusCode !== 200)
|
||||||
if (res.body.error)
|
|
||||||
throw new Error(res.body.error.message);
|
|
||||||
throw new Error('Status code: ' + res.statusCode);
|
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;
|
return res.body;
|
||||||
});
|
});
|
||||||
|
|||||||
@ -58,19 +58,24 @@ RPCClient.prototype.execute = co(function* execute(method, params) {
|
|||||||
auth: {
|
auth: {
|
||||||
username: 'bitcoinrpc',
|
username: 'bitcoinrpc',
|
||||||
password: this.apiKey || ''
|
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)
|
if (!res.body)
|
||||||
throw new Error('No body for JSON-RPC response.');
|
throw new Error('No body for JSON-RPC response.');
|
||||||
|
|
||||||
if (res.body.error)
|
if (res.body.error)
|
||||||
throw new RPCError(res.body.error.message, res.body.error.code);
|
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;
|
return res.body.result;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user