diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index ff7b5d07..882ec795 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -456,7 +456,7 @@ ChainDB.prototype.getEntry = function getEntry(hash, callback) { return utils.nextTick(callback); return this.getHash(hash, function(err, hash) { - if (err && err.type !== 'NotFoundError') + if (err) return callback(err); if (!hash) diff --git a/lib/bcoin/lowlevelup.js b/lib/bcoin/lowlevelup.js index 4ac8086b..a1309267 100644 --- a/lib/bcoin/lowlevelup.js +++ b/lib/bcoin/lowlevelup.js @@ -124,10 +124,8 @@ LowlevelUp.prototype.get = function get(key, options, callback) { return this.binding.get(key, options, function(err, result) { if (err) { - if (err.notFound || /not\s*found/i.test(err.message)) { - err.notFound = true; - err.type = 'NotFoundError'; - } + if (isNotFound(err)) + return callback(); return callback(err); } return callback(null, result); @@ -222,7 +220,7 @@ LowlevelUp.prototype.approximateSize = function approximateSize(start, end, call LowlevelUp.prototype.has = function has(key, callback) { return this.get(key, function(err, value) { - if (err && err.type !== 'NotFoundError') + if (err) return callback(err); return callback(null, value != null); @@ -239,7 +237,7 @@ LowlevelUp.prototype.has = function has(key, callback) { LowlevelUp.prototype.fetch = function fetch(key, parse, callback) { return this.get(key, function(err, value) { - if (err && err.type !== 'NotFoundError') + if (err) return callback(err); if (!value) @@ -265,6 +263,8 @@ LowlevelUp.prototype.iterate = function iterate(options, callback) { var items = []; var iter, opt; + assert(this.loaded, 'Cannot use database before it is loaded.'); + opt = { gte: options.gte, lte: options.lte, @@ -354,7 +354,7 @@ LowlevelUp.prototype.lookup = function lookup(options, callback) { utils.forEachSerial(keys, function(key, next) { self.get(key, function(err, value) { - if (err && err.type !== 'NotFoundError') + if (err) return callback(err); if (!value) @@ -381,6 +381,19 @@ LowlevelUp.prototype.lookup = function lookup(options, callback) { }); }; +/* + * Helpers + */ + +function isNotFound(err) { + if (!err) + return false; + + return err.notFound + || err.type === 'NotFoundError' + || /not\s*found/i.test(err.message); +} + /* * Expose */ diff --git a/lib/bcoin/txdb.js b/lib/bcoin/txdb.js index 0c0f3e2a..58ba1bc4 100644 --- a/lib/bcoin/txdb.js +++ b/lib/bcoin/txdb.js @@ -215,7 +215,7 @@ TXDB.prototype._addOrphan = function _addOrphan(key, hash, index, callback) { var p; this.db.get('o/' + key, function(err, buf) { - if (err && err.type !== 'NotFoundError') + if (err) return callback(err); p = new BufferWriter(); diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index d4db8c8f..7c6290d8 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -431,8 +431,8 @@ WalletDB.prototype.get = function get(id, callback) { } this.db.get('w/' + id, function(err, data) { - if (err && err.type !== 'NotFoundError') - return callback(); + if (err) + return callback(err); if (!data) return callback(); @@ -577,7 +577,7 @@ WalletDB.prototype.getAccount = function getAccount(id, name, callback) { return callback(); self.db.get('a/' + id + '/' + index, function(err, data) { - if (err && err.type !== 'NotFoundError') + if (err) return callback(err); if (!data) @@ -640,8 +640,8 @@ WalletDB.prototype.getAccountIndex = function getAccountIndex(id, name, callback return callback(null, name); this.db.get('i/' + id + '/' + name, function(err, index) { - if (err && err.type !== 'NotFoundError') - return callback(); + if (err) + return callback(err); if (!index) return callback(null, -1);