fix HTTP reponse codes

This commit is contained in:
Matias Alejo Garcia 2014-01-20 15:51:23 -03:00
parent b4936fd6b4
commit 4e1562ce7a
7 changed files with 73 additions and 43 deletions

View File

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

View File

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

18
app/controllers/common.js Normal file
View File

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

View File

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

View File

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

View File

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

View File

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