From e927afc3838b0e82afe5d1fe791a2931bd73001f Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi <1591639+s1na@users.noreply.github.com> Date: Wed, 1 Aug 2018 21:56:33 +0200 Subject: [PATCH 1/2] Fix error propagation in iterator This fixes a minor issue where the iterator, after calling the `run` function, doesn't pass along its error to the callback. --- index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.js b/index.js index 5273453..28be3c5 100644 --- a/index.js +++ b/index.js @@ -830,7 +830,7 @@ Blockchain.prototype._iterator = function (name, func, cb) { async.whilst( () => blockNumber, run, - () => self._saveHeads(cb) + (err) => self._saveHeads(() => cb(err)) ) }) From fcc98b1f9b42efda00000c0c986583227b2caa23 Mon Sep 17 00:00:00 2001 From: Sina Mahmoodi Date: Thu, 2 Aug 2018 11:49:06 +0200 Subject: [PATCH 2/2] Fix _saveHeads cb, add test case Signed-off-by: Sina Mahmoodi --- index.js | 2 +- test/index.js | 11 ++++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 28be3c5..191475b 100644 --- a/index.js +++ b/index.js @@ -830,7 +830,7 @@ Blockchain.prototype._iterator = function (name, func, cb) { async.whilst( () => blockNumber, run, - (err) => self._saveHeads(() => cb(err)) + (err) => err ? cb(err) : self._saveHeads(cb) ) }) diff --git a/test/index.js b/test/index.js index ced9fa9..b15f8d3 100644 --- a/test/index.js +++ b/test/index.js @@ -11,7 +11,7 @@ const testData = require('./testdata.json') const BN = require('bn.js') test('blockchain test', function (t) { - t.plan(59) + t.plan(61) var blockchain = new Blockchain() var genesisBlock var blocks = [] @@ -220,6 +220,15 @@ test('blockchain test', function (t) { done() }) }, + function iterateError (done) { + blockchain.iterator('error', function (block, reorg, cb) { + cb(new Error('iterator func error')) + }, function (err) { + t.ok(err, 'should catch iterator func error') + t.equal(err.message, 'iterator func error', 'should return correct error') + done() + }) + }, function iterateEmpty (done) { var blockchain = new Blockchain() blockchain.validate = false