Skip to content

Commit

Permalink
Merge pull request #936 from mathulbrich/add-suppress-errors-option
Browse files Browse the repository at this point in the history
feat: add suppress errors option
  • Loading branch information
kamilmysliwiec committed Aug 16, 2023
2 parents 848d0aa + e322cb8 commit 5716f71
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/event-subscribers.loader.ts
Expand Up @@ -95,7 +95,7 @@ export class EventSubscribersLoader
listenerMethod(
event,
(...args: unknown[]) =>
this.wrapFunctionInTryCatchBlocks(instance, methodKey, args),
this.wrapFunctionInTryCatchBlocks(instance, methodKey, args, options),
options,
);
}
Expand Down Expand Up @@ -142,6 +142,7 @@ export class EventSubscribersLoader
contextInstance,
listenerMethodKey,
args,
options,
);
},
options,
Expand Down Expand Up @@ -177,11 +178,16 @@ export class EventSubscribersLoader
instance: Record<string, any>,
methodKey: string,
args: unknown[],
options?: OnEventOptions,
) {
try {
return await instance[methodKey].call(instance, ...args);
} catch (e) {
this.logger.error(e);
if (options?.suppressErrors ?? true) {
this.logger.error(e);
} else {
throw e;
}
}
}
}
7 changes: 7 additions & 0 deletions lib/interfaces/on-event-options.interface.ts
Expand Up @@ -9,4 +9,11 @@ export type OnEventOptions = OnOptions & {
* @default false
*/
prependListener?: boolean;

/**
* If "true", the onEvent callback will not throw an error while handling the event. Otherwise, if "false" it will throw an error.
*
* @default true
*/
suppressErrors?: boolean;
};
34 changes: 34 additions & 0 deletions tests/e2e/module-e2e.spec.ts
Expand Up @@ -133,6 +133,17 @@ describe('EventEmitterModule - e2e', () => {
expect(result).toBeTruthy();
});

it('should be able to gracefully recover when an unexpected error occurs from provider and suppressErrors is true', async () => {
const eventsConsumerRef = app.get(EventsProviderConsumer);
await app.init();

const emitter = app.get(EventEmitter2);
const result = emitter.emit('error-handling-suppressed.provider');

expect(eventsConsumerRef.errorHandlingCalls).toEqual(1);
expect(result).toBeTruthy();
});

it('should be able to gracefully recover when an unexpected error occurs from request scoped', async () => {
await app.init();

Expand All @@ -142,6 +153,29 @@ describe('EventEmitterModule - e2e', () => {
expect(result).toBeTruthy();
});

it('should be able to gracefully recover when an unexpected error occurs from request scoped and suppressErrors is true', async () => {
await app.init();

const eventEmitter = app.get(EventEmitter2);
const result = eventEmitter.emit('error-handling-suppressed.request-scoped');

expect(result).toBeTruthy();
});

it('should throw when an unexpected error occurs from provider and suppressErrors is false', async () => {
await app.init();

const eventEmitter = app.get(EventEmitter2);
expect(eventEmitter.emitAsync('error-throwing.provider')).rejects.toThrow("This is a test error");
});

it('should throw when an unexpected error occurs from request scoped and suppressErrors is false', async () => {
await app.init();

const eventEmitter = app.get(EventEmitter2);
expect(eventEmitter.emitAsync('error-throwing.request-scoped')).rejects.toThrow("This is a test error");
});

afterEach(async () => {
await app.close();
});
Expand Down
11 changes: 11 additions & 0 deletions tests/src/events-provider.consumer.ts
Expand Up @@ -23,4 +23,15 @@ export class EventsProviderConsumer {
this.errorHandlingCalls++;
throw new Error('This is a test error');
}

@OnEvent('error-handling-suppressed.provider', { suppressErrors: true })
onErrorHandlingSuppressedEvent() {
this.errorHandlingCalls++;
throw new Error('This is a test error');
}

@OnEvent('error-throwing.provider', { suppressErrors: false })
onErrorThrowingEvent() {
throw new Error('This is a test error');
}
}
10 changes: 10 additions & 0 deletions tests/src/events-provider.request-scoped.consumer.ts
Expand Up @@ -26,4 +26,14 @@ export class EventsProviderRequestScopedConsumer {
onErrorHandlingEvent() {
throw new Error('This is a test error');
}

@OnEvent('error-handling-suppressed.request-scoped', { suppressErrors: true })
onErrorHandlingSuppressedEvent() {
throw new Error('This is a test error');
}

@OnEvent('error-throwing.request-scoped', { suppressErrors: false })
onErrorThrowingEvent() {
throw new Error('This is a test error');
}
}

0 comments on commit 5716f71

Please sign in to comment.