test error handling in fetching process
This commit is contained in:
parent
aa415268fc
commit
8d6d545139
|
@ -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);
|
||||
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue