diff --git a/lib/database/leveluparrayadapter.js b/lib/database/leveluparrayadapter.js index 28fc120f4e..ba12e38b1c 100644 --- a/lib/database/leveluparrayadapter.js +++ b/lib/database/leveluparrayadapter.js @@ -1,4 +1,5 @@ var Sublevel = require("level-sublevel"); +const { LevelUpOutOfRangeError, BlockOutOfRangeError } = require("../utils/errorhelper"); // Level up adapter that looks like an array. Doesn't support inserts. @@ -58,11 +59,11 @@ LevelUpArrayAdapter.prototype.get = function(index, callback) { return callback(err); } if (index >= length) { - return callback( - new Error( - "LevelUpArrayAdapter named '" + self.name + "' index out of range: index " + index + "; length: " + length - ) - ); + // index out of range + let RangeError = self.name === "blocks" + ? new BlockOutOfRangeError(index, length) + : new LevelUpOutOfRangeError(self.name, index, length); + return callback(RangeError); } self._get(index, callback); }); diff --git a/lib/statemanager.js b/lib/statemanager.js index 72ec29f0f5..906c156f59 100644 --- a/lib/statemanager.js +++ b/lib/statemanager.js @@ -12,6 +12,7 @@ var ForkedBlockchain = require("./utils/forkedblockchain.js"); var Web3 = require("web3"); var sigUtil = require("eth-sig-util"); var _ = require("lodash"); +const { BlockOutOfRangeError } = require("./utils/errorhelper"); var to = require("./utils/to"); var random = require("./utils/random"); @@ -627,7 +628,7 @@ StateManager.prototype.processCall = function(from, rawTx, blockNumber, callback self.blockchain.processCall(tx, blockNumber, function(err, results) { if (err) { - if (err.message.indexOf("LevelUpArrayAdapter named 'blocks' index out of range: index") >= 0) { + if (err instanceof BlockOutOfRangeError) { // block doesn't exist return callback(null, null); } diff --git a/lib/subproviders/geth_api_double.js b/lib/subproviders/geth_api_double.js index 67335d3c32..a906a6bca9 100644 --- a/lib/subproviders/geth_api_double.js +++ b/lib/subproviders/geth_api_double.js @@ -5,6 +5,7 @@ var to = require("../utils/to"); var txhelper = require("../utils/txhelper"); var blockHelper = require("../utils/block_helper"); var pkg = require("../../package.json"); +const { BlockOutOfRangeError } = require("../utils/errorhelper"); var Subprovider = require("web3-provider-engine/subproviders/subprovider.js"); @@ -188,7 +189,7 @@ GethApiDouble.prototype.eth_getBlockByHash = function(txHash, includeFullTransac GethApiDouble.prototype.eth_getBlockTransactionCountByNumber = function(blockNumber, callback) { this.state.blockchain.getBlock(blockNumber, function(err, block) { if (err) { - if (err.message.indexOf("LevelUpArrayAdapter named 'blocks' index out of range: index") >= 0) { + if (err instanceof BlockOutOfRangeError) { // block doesn't exist return callback(null, 0); } diff --git a/lib/utils/errorhelper.js b/lib/utils/errorhelper.js new file mode 100644 index 0000000000..b52f2e5e9b --- /dev/null +++ b/lib/utils/errorhelper.js @@ -0,0 +1,20 @@ +class LevelUpOutOfRangeError extends Error { + constructor(type, index, len) { + const message = "LevelUpArrayAdapter named '" + type + "' index out of range: index " + index + "; length: " + len; + super(message); + this.message = message; + this.name = `${this.constructor.name}:${type}`; + this.type = type; + } +} + +class BlockOutOfRangeError extends LevelUpOutOfRangeError { + constructor(index, len) { + super("blocks", index, len); + } +} + +module.exports = { + LevelUpOutOfRangeError, + BlockOutOfRangeError +};