Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

Commit

Permalink
Return error on getStats if Redis client is undefined
Browse files Browse the repository at this point in the history
Currently when calling getStats, if the first sync call has not been
made yet, an 'cannot get value of undefined' error will be thrown.

This adds an error message explaining that the client is not
initialised and that an initial sync is required.
  • Loading branch information
Aiden Keating committed Aug 16, 2017
1 parent f9bb215 commit f365e57
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
43 changes: 24 additions & 19 deletions lib/sync-metrics.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,28 +148,33 @@ var getStats = function(cb) {
return input.toFixed(2) + 'ms';
}
}];
async.map(metricsToFetch, function(metric, callback) {
var metricName = metric.metricName;
redisClient.lrange([statsNamespace, metricName].join(':'), 0, MAX_NUMBER, function(err, data) {
if (redisClient) {
async.map(metricsToFetch, function(metric, callback) {
var metricName = metric.metricName;
redisClient.lrange([statsNamespace, metricName].join(':'), 0, MAX_NUMBER, function(err, data) {
if (err) {
debugError('Failed to get values from redis for key %s with error : %s', metricName, err);
return callback();
}
var stats = aggregateData(metric, data);
stats.name = metric.displayName;
return callback(null, stats)
});
}, function(err, results) {
if (err) {
debugError('Failed to get values from redis for key %s with error : %s', metricName, err);
return callback();
return cb(err);
}
var stats = aggregateData(metric, data);
stats.name = metric.displayName;
return callback(null, stats)
var reduced = _.reduce(results, function(sofar, result) {
sofar[result.name] = result;
delete result.name;
return sofar;
}, {});
return cb(null, reduced);
});
}, function(err, results) {
if (err) {
return cb(err);
}
var reduced = _.reduce(results, function(sofar, result) {
sofar[result.name] = result;
delete result.name;
return sofar;
}, {});
return cb(null, reduced);
});
} else {
var redisClientError = new Error('Redis client is not initialised. An initial sync may not have been performed yet.');
return cb(redisClientError);
}
};

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fh-sync",
"version": "1.0.9",
"version": "1.0.10",
"description": "FeedHenry Data Synchronization Server",
"main": "index.js",
"dependencies": {
Expand Down

0 comments on commit f365e57

Please sign in to comment.