From f619192708be1464d67dca565e8d88ae8793b5b2 Mon Sep 17 00:00:00 2001 From: Matias Alejo Garcia Date: Mon, 20 Jan 2014 16:11:35 -0300 Subject: [PATCH] better error API handling --- app/controllers/blocks.js | 2 +- app/models/Block.js | 1 - public/js/controllers/address.js | 12 ++++++++++-- public/js/controllers/blocks.js | 12 ++++++++++-- public/js/controllers/transactions.js | 12 ++++++++++-- 5 files changed, 31 insertions(+), 8 deletions(-) diff --git a/app/controllers/blocks.js b/app/controllers/blocks.js index 9393359..316f17f 100644 --- a/app/controllers/blocks.js +++ b/app/controllers/blocks.js @@ -13,7 +13,7 @@ var mongoose = require('mongoose'), */ exports.block = function(req, res, next, hash) { Block.fromHashWithInfo(hash, function(err, block) { - if (err || ! tx) return common.handleErrors(err, res, next); + if (err || ! block) return common.handleErrors(err, res, next); req.block = block.info; return next(); diff --git a/app/models/Block.js b/app/models/Block.js index 8669759..3f4bcbc 100644 --- a/app/models/Block.js +++ b/app/models/Block.js @@ -92,7 +92,6 @@ BlockSchema.statics.fromHashWithInfo = function(hash, cb) { 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(); diff --git a/public/js/controllers/address.js b/public/js/controllers/address.js index 46a441d..b53ec85 100644 --- a/public/js/controllers/address.js +++ b/public/js/controllers/address.js @@ -8,8 +8,16 @@ angular.module('insight.address').controller('AddressController', ['$scope', '$r addrStr: $routeParams.addrStr }, function(address) { $scope.address = address; - }, function() { - $rootScope.flashMessage = 'Address Not Found'; + }, function(e) { + if (e.status === 400) { + $rootScope.flashMessage = 'Invalid Address: ' + $routeParams.addrStr; + } + else if (e.status === 503) { + $rootScope.flashMessage = 'Backend Error. ' + e.data; + } + else { + $rootScope.flashMessage = 'Address Not Found'; + } $location.path('/'); }); }; diff --git a/public/js/controllers/blocks.js b/public/js/controllers/blocks.js index b398ef3..e300299 100644 --- a/public/js/controllers/blocks.js +++ b/public/js/controllers/blocks.js @@ -17,8 +17,16 @@ angular.module('insight.blocks').controller('BlocksController', ['$scope', '$roo blockHash: $routeParams.blockHash }, function(block) { $scope.block = block; - }, function() { - $rootScope.flashMessage = 'Block Not Found'; + }, function(e) { + if (e.status === 400) { + $rootScope.flashMessage = 'Invalid Transaction ID: ' + $routeParams.txId; + } + else if (e.status === 503) { + $rootScope.flashMessage = 'Backend Error. ' + e.data; + } + else { + $rootScope.flashMessage = 'Block Not Found'; + } $location.path('/'); }); }; diff --git a/public/js/controllers/transactions.js b/public/js/controllers/transactions.js index e1272e8..6e47dc3 100644 --- a/public/js/controllers/transactions.js +++ b/public/js/controllers/transactions.js @@ -8,8 +8,16 @@ angular.module('insight.transactions').controller('transactionsController', ['$s txId: $routeParams.txId }, function(tx) { $scope.tx = tx; - }, function() { - $rootScope.flashMessage = 'Transaction Not Found'; + }, function(e) { + if (e.status === 400) { + $rootScope.flashMessage = 'Invalid Transaction ID: ' + $routeParams.txId; + } + else if (e.status === 503) { + $rootScope.flashMessage = 'Backend Error. ' + e.data; + } + else { + $rootScope.flashMessage = 'Transaction Not Found'; + } $location.path('/'); }); };