feat: Add error handling for inMemoryCache

This commit is contained in:
Bouncey
2018-12-02 13:38:03 +00:00
committed by mrugesh mohapatra
parent 09cb38aa21
commit 15a9992603
3 changed files with 84 additions and 26 deletions

View File

@ -2,21 +2,32 @@ function isPromiseLike(thing) {
return !!thing && typeof thing.then === 'function';
}
function InMemoryCache(initialValue) {
function InMemoryCache(initialValue, reportError) {
if (typeof reportError !== 'function') {
throw new Error(
'No reportError function specified for this in-memory-cache'
);
}
const cacheKey = Symbol('cacheKey');
const cache = new Map();
if (typeof initialValue !== 'undefined') {
cache.set(cacheKey, initialValue);
}
cache.set(cacheKey, initialValue);
return {
get() {
const value = cache.get(cacheKey);
return typeof value !== 'undefined' ? value : null;
},
async update(fn) {
const maybePromisedValue = fn();
let maybePromisedValue;
try {
maybePromisedValue = fn();
} catch (e) {
const errMsg = `InMemoryCache > update > caught: ${e.message}`;
e.message = errMsg;
reportError(e);
return null;
}
if (isPromiseLike(maybePromisedValue)) {
return maybePromisedValue.then(value => cache.set(cacheKey, value));
} else {
@ -25,6 +36,7 @@ function InMemoryCache(initialValue) {
return null;
}
},
clear() {
return cache.delete(cacheKey);
}

View File

@ -1,33 +1,40 @@
/* global describe expect it beforeEach */
/* global describe expect it */
import inMemoryCache from './in-memory-cache';
import sinon from 'sinon';
describe('InMemoryCache', () => {
let reportErrorStub;
const theAnswer = 42;
const before = 'before';
const after = 'after';
const emptyCacheValue = null;
describe('get', () => {
it('returns null for an empty cache', () => {
const cache = inMemoryCache();
expect(cache.get()).toBe(emptyCacheValue);
});
beforeEach(() => {
reportErrorStub = sinon.spy();
});
it('throws if no report function is passed as a second argument', () => {
expect(() => inMemoryCache(null)).toThrowError(
'No reportError function specified for this in-memory-cache'
);
});
describe('get', () => {
it('returns an initial value', () => {
const cache = inMemoryCache(theAnswer);
const cache = inMemoryCache(theAnswer, reportErrorStub);
expect(cache.get()).toBe(theAnswer);
});
});
describe('update', () => {
it('updates the cached value', () => {
const cache = inMemoryCache(before);
const cache = inMemoryCache(before, reportErrorStub);
cache.update(() => after);
expect(cache.get()).toBe(after);
});
it('can handle promises correctly', done => {
const cache = inMemoryCache(before);
const cache = inMemoryCache(before, reportErrorStub);
cache.update(() => new Promise(resolve => resolve(after)));
// because async
setImmediate(() => {
@ -35,12 +42,25 @@ describe('InMemoryCache', () => {
done();
});
});
it('reports errors thrown from the update function', () => {
const reportErrorStub = sinon.spy();
const cache = inMemoryCache(before, reportErrorStub);
const updateError = new Error('An update error');
const updateThatThrows = () => {
throw updateError;
};
cache.update(updateThatThrows);
expect(reportErrorStub.calledWith(updateError)).toBe(true);
});
});
describe('clear', () => {
it('clears the cache', () => {
expect.assertions(2);
const cache = inMemoryCache(theAnswer);
const cache = inMemoryCache(theAnswer, reportErrorStub);
expect(cache.get()).toBe(theAnswer);
cache.clear();
expect(cache.get()).toBe(emptyCacheValue);