Skip to content

Commit

Permalink
Merge pull request #58 from bbc/stats-errors
Browse files Browse the repository at this point in the history
Task: send ceych.errors stats when the cache returns an error
  • Loading branch information
Nick Spragg authored Aug 8, 2017
2 parents 0bc6001 + 62c3314 commit 3520b03
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
11 changes: 10 additions & 1 deletion lib/memoize.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@ function registerCallback(promise, cb) {
}

module.exports = (cacheClient, cacheOpts, fn) => {
const statsClient = cacheOpts.statsClient;

function setInCache(key, ttl) {
return (returnValues) => {
return cacheClient
.setAsync(key, returnValues, ttl * 1000)
.catch((err) => {
statsClient ? statsClient.increment('ceych.errors') : _.noop();
return Promise.reject(err);
})
.then(() => returnValues);
};
}
Expand All @@ -64,7 +70,10 @@ module.exports = (cacheClient, cacheOpts, fn) => {
}

const reply = cacheClient.getAsync(cacheKey)
.catch((err) => Promise.reject(err))
.catch((err) => {
statsClient ? statsClient.increment('ceych.errors') : _.noop();
return Promise.reject(err);
})
.then((cached) => {
if (cached) {
statsClient ? statsClient.increment('ceych.hits') : _.noop();
Expand Down
30 changes: 26 additions & 4 deletions test/lib/memoize.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ describe('memoize', () => {
const func = memoize(cacheClient, opts, wrappableWithObject);

return func({
testing: '123'
})
testing: '123'
})
.catch(assert.ifError)
.then(() => {
sinon.assert.calledWith(hash.create, `${wrappableWithObject.toString()}[{"testing":"123"}]`);
Expand Down Expand Up @@ -234,8 +234,8 @@ describe('memoize', () => {
const func = memoize(cacheClient, opts, wrappableStub);

return func(1, {
two: 'three'
}, [4], 'five')
two: 'three'
}, [4], 'five')
.catch(assert.ifError)
.then(() => {
sinon.assert.calledWith(wrappableStub, 1, sinon.match({
Expand Down Expand Up @@ -410,5 +410,27 @@ describe('memoize', () => {
done();
});
});

it('increments a StatsD counter when fetching from the cache returns an error', (done) => {
cacheClient.getAsync.returns(Promise.reject(new Error('error')));
const func = memoize(cacheClient, optsWithStats, wrappableWithCb);

func((err) => {
assert.ok(err);
sinon.assert.calledWith(statsClient.increment, 'ceych.errors');
done();
});
});

it('increments a StatsD counter when saving to the cache returns an error', (done) => {
cacheClient.setAsync.returns(Promise.reject(new Error('error')));
const func = memoize(cacheClient, optsWithStats, wrappableWithCb);

func((err) => {
assert.ok(err);
sinon.assert.calledWith(statsClient.increment, 'ceych.errors');
done();
});
});
});
});

0 comments on commit 3520b03

Please sign in to comment.