From 62c33147a1f69e1bba727e4ce8436c70e3a15948 Mon Sep 17 00:00:00 2001 From: Cem Staveley Date: Thu, 3 Aug 2017 15:06:37 +0100 Subject: [PATCH] Task: send ceych.errors stats when communicating with the cache results in an error --- lib/memoize.js | 11 ++++++++++- test/lib/memoize.js | 30 ++++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/lib/memoize.js b/lib/memoize.js index 9d750b8..7686fcb 100644 --- a/lib/memoize.js +++ b/lib/memoize.js @@ -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); }; } @@ -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(); diff --git a/test/lib/memoize.js b/test/lib/memoize.js index 8df1912..b53f0dc 100644 --- a/test/lib/memoize.js +++ b/test/lib/memoize.js @@ -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"}]`); @@ -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({ @@ -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(); + }); + }); }); });