From 835295eb9fe920c2b7f35e3513fac16924d6972f Mon Sep 17 00:00:00 2001 From: Innei Date: Wed, 20 Apr 2022 21:49:02 +0800 Subject: [PATCH] feat: boardcast error event --- package.json | 4 ++-- src/common/filters/any-exception.filter.ts | 19 +++++++++++++++++-- src/constants/event-bus.constant.ts | 1 + 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index ed173f19..beaa9662 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "start": "cross-env NODE_ENV=development nest start -w --path tsconfig.json", "start:debug": "cross-env NODE_ENV=development nest start --debug --watch", "start:cluster": "cross-env NODE_ENV=development nest start --watch -- --cluster", - "start:prod": "cross-env NODE_ENV=production node dist/main", + "start:prod": "cross-env NODE_ENV=production node dist/src/main", "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix", "prod": "cross-env NODE_ENV=production pm2-runtime start ecosystem.config.js", "prod:pm2": "cross-env NODE_ENV=production pm2 restart ecosystem.config.js", @@ -184,4 +184,4 @@ "ioredis": "5.0.4", "webpack": "5.72.0" } -} \ No newline at end of file +} diff --git a/src/common/filters/any-exception.filter.ts b/src/common/filters/any-exception.filter.ts index 3e144fae..2ed11400 100644 --- a/src/common/filters/any-exception.filter.ts +++ b/src/common/filters/any-exception.filter.ts @@ -13,10 +13,13 @@ import { } from '@nestjs/common' import { Reflector } from '@nestjs/core' +import { EventScope } from '~/constants/business-event.constant' +import { EventBusEvents } from '~/constants/event-bus.constant' import { HTTP_REQUEST_TIME } from '~/constants/meta.constant' import { LOG_DIR } from '~/constants/path.constant' import { REFLECTOR } from '~/constants/system.constant' import { isDev } from '~/global/env.global' +import { EventManagerService } from '~/processors/helper/helper.event.service' import { getIp } from '../../utils/ip.util' import { LoggingInterceptor } from '../interceptors/logging.interceptor' @@ -32,7 +35,10 @@ type myError = { export class AllExceptionsFilter implements ExceptionFilter { private readonly logger = new Logger(AllExceptionsFilter.name) private errorLogPipe: WriteStream - constructor(@Inject(REFLECTOR) private reflector: Reflector) {} + constructor( + @Inject(REFLECTOR) private reflector: Reflector, + private readonly eventManager: EventManagerService, + ) {} catch(exception: unknown, host: ArgumentsHost) { const ctx = host.switchToHttp() const response = ctx.getResponse() @@ -55,7 +61,16 @@ export class AllExceptionsFilter implements ExceptionFilter { if (status === HttpStatus.INTERNAL_SERVER_ERROR) { // message && Logger.debug(message, undefined, 'Catch') Logger.error(exception, undefined, 'Catch') - + this.eventManager.broadcast( + EventBusEvents.SystemException, + { + message: (exception as Error)?.message, + stack: (exception as Error)?.stack, + }, + { + scope: EventScope.TO_SYSTEM, + }, + ) if (!isDev) { this.errorLogPipe = this.errorLogPipe ?? diff --git a/src/constants/event-bus.constant.ts b/src/constants/event-bus.constant.ts index 7266b09f..b1b2c796 100644 --- a/src/constants/event-bus.constant.ts +++ b/src/constants/event-bus.constant.ts @@ -4,4 +4,5 @@ export enum EventBusEvents { TokenExpired = 'token.expired', CleanAggregateCache = 'cache.aggregate', + SystemException = 'system.exception', }