diff --git a/Changelog.md b/Changelog.md index 537c27f..f57d98c 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,9 @@ # node-bitcoin changelog +## v1.2.2 (2012/04/26) +Fix callback being called twice when a client and request error +occur on the same command call. + ## v1.2.1 (2012/04/26) * Add missing `getBlock` command diff --git a/lib/jsonrpc.js b/lib/jsonrpc.js index 9b7eeb3..1d32978 100644 --- a/lib/jsonrpc.js +++ b/lib/jsonrpc.js @@ -15,6 +15,7 @@ var Client = function(port, host, user, password) { this.call = function(method, params, callback, errback, path) { var client = http.createClient(port, host); + var clientRequestErrorCalled = false; // First we encode the request into JSON var requestJSON = JSON.stringify({ @@ -40,14 +41,15 @@ var Client = function(port, host, user, password) { var request = client.request('POST', path || '/', headers); request.write(requestJSON); - - client.on('error', function(e){ - errback(e); - }); - - request.on('error', function(e){ - errback(e); - }); + + function clientRequestErrorHandler(e) { + if (!clientRequestErrorCalled) { + clientRequestErrorCalled = true; + errback(e); + } + } + client.on('error', clientRequestErrorHandler); + request.on('error', clientRequestErrorHandler); request.on('response', function(response) { // We need to buffer the response chunks in a nonblocking way. diff --git a/test/api.js b/test/api.js index 5cf6520..4ff9d1d 100644 --- a/test/api.js +++ b/test/api.js @@ -174,5 +174,21 @@ vows.describe('api').addBatch({ }, }, }, + 'creating a client on a non-listening port': { + topic: function() { + return new bitcoin.Client(config.host, 9897, 'baduser', 'badpwd'); + }, + 'will return client object': function(client) { + assert.equal(typeof client, 'object'); + }, + 'but when calling a command': { + topic: function(client) { + client.listSinceBlock(this.callback); + }, + 'should not call callback more than once': function(err, result) { + assert.instanceOf(err, Error); + } + } + } }).export(module);