Skip to content

Commit

Permalink
Improve response handling
Browse files Browse the repository at this point in the history
* Better error handling for both RPC and REST calls.
  * Handle `text/html` and `text/plain` differences from both request
    types correctly.
  * Proxy response errors as error messages for easier debugging.
  * Add a test for batched requests where one of the request fails.
  * Handle errors in binary-encoded requests.
* Parse REST responses with JSONBigInt to avoid losing precision. This
  means that some of the response values will now be strings (e.g.
  the network difficulty on `getDifficulty()`).
  • Loading branch information
Pedro Branco authored and Rui Marinho committed Nov 27, 2017
1 parent d4ac43e commit 093d562
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ This feature is available to all JSON-RPC methods that accept arguments.

### Floating point number precision in JavaScript

Due to [Javascript's limited floating point precision](http://floating-point-gui.de/), all big numbers (numbers with more than 15 significant digits) are returned as strings to prevent precision loss.
Due to [JavaScript's limited floating point precision](http://floating-point-gui.de/), all big numbers (numbers with more than 15 significant digits) are returned as strings to prevent precision loss. This includes both the RPC and REST APIs.

### Version Checking
By default, all methods are exposed on the client independently of the version it is connecting to. This is the most flexible option as defining methods for unavailable RPC calls does not cause any harm and the library is capable of handling a `Method not found` response error correctly.
Expand Down
16 changes: 7 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ class Client {
this.request = Promise.promisifyAll(request.defaults({
agentOptions: this.agentOptions,
baseUrl: `${this.ssl.enabled ? 'https' : 'http'}://${this.host}:${this.port}`,
json: true,
strictSSL: this.ssl.strict,
timeout: this.timeout
}), { multiArgs: true });
Expand Down Expand Up @@ -139,7 +138,6 @@ class Client {
return this.request.postAsync({
auth: _.pickBy(this.auth, _.identity),
body: JSON.stringify(body),
json: false,
uri: '/'
}).bind(this)
.then(this.parser.rpc);
Expand All @@ -158,7 +156,7 @@ class Client {
encoding: extension === 'bin' ? null : undefined,
url: `/rest/tx/${hash}.${extension}`
}).bind(this)
.then(this.parser.rest);
.then(_.partial(this.parser.rest, extension));
}).asCallback(callback);
}

Expand All @@ -176,7 +174,7 @@ class Client {
encoding: extension === 'bin' ? null : undefined,
url: `/rest/block${summary ? '/notxdetails/' : '/'}${hash}.${extension}`
}).bind(this)
.then(this.parser.rest);
.then(_.partial(this.parser.rest, extension));
}).asCallback(callback);
}

Expand All @@ -196,7 +194,7 @@ class Client {
encoding: extension === 'bin' ? null : undefined,
url: `/rest/headers/${count}/${hash}.${extension}`
}).bind(this)
.then(this.parser.rest);
.then(_.partial(this.parser.rest, extension));
}).asCallback(callback);
}

Expand All @@ -210,7 +208,7 @@ class Client {

return this.request.getAsync(`/rest/chaininfo.json`)
.bind(this)
.then(this.parser.rest)
.then(_.partial(this.parser.rest, 'json'))
.asCallback(callback);
}

Expand All @@ -231,7 +229,7 @@ class Client {
encoding: extension === 'bin' ? null : undefined,
url: `/rest/getutxos/checkmempool/${sets}.${extension}`
}).bind(this)
.then(this.parser.rest)
.then(_.partial(this.parser.rest, extension))
.asCallback(callback);
}

Expand All @@ -245,7 +243,7 @@ class Client {

return this.request.getAsync('/rest/mempool/contents.json')
.bind(this)
.then(this.parser.rest)
.then(_.partial(this.parser.rest, 'json'))
.asCallback(callback);
}

Expand All @@ -263,7 +261,7 @@ class Client {

return this.request.getAsync('/rest/mempool/info.json')
.bind(this)
.then(this.parser.rest)
.then(_.partial(this.parser.rest, 'json'))
.asCallback(callback);
}
}
Expand Down
39 changes: 25 additions & 14 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@ import _ from 'lodash';
const { parse } = JSONBigInt({ storeAsString: true, strict: true }); // eslint-disable-line new-cap

/**
* Get response result and errors.
* Get RPC response body result.
*/

