From 4e1562ce7a62ca78d4a9910741d5f21e3d264c58 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 20 Jan 2014 15:51:23 -0300 Subject: [PATCH] fix HTTP reponse codes --- app/controllers/addresses.js | 28 ++++++++++++++-------------- app/controllers/blocks.js | 9 +++------ app/controllers/common.js | 18 ++++++++++++++++++ app/controllers/transactions.js | 12 +++++------- app/models/Address.js | 12 ++---------- app/models/Block.js | 29 +++++++++++++++++++++++++---- app/models/Transaction.js | 8 ++++++-- 7 files changed, 73 insertions(+), 43 deletions(-) create mode 100644 app/controllers/common.js diff --git a/app/controllers/addresses.js b/app/controllers/addresses.js index e13000c..f12087e 100644 --- a/app/controllers/addresses.js +++ b/app/controllers/addresses.js @@ -4,30 +4,30 @@ * Module dependencies. */ -var Address = require('../models/Address'); +var Address = require('../models/Address'), + common = require('./common'); -/** - * Find block by hash ... - */ exports.address = function(req, res, next, addr) { - var a = Address.new(addr); + + + var a; + try { + a = Address.new(addr); + } catch (e) { + return common.handleErrors({message: 'Invalid address:' + e.message, code: 1}, res, next); + } a.update(function(err) { - if (err && !a.totalReceivedSat) { - console.log(err); - res.status(404).send('Invalid address'); - return next(); - } + if (err) return common.handleErrors(err, res, next); - req.address = a; - return next(); - }); + req.address = a; + return next(); + }); }; /** - * Show block */ exports.show = function(req, res) { if (req.address) { diff --git a/app/controllers/blocks.js b/app/controllers/blocks.js index a21d52e..9393359 100644 --- a/app/controllers/blocks.js +++ b/app/controllers/blocks.js @@ -4,7 +4,8 @@ * Module dependencies. */ var mongoose = require('mongoose'), - Block = mongoose.model('Block'); + Block = mongoose.model('Block'), + common = require('./common'); /** @@ -12,11 +13,7 @@ var mongoose = require('mongoose'), */ exports.block = function(req, res, next, hash) { Block.fromHashWithInfo(hash, function(err, block) { - if (err && !block) { - console.log(err); - res.status(404).send('Not found'); - return next(); - } + if (err || ! tx) return common.handleErrors(err, res, next); req.block = block.info; return next(); diff --git a/app/controllers/common.js b/app/controllers/common.js new file mode 100644 index 0000000..faae67c --- /dev/null +++ b/app/controllers/common.js @@ -0,0 +1,18 @@ +'use strict'; + + +exports.handleErrors = function (err, res, next) { + if (err) { + if (err.code) { + res.status(400).send(err.message + '. Code:' + err.code); + } + else { + res.status(503).send(err.message); + } + return next(); + } + else { + res.status(404).send('Not found'); + return next(); + } +}; diff --git a/app/controllers/transactions.js b/app/controllers/transactions.js index 833fc66..c865f57 100644 --- a/app/controllers/transactions.js +++ b/app/controllers/transactions.js @@ -7,6 +7,7 @@ var Transaction = require('../models/Transaction'); var Block = require('../models/Block'); var Address = require('../models/Address'); var async = require('async'); +var common = require('./common'); /** @@ -14,15 +15,12 @@ var async = require('async'); */ exports.transaction = function(req, res, next, txid) { Transaction.fromIdWithInfo(txid, function(err, tx) { - if (err) { - console.log(err); - res.status(404).send('Not found'); - return next(); - } - if (!tx) return next(new Error('Failed to load TX ' + txid)); + if (err || ! tx) return common.handleErrors(err, res, next); + + req.transaction = tx.info; - next(); + return next(); }); }; diff --git a/app/models/Address.js b/app/models/Address.js index 05d1a78..e054ff1 100644 --- a/app/models/Address.js +++ b/app/models/Address.js @@ -19,11 +19,8 @@ function spec() { this.transactions = []; var a = new BitcoreAddress(addrStr); - try { - a.validate(); - this.addrStr = addrStr; - } catch(e){ - } + a.validate(); + this.addrStr = addrStr; Object.defineProperty(this, 'totalSent', { @@ -58,11 +55,6 @@ function spec() { } Address.prototype.update = function(next) { - - if (! this.addrStr) { - return next(new Error('Invalid or undefined address string')); - } - var that = this; async.series([ // TODO TXout! diff --git a/app/models/Block.js b/app/models/Block.js index e4d84d3..8669759 100644 --- a/app/models/Block.js +++ b/app/models/Block.js @@ -81,16 +81,34 @@ BlockSchema.statics.fromHash = function(hash, cb) { BlockSchema.statics.fromHashWithInfo = function(hash, cb) { + var That = this; + this.fromHash(hash, function(err, block) { if (err) return cb(err); - if (!block) { return cb(new Error('Block not found')); } - block.getInfo(function(err) { return cb(err,block); } ); + if (!block) { + // No in mongo...but maybe in bitcoind... lets query it + block = new That(); + + block.hash = hash; + block.getInfo(function(err, blockInfo) { +console.log('[Block.js.95:err:]',err); //TODO + if (err) return cb(err); + if (!blockInfo) return cb(); + + block.save(function(err) { + return cb(err,block); + }); + }); + } + else { + block.getInfo(function(err) { + return cb(err,block); + }); + } }); }; - - // TODO: Can we store the rpc instance in the Block object? BlockSchema.methods.getInfo = function (next) { @@ -98,6 +116,9 @@ BlockSchema.methods.getInfo = function (next) { var rpc = new RpcClient(config.bitcoind); rpc.getBlock(this.hash, function(err, blockInfo) { + // Not found? + if (err && err.code === -5) return next(); + if (err) return next(err); /* diff --git a/app/models/Transaction.js b/app/models/Transaction.js index fdad11e..291c8ba 100644 --- a/app/models/Transaction.js +++ b/app/models/Transaction.js @@ -73,8 +73,8 @@ TransactionSchema.statics.fromIdWithInfo = function(txid, cb) { tx.txid = txid; tx.fillInfo(function(err, txInfo) { - if (!txInfo) - return cb(new Error('TX not found')); + if (err) return cb(err); + if (!txInfo) return cb(); tx.save(function(err) { return cb(err,tx); @@ -239,6 +239,10 @@ TransactionSchema.statics.queryInfo = function(txid, cb) { var rpc = new RpcClient(config.bitcoind); rpc.getRawTransaction(txid, 1, function(err, txInfo) { + + // Not found? + if (err && err.code === -5) return cb(); + if (err) return cb(err); var info = txInfo.result;