diff --git a/api/controllers/blocks.js b/api/controllers/blocks.js index 4d4dc346..a7278983 100644 --- a/api/controllers/blocks.js +++ b/api/controllers/blocks.js @@ -5,6 +5,8 @@ var _ = bitcore.deps._; var $ = bitcore.util.preconditions; var Block = bitcore.Block; +var BitcoreNode = require('../../'); + var Blocks = {}; var node; @@ -21,14 +23,14 @@ Blocks.setNode = function(aNode) { * Finds a block by its hash */ Blocks.blockHashParam = function(req, res, next, blockHash) { - var block = node.getBlock(blockHash); - - if (_.isUndefined(block)) { - res.status(404).send('Block with id ' + blockHash + ' not found'); - return; - } - req.block = block; - next(); + node.getBlock(blockHash) + .then(function(block) { + req.block = block; + }) + .then(next) + .catch(BitcoreNode.errors.Blocks.NotFound, function() { + res.status(404).send('Block with id ' + blockHash + ' not found'); + }); }; /* @@ -36,14 +38,14 @@ Blocks.blockHashParam = function(req, res, next, blockHash) { */ Blocks.heightParam = function(req, res, next, height) { height = parseInt(height); - var block = node.getBlock(height); - - if (_.isUndefined(block)) { - res.status(404).send('Block with height ' + height + ' not found'); - return; - } - req.block = block; - next(); + node.getBlock(height) + .then(function(block) { + req.block = block; + }) + .then(next) + .catch(BitcoreNode.errors.Blocks.NotFound, function() { + res.status(404).send('Block with height ' + height + ' not found'); + }); }; diff --git a/api/test/v1/blocks.js b/api/test/v1/blocks.js index cd609d5f..110720e2 100644 --- a/api/test/v1/blocks.js +++ b/api/test/v1/blocks.js @@ -5,8 +5,13 @@ var should = chai.should(); var request = require('supertest'); var EventEmitter = require('eventemitter2').EventEmitter2; +var Promise = require('bluebird'); +Promise.longStackTraces(); +var bitcore = require('bitcore'); +var _ = bitcore.deps._; var BitcoreHTTP = require('../../lib/http'); +var BitcoreNode = require('../../../'); var mockBlocks = require('../data/blocks'); describe('BitcoreHTTP v1 blocks routes', function() { @@ -18,11 +23,18 @@ describe('BitcoreHTTP v1 blocks routes', function() { beforeEach(function() { nodeMock = new EventEmitter(); nodeMock.getBlock = function(blockHash) { + var block; if (typeof blockHash === 'number') { var height = blockHash; - return mockBlocks[Object.keys(mockBlocks)[height]]; + block = mockBlocks[Object.keys(mockBlocks)[height]]; + } else { + block = mockBlocks[blockHash]; } - return mockBlocks[blockHash]; + if (_.isUndefined(block)) { + return Promise.reject(new BitcoreNode.errors.Blocks.NotFound(blockHash)); + } + return Promise.resolve(block); + }; nodeMock.getLatestBlock = function() { return mockBlocks[Object.keys(mockBlocks).splice(-1)[0]]; diff --git a/lib/errors.js b/lib/errors.js new file mode 100644 index 00000000..fb293fb3 --- /dev/null +++ b/lib/errors.js @@ -0,0 +1,26 @@ +'use strict'; + +var spec = { + name: 'BitcoreNode', + message: 'Internal Error on BitcoreNode', + errors: [{ + name: 'Transactions', + message: 'Internal Transactions error on BitcoreNode', + errors: [{ + name: 'NotFound', + message: 'Transaction {0} not found' + }, { + name: 'CantBroadcast', + message: 'Unable to broadcast transaction {0}' + }] + }, { + name: 'Blocks', + message: 'Internal Blocks error on BitcoreNode', + errors: [{ + name: 'NotFound', + message: 'Block {0} not found' + }] + }] +}; + +module.exports = require('bitcore').errors.extend(spec);