From 44746344526e2d8c6af3c8159c2e970683b6d692 Mon Sep 17 00:00:00 2001 From: Andrew Pendleton Date: Thu, 14 Apr 2016 13:32:38 -0400 Subject: [PATCH] Improve error handling for geocoderDataIterator --- lib/mbtiles.js | 14 ++++++++++---- test/geocoder.test.js | 3 ++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/mbtiles.js b/lib/mbtiles.js index 54d4b9e..a7388b8 100644 --- a/lib/mbtiles.js +++ b/lib/mbtiles.js @@ -633,18 +633,24 @@ MBTiles.prototype.geocoderDataIterator = function(type) { while (nextQueue.length && dataQueue.length) { var nextCb = nextQueue.shift(), data, cbValue; if (dataQueue[0] == doneSentinel) { - cbValue = {value: undefined, done: true}; + cbValue = { + err: null, + row: {value: undefined, done: true} + } } else { data = dataQueue.shift(); maybeRefillBuffer(); - cbValue = {value: {shard: data.shard, data: zlib.inflateSync(data.data)}, done: false}; + cbValue = { + err: data.err, + row: {value: {shard: data.row.shard, data: zlib.inflateSync(data.row.data)}, done: false} + }; } // bind the callback and data now so that they don't change before the setImmediate // callback is executed (function(nextCb, cbValue) { setImmediate(function() { - nextCb(cbValue); + nextCb(cbValue.err, cbValue.row); }); })(nextCb, cbValue); } @@ -657,7 +663,7 @@ MBTiles.prototype.geocoderDataIterator = function(type) { refilling = true; var segmentCount = 0; _this._db.each('SELECT shard, data FROM geocoder_data WHERE type = ? ORDER BY shard limit ?,?', type, position, chunkSize, function(err, row) { - dataQueue.push(row); + dataQueue.push({row: row, err: err}); segmentCount += 1; sendIfAvailable(); }, function() { diff --git a/test/geocoder.test.js b/test/geocoder.test.js index d5336a4..49cecca 100644 --- a/test/geocoder.test.js +++ b/test/geocoder.test.js @@ -80,7 +80,8 @@ tape('putGeocoderData', function(assert) { tape('geocoderDataIterator', function(assert) { var it = to.geocoderDataIterator("term"); var data = []; - var n = function(item) { + var n = function(err, item) { + assert.ifError(err); if (item.done) { assert.equal(data.length, 2, "iterator produces two shards");