diff --git a/lib/AppLoader.js b/lib/AppLoader.js index e1db424..0ff4eb0 100644 --- a/lib/AppLoader.js +++ b/lib/AppLoader.js @@ -43,6 +43,10 @@ class AppLoader extends types_1.AppLoader { * @param handler 内部错误处理器 */ registerInternalErrorHandler(handler) { + // 检查有没有名称重复 + if (this.internalErrorHandlers.find(h => h.name === handler.name)) { + throw new Error(`内部错误处理器名称重复: ${handler.name}`); + } this.internalErrorHandlers.push(handler); } /** @@ -54,7 +58,8 @@ class AppLoader extends types_1.AppLoader { return new Promise(async (resolve) => { try { const ctx = await this.makeContext(); - handler(ctx, type, message, errorToPublish); + console.log(`调用internalErrorHandler【${handler.name}】处理内部错误事件`); + handler.handle(ctx, type, message, errorToPublish); } catch (e) { console.error('执行internalErrorHandler时出错', e); diff --git a/lib/types/index.d.ts b/lib/types/index.d.ts index 37d97a4..b5e7060 100644 --- a/lib/types/index.d.ts +++ b/lib/types/index.d.ts @@ -2,5 +2,8 @@ import { BaseEntityDict } from "oak-domain"; import { AsyncContext } from "oak-domain/lib/store/AsyncRowStore"; import { EntityDict } from "oak-domain/lib/types"; export type InternalErrorType = 'aspect' | 'trigger' | 'watcher' | 'timer' | 'checkpoint'; -export type InternalErrorHandler> = (ctx: Cxt, type: InternalErrorType, message: string, err: Error) => Promise; +export type InternalErrorHandler> = { + name: string; + handle: (ctx: Cxt, type: InternalErrorType, message: string, err: Error) => Promise; +}; export type ExceptionPublisher = (type: string, message: string, err: any) => Promise; diff --git a/src/AppLoader.ts b/src/AppLoader.ts index 8116936..4d16c3c 100644 --- a/src/AppLoader.ts +++ b/src/AppLoader.ts @@ -52,6 +52,10 @@ export class AppLoader) { + // 检查有没有名称重复 + if (this.internalErrorHandlers.find(h => h.name === handler.name)) { + throw new Error(`内部错误处理器名称重复: ${handler.name}`); + } this.internalErrorHandlers.push(handler); } @@ -65,7 +69,8 @@ export class AppLoader(async (resolve) => { try { const ctx = await this.makeContext(); - handler(ctx, type, message, errorToPublish); + console.log(`调用internalErrorHandler【${handler.name}】处理内部错误事件`); + handler.handle(ctx, type, message, errorToPublish); } catch (e) { console.error('执行internalErrorHandler时出错', e); } finally { diff --git a/src/types/index.ts b/src/types/index.ts index a9a6865..a580e6d 100644 --- a/src/types/index.ts +++ b/src/types/index.ts @@ -3,5 +3,8 @@ import { AsyncContext } from "oak-domain/lib/store/AsyncRowStore"; import { EntityDict, OakException } from "oak-domain/lib/types"; export type InternalErrorType = 'aspect' | 'trigger' | 'watcher' | 'timer' | 'checkpoint' -export type InternalErrorHandler> = (ctx: Cxt, type: InternalErrorType, message:string, err: Error) => Promise; +export type InternalErrorHandler> = { + name: string; + handle: (ctx: Cxt, type: InternalErrorType, message:string, err: Error) => Promise; +} export type ExceptionPublisher = (type: string, message: string, err: any) => Promise; \ No newline at end of file