Skip to content

Commit

Permalink
fix: update keyv events errors handling (#1249)
Browse files Browse the repository at this point in the history
* fix: update error handling in Keyv class to respect emitErrors option

* fix: standardize indentation

* fix: fixed linting

* fix: remove whitespaces

* updating args to arguments_

* adding in tests on set handling

* tests on error handling for store delete

* adding in error handler for store clear

* tests on store has using get method

* test to not emit errors when emitErrors is false

---------

Co-authored-by: Jared Wray <me@jaredwray.com>
  • Loading branch information
Sadzurami and jaredwray authored Dec 12, 2024
1 parent e927799 commit fb9a5c5
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
12 changes: 10 additions & 2 deletions packages/keyv/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ export class Keyv<GenericValue = any> extends EventManager {
throw new Error('Invalid storage adapter');
}

if (typeof this._store.on === 'function' && this.opts.emitErrors) {
if (typeof this._store.on === 'function') {
this._store.on('error', (error: any) => this.emit('error', error));
}

Expand Down Expand Up @@ -220,7 +220,7 @@ export class Keyv<GenericValue = any> extends EventManager {
this._store = store;
this.opts.store = store;

if (typeof store.on === 'function' && this.opts.emitErrors) {
if (typeof store.on === 'function') {
store.on('error', (error: any) => this.emit('error', error));
}

Expand Down Expand Up @@ -668,6 +668,14 @@ export class Keyv<GenericValue = any> extends EventManager {
}
}

public emit(event: string, ...arguments_: any[]): void {
if (event === 'error' && !this.opts.emitErrors) {
return;
}

super.emit(event, ...arguments_);
}

public async serializeData<T>(data: DeserializedData<T>): Promise<string | DeserializedData<T>> {
if (!this._serialize) {
return data;
Expand Down
61 changes: 61 additions & 0 deletions packages/keyv/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -833,3 +833,64 @@ test.it('should emit error if set fails', async t => {
t.expect(result).toBe(false);
t.expect(errorHandler).toHaveBeenCalledWith(new Error('store set error'));
});

test.it('should return when value equals non boolean', async t => {
const store = new Map();
// @ts-expect-error
store.set = () => 'foo';
const keyv = new Keyv(store);
const result = await keyv.set('foo111', 'bar111');
t.expect(result).toBe(true);
});

test.it('should return store set value equals non boolean', async t => {
const store = new Map();
// @ts-expect-error
store.set = () => true;
const keyv = new Keyv(store);
const result = await keyv.set('foo1112', 'bar1112');
t.expect(result).toBe(true);
});

test.it('should handle error on store delete', async t => {
const store = new Map();
store.delete = test.vi.fn().mockRejectedValue(new Error('store delete error'));
const keyv = new Keyv(store);
const errorHandler = test.vi.fn();
keyv.on('error', errorHandler);
const result = await keyv.delete('foo55');
t.expect(result).toBe(false);
t.expect(errorHandler).toHaveBeenCalledWith(new Error('store delete error'));
});

test.it('should handle error on store clear', async t => {
const store = new Map();
store.clear = test.vi.fn().mockRejectedValue(new Error('store clear error'));
const keyv = new Keyv(store);
const errorHandler = test.vi.fn();
keyv.on('error', errorHandler);
await keyv.clear();
t.expect(errorHandler).toHaveBeenCalledWith(new Error('store clear error'));
});

test.it('should handle error on store has / get', async t => {
const store = new Map();
store.get = test.vi.fn().mockRejectedValue(new Error('store has error'));
const keyv = new Keyv(store);
const errorHandler = test.vi.fn();
keyv.on('error', errorHandler);
const result = await keyv.has('foo');
t.expect(result).toBe(false);
t.expect(errorHandler).toHaveBeenCalledWith(new Error('store has error'));
});

test.it('should not emit error with opts.emitErrors false', async t => {
const store = new Map();
store.set = test.vi.fn().mockRejectedValue(new Error('store set error'));
const keyv = new Keyv({store, emitErrors: false});
const errorHandler = test.vi.fn();
keyv.on('error', errorHandler);
const result = await keyv.set('foo', 'bar');
t.expect(result).toBe(false);
t.expect(errorHandler).not.toHaveBeenCalled();
});

0 comments on commit fb9a5c5

Please sign in to comment.