From 8d6d545139c167491b582025287b927ed8020347 Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 11 Jan 2016 16:36:51 -0300 Subject: [PATCH] test error handling in fetching process --- lib/fiatrateservice.js | 9 ++++---- test/integration/fiatrateservice.js | 34 +++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/lib/fiatrateservice.js b/lib/fiatrateservice.js index 9048d85..7cc6e52 100644 --- a/lib/fiatrateservice.js +++ b/lib/fiatrateservice.js @@ -68,11 +68,13 @@ FiatRateService.prototype._fetch = function(cb) { async.each(self.providers, function(provider, next) { self._retrieve(provider, function(err, res) { if (err) { - log.warn(err); + log.warn('Error retrieving data for ' + provider.name, err); return next(); } self.storage.storeFiatRate(provider.name, res, function(err) { - if (err) log.warn(err); + if (err) { + log.warn('Error storing data for ' + provider.name, err); + } return next(); }); }); @@ -88,14 +90,13 @@ FiatRateService.prototype._retrieve = function(provider, cb) { json: true, }, function(err, res, body) { if (err || !body) { - log.warn('Error fetching data for ' + provider.name, err); return cb(err); } log.debug('Data for ' + provider.name + ' fetched successfully'); if (!provider.parseFn) { - return cb('No parse function for provider ' + provider.name); + return cb(new Error('No parse function for provider ' + provider.name)); } var rates = provider.parseFn(body); diff --git a/test/integration/fiatrateservice.js b/test/integration/fiatrateservice.js index 53b92a9..98420f0 100644 --- a/test/integration/fiatrateservice.js +++ b/test/integration/fiatrateservice.js @@ -209,5 +209,39 @@ describe('Fiat rate service', function() { }); }); }); + + it('should not stop when failing to fetch provider', function(done) { + var clock = sinon.useFakeTimers(100, 'Date'); + var bitstamp = { + last: 120.00, + }; + request.get.withArgs({ + url: 'https://bitpay.com/api/rates/', + json: true + }).yields('dummy error', null, null); + request.get.withArgs({ + url: 'https://www.bitstamp.net/api/ticker/', + json: true + }).yields(null, null, bitstamp); + + service._fetch(function(err) { + should.not.exist(err); + service.getRate('USD', {}, function(err, res) { + should.not.exist(err); + res.ts.should.equal(100); + should.not.exist(res.rate) + should.not.exist(res.fetchedOn) + service.getRate('USD', { + provider: 'Bitstamp' + }, function(err, res) { + should.not.exist(err); + res.fetchedOn.should.equal(100); + res.rate.should.equal(120.00); + clock.restore(); + done(); + }); + }); + }); + }); }); });