Skip to content

Commit

Permalink
fix(): move try-catch to event-subscribers.loader.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
pooreumu committed Aug 6, 2023
1 parent 498fbb3 commit ff091ec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
19 changes: 0 additions & 19 deletions lib/decorators/on-event.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { extendArrayMetadata } from '@nestjs/common/utils/extend-metadata.util';
import { EVENT_LISTENER_METADATA } from '../constants';
import { OnEventOptions } from '../interfaces';
import { Logger } from '@nestjs/common';

/**
* `@OnEvent` decorator metadata
Expand All @@ -22,22 +21,6 @@ export interface OnEventMetadata {
*/
export type OnEventType = string | symbol | Array<string | symbol>;

/**
* Wraps the method in try/catch blocks.
* @param descriptor
*/
const wrapDescriptorInTryCatchBlocks = (descriptor: any) => {
const originMethod = descriptor.value;
descriptor.value = async function (...args: any[]) {
try {
await originMethod.call(this, ...args);
} catch (error) {
Logger.error(error, 'OnEvent');
}
};
Object.setPrototypeOf(descriptor.value, originMethod);
};

/**
* Event listener decorator.
* Subscribes to events based on the specified name(s).
Expand All @@ -49,8 +32,6 @@ export const OnEvent = (
options?: OnEventOptions,
): MethodDecorator => {
const decoratorFactory = (target: object, key?: any, descriptor?: any) => {
wrapDescriptorInTryCatchBlocks(descriptor);

extendArrayMetadata(
EVENT_LISTENER_METADATA,
[{ event, options } as OnEventMetadata],
Expand Down
22 changes: 19 additions & 3 deletions lib/event-subscribers.loader.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Injectable,
Logger,
OnApplicationBootstrap,
OnApplicationShutdown,
} from '@nestjs/common';
Expand All @@ -24,6 +25,7 @@ export class EventSubscribersLoader
implements OnApplicationBootstrap, OnApplicationShutdown
{
private readonly injector = new Injector();
private readonly logger = new Logger('Event');

constructor(
private readonly discoveryService: DiscoveryService,
Expand Down Expand Up @@ -92,7 +94,8 @@ export class EventSubscribersLoader
} else {
listenerMethod(
event,
(...args: unknown[]) => instance[methodKey].call(instance, ...args),
(...args: unknown[]) =>
this.wrapFunctionInTryCatchBlocks(instance, methodKey, args),
options,
);
}
Expand Down Expand Up @@ -135,9 +138,10 @@ export class EventSubscribersLoader
moduleRef.providers,
contextId,
);
return contextInstance[listenerMethodKey].call(
return this.wrapFunctionInTryCatchBlocks(
contextInstance,
...args,
listenerMethodKey,
args,
);
},
options,
Expand Down Expand Up @@ -168,4 +172,16 @@ export class EventSubscribersLoader

this.moduleRef.registerRequestByContextId(payloadObjectOrArray, contextId);
}

private async wrapFunctionInTryCatchBlocks(
instance: Record<string, any>,
methodKey: string,
args: unknown[],
) {
try {
return await instance[methodKey].call(instance, ...args);
} catch (e) {
this.logger.error(e);
}
}
}

0 comments on commit ff091ec

Please sign in to comment.