function get(body, { headers = false, response } = {}) {
if (!body) {
throw new RpcError(response.statusCode, response.statusMessage);
}

function getRpcResult(body, { headers = false, response } = {}) {
if (body.error !== null) {
throw new RpcError(
_.get(body, 'error.code', -32603),
_.get(body, 'error.message', 'An error occurred while processing the RPC call to bitcoind')
);
}

// Defensive measure. This should not happen on a RPC call.
if (!_.has(body, 'result')) {
throw new RpcError(-32700, 'Missing `result` on the RPC call result');
}
Expand All @@ -54,22 +51,23 @@ export default class Parser {
*/

rpc([response, body]) {
// Body contains HTML (e.g. 401 Unauthorized).
if (typeof body === 'string' && response.statusCode !== 200) {
throw new RpcError(response.statusCode);
// The RPC api returns a `text/html; charset=ISO-8859-1` encoded response with an empty string as the body
// when an error occurs.
if (typeof body === 'string' && response.headers['content-type'] !== 'application/json' && response.statusCode !== 200) {
throw new RpcError(response.statusCode, response.statusMessage, { body });
}

// Parsing the body with custom parser to support BigNumbers.
body = parse(body);

if (!Array.isArray(body)) {
return get(body, { headers: this.headers, response });
return getRpcResult(body, { headers: this.headers, response });
}

// Batch response parsing where each response may or may not be successful.
const batch = body.map(response => {
try {
return get(response, { headers: false, response });
return getRpcResult(response, { headers: false, response });
} catch (e) {
return e;
}
Expand All @@ -82,9 +80,22 @@ export default class Parser {
return batch;
}

rest([response, body]) {
if (body.error) {
throw new RpcError(body.error.code, body.error.message);
rest(extension, [response, body]) {
// The REST api returns a `text/plain` encoded response with the error line and the control
// characters \r\n. For readability and debuggability, the error message is set to this content.
// When requesting a binary response, the body will be returned as a Buffer representation of
// this error string.
if (response.headers['content-type'] !== 'application/json' && response.statusCode !== 200) {
if (body instanceof Buffer) {
body = body.toString('utf-8');
}

throw new RpcError(response.statusCode, body.replace('\r\n', ''), { body });
}

// Parsing the body with custom parser to support BigNumbers.
if (extension === 'json') {
body = parse(body);
}

if (this.headers) {
Expand Down
2 changes: 1 addition & 1 deletion test/errors/rpc-error_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Module dependencies.
*/

import RpcError from '../../src/errors/rpc-error';
import { STATUS_CODES } from 'http';
import RpcError from '../../src/errors/rpc-error';
import should from 'should';

/**
Expand Down
41 changes: 39 additions & 2 deletions test/index_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ describe('Client', () => {
} catch (e) {
e.should.be.an.instanceOf(RpcError);
e.message.should.equal('Unauthorized');
e.body.should.equal('');
e.code.should.equal(401);
e.status.should.equal(401);
}
});

Expand All @@ -133,9 +133,20 @@ describe('Client', () => {

const [newAddress, addressValidation] = await new Client(config.bitcoind).command(batch);

addressValidation.should.have.properties('address', 'isvalid', 'ismine', 'scriptPubKey');
addressValidation.should.have.properties('address', 'ismine', 'isvalid', 'scriptPubKey');
newAddress.should.be.a.String();
});

it('should return an error if one of the request fails', async () => {
const batch = [{ method: 'foobar' }, { method: 'validateaddress', parameters: ['mkteeBFmGkraJaWN5WzqHCjmbQWVrPo5X3'] }];

const [newAddressError, addressValidation] = await new Client(config.bitcoind).command(batch);

addressValidation.should.have.properties('address', 'ismine', 'isvalid', 'scriptPubKey');
newAddressError.should.be.an.instanceOf(RpcError);
newAddressError.message.should.equal('Method not found');
newAddressError.code.should.equal(-32601);
});
});

describe('headers', () => {
Expand Down Expand Up @@ -615,5 +626,31 @@ describe('Client', () => {
information.usage.should.be.a.Number();
});
});

it('should throw an error if a method contains invalid arguments', async () => {
try {
await new Client(config.bitcoind).getTransactionByHash('foobar');

should.fail();
} catch (e) {
e.should.be.an.instanceOf(RpcError);
e.body.should.equal('Invalid hash: foobar\r\n');
e.message.should.equal('Invalid hash: foobar');
e.code.should.equal(400);
}
});

it('should throw an error if a method in binary mode contains invalid arguments', async () => {
try {
await new Client(config.bitcoind).getTransactionByHash('foobar', { extension: 'bin' });

should.fail();
} catch (e) {
e.should.be.an.instanceOf(RpcError);
e.body.should.equal('Invalid hash: foobar\r\n');
e.message.should.equal('Invalid hash: foobar');
e.code.should.equal(400);
}
});
});
});

0 comments on commit 093d562

Please sign in to comment.