fix HTTP reponse codes
This commit is contained in:
parent
b4936fd6b4
commit
4e1562ce7a
|
@ -4,30 +4,30 @@
|
||||||
* Module dependencies.
|
* 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) {
|
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) {
|
a.update(function(err) {
|
||||||
if (err && !a.totalReceivedSat) {
|
if (err) return common.handleErrors(err, res, next);
|
||||||
console.log(err);
|
|
||||||
res.status(404).send('Invalid address');
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
req.address = a;
|
req.address = a;
|
||||||
return next();
|
return next();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Show block
|
|
||||||
*/
|
*/
|
||||||
exports.show = function(req, res) {
|
exports.show = function(req, res) {
|
||||||
if (req.address) {
|
if (req.address) {
|
||||||
|
|
|
@ -4,7 +4,8 @@
|
||||||
* Module dependencies.
|
* Module dependencies.
|
||||||
*/
|
*/
|
||||||
var mongoose = require('mongoose'),
|
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) {
|
exports.block = function(req, res, next, hash) {
|
||||||
Block.fromHashWithInfo(hash, function(err, block) {
|
Block.fromHashWithInfo(hash, function(err, block) {
|
||||||
if (err && !block) {
|
if (err || ! tx) return common.handleErrors(err, res, next);
|
||||||
console.log(err);
|
|
||||||
res.status(404).send('Not found');
|
|
||||||
return next();
|
|
||||||
}
|
|
||||||
|
|
||||||
req.block = block.info;
|
req.block = block.info;
|
||||||
return next();
|
return next();
|
||||||
|
|
|
@ -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();
|
||||||
|
}
|
||||||
|
};
|
|
@ -7,6 +7,7 @@ var Transaction = require('../models/Transaction');
|
||||||
var Block = require('../models/Block');
|
var Block = require('../models/Block');
|
||||||
var Address = require('../models/Address');
|
var Address = require('../models/Address');
|
||||||
var async = require('async');
|
var async = require('async');
|
||||||
|
var common = require('./common');
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -14,15 +15,12 @@ var async = require('async');
|
||||||
*/
|
*/
|
||||||
exports.transaction = function(req, res, next, txid) {
|
exports.transaction = function(req, res, next, txid) {
|
||||||
Transaction.fromIdWithInfo(txid, function(err, tx) {
|
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;
|
req.transaction = tx.info;
|
||||||
next();
|
return next();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -19,11 +19,8 @@ function spec() {
|
||||||
this.transactions = [];
|
this.transactions = [];
|
||||||
|
|
||||||
var a = new BitcoreAddress(addrStr);
|
var a = new BitcoreAddress(addrStr);
|
||||||
try {
|
a.validate();
|
||||||
a.validate();
|
this.addrStr = addrStr;
|
||||||
this.addrStr = addrStr;
|
|
||||||
} catch(e){
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
Object.defineProperty(this, 'totalSent', {
|
Object.defineProperty(this, 'totalSent', {
|
||||||
|
@ -58,11 +55,6 @@ function spec() {
|
||||||
}
|
}
|
||||||
|
|
||||||
Address.prototype.update = function(next) {
|
Address.prototype.update = function(next) {
|
||||||
|
|
||||||
if (! this.addrStr) {
|
|
||||||
return next(new Error('Invalid or undefined address string'));
|
|
||||||
}
|
|
||||||
|
|
||||||
var that = this;
|
var that = this;
|
||||||
async.series([
|
async.series([
|
||||||
// TODO TXout!
|
// TODO TXout!
|
||||||
|
|
|
@ -81,16 +81,34 @@ BlockSchema.statics.fromHash = function(hash, cb) {
|
||||||
|
|
||||||
|
|
||||||
BlockSchema.statics.fromHashWithInfo = function(hash, cb) {
|
BlockSchema.statics.fromHashWithInfo = function(hash, cb) {
|
||||||
|
var That = this;
|
||||||
|
|
||||||
this.fromHash(hash, function(err, block) {
|
this.fromHash(hash, function(err, block) {
|
||||||
if (err) return cb(err);
|
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?
|
// TODO: Can we store the rpc instance in the Block object?
|
||||||
BlockSchema.methods.getInfo = function (next) {
|
BlockSchema.methods.getInfo = function (next) {
|
||||||
|
|
||||||
|
@ -98,6 +116,9 @@ BlockSchema.methods.getInfo = function (next) {
|
||||||
var rpc = new RpcClient(config.bitcoind);
|
var rpc = new RpcClient(config.bitcoind);
|
||||||
|
|
||||||
rpc.getBlock(this.hash, function(err, blockInfo) {
|
rpc.getBlock(this.hash, function(err, blockInfo) {
|
||||||
|
// Not found?
|
||||||
|
if (err && err.code === -5) return next();
|
||||||
|
|
||||||
if (err) return next(err);
|
if (err) return next(err);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -73,8 +73,8 @@ TransactionSchema.statics.fromIdWithInfo = function(txid, cb) {
|
||||||
tx.txid = txid;
|
tx.txid = txid;
|
||||||
tx.fillInfo(function(err, txInfo) {
|
tx.fillInfo(function(err, txInfo) {
|
||||||
|
|
||||||
if (!txInfo)
|
if (err) return cb(err);
|
||||||
return cb(new Error('TX not found'));
|
if (!txInfo) return cb();
|
||||||
|
|
||||||
tx.save(function(err) {
|
tx.save(function(err) {
|
||||||
return cb(err,tx);
|
return cb(err,tx);
|
||||||
|
@ -239,6 +239,10 @@ TransactionSchema.statics.queryInfo = function(txid, cb) {
|
||||||
var rpc = new RpcClient(config.bitcoind);
|
var rpc = new RpcClient(config.bitcoind);
|
||||||
|
|
||||||
rpc.getRawTransaction(txid, 1, function(err, txInfo) {
|
rpc.getRawTransaction(txid, 1, function(err, txInfo) {
|
||||||
|
|
||||||
|
// Not found?
|
||||||
|
if (err && err.code === -5) return cb();
|
||||||
|
|
||||||
if (err) return cb(err);
|
if (err) return cb(err);
|
||||||
|
|
||||||
var info = txInfo.result;
|
var info = txInfo.result;
|
||||||
|
|
Loading…
Reference in New Issue