Add an onError handler to getClient options

Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
Eric Peterson
2021-05-13 17:38:47 +02:00
parent 96e3be74c2
commit 3839c2d9bc
3 changed files with 23 additions and 0 deletions
+11
View File
@@ -86,6 +86,17 @@ describe('CacheManager', () => {
expect(client).toHaveBeenCalledTimes(1);
});
it('attaches error handler to client', () => {
const pluginId = 'error-test';
const handler = jest.fn();
manager.forPlugin(pluginId).getClient({ onError: handler });
const client = DefaultCacheClient as jest.Mock;
const mockCalls = client.mock.calls.splice(-1);
const realClient = mockCalls[0][0].client as Keyv;
expect(realClient.on).toHaveBeenCalledWith('error', handler);
});
it('provides different plugins different cache clients', async () => {
const plugin1Id = 'test1';
const plugin2Id = 'test2';
+6
View File
@@ -73,6 +73,12 @@ export class CacheManager {
return {
getClient: (opts = {}): CacheClient => {
const concreteClient = this.getClientWithTtl(pluginId, opts.defaultTtl);
// Attach error handler if provided.
if (typeof opts?.onError === 'function') {
concreteClient.on('error', opts.onError);
}
return new DefaultCacheClient({
client: concreteClient,
});
+6
View File
@@ -23,6 +23,12 @@ type ClientOptions = {
* can be configured per entry at set-time).
*/
defaultTtl?: number;
/**
* An optional handler for connection errors emitted from the underlying data
* store.
*/
onError?: (err: Error) => void
};
/**