handle notfound errors at a lower level.

This commit is contained in:
Christopher Jeffrey 2016-06-30 22:37:05 -07:00
parent c6af30e69d
commit bd56b46806
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD
4 changed files with 27 additions and 14 deletions

View File

@ -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)

View File

@ -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
*/

View File

@ -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();

View File

@ -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);