Add an onError handler to getClient options
Signed-off-by: Eric Peterson <ericpeterson@spotify.com>
This commit is contained in:
@@ -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';
|
||||
|
||||
@@ -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
@@ -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
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user