feat: 改为从appLoader加载ExceptionHandler

This commit is contained in:
Pan Qiancheng 2025-11-11 11:03:39 +08:00
parent 2d8690abb6
commit f0671efef6
4 changed files with 56 additions and 58 deletions

View File

@ -39,13 +39,17 @@ function concat(...paths) {
});
}
async function startup(path, connector, omitWatchers, omitTimers, routine) {
let errorHandler = undefined;
try {
errorHandler = require((0, path_1.join)(path, 'lib', 'configuration', 'exception')).default;
}
catch (err) {
// 不存在exception配置
}
// let errorHandler: InternalErrorHandler<ED, Cxt> | undefined = undefined;
// try {
// errorHandler = require(join(
// path,
// 'lib',
// 'configuration',
// 'exception'
// )).default;
// } catch (err) {
// // 不存在exception配置
// }
const serverConfiguration = require((0, path_1.join)(path, 'lib', 'configuration', 'server')).default;
// 拿到package.json用作项目的唯一标识否则无法区分不同项目的Redis+socketIO连接
const packageJson = require((0, path_1.join)(path, 'package.json'));
@ -172,21 +176,22 @@ async function startup(path, connector, omitWatchers, omitTimers, routine) {
await appLoader.unmount();
return result;
}
if (errorHandler && typeof errorHandler === 'function') {
// polyfillConsole("startup", true, (props) => {
// if (props.level === "error") {
// appLoader.execRoutine(async (ctx) => {
// await errorHandler(props.caller, props.args, ctx);
// }).catch((err) => {
// console.warn('执行全局错误处理失败:', err);
// });
// if (errorHandler && typeof errorHandler === 'function') {
// // polyfillConsole("startup", true, (props) => {
// // if (props.level === "error") {
// // appLoader.execRoutine(async (ctx) => {
// // await errorHandler(props.caller, props.args, ctx);
// // }).catch((err) => {
// // console.warn('执行全局错误处理失败:', err);
// // });
// // }
// // return props.args;
// // });
// // appLoader.registerInternalErrorHandler(async (ctx, type, msg, err) => {
// // await errorHandler(ctx, type, msg, err);
// // });
// }
// return props.args;
// });
appLoader.registerInternalErrorHandler(async (ctx, type, msg, err, oakErr) => {
await errorHandler(ctx, type, msg, err, oakErr);
});
}
appLoader.regAllExceptionHandler();
// 否则启动服务器模式
koa.use(async (ctx, next) => {
try {

View File

@ -1,6 +1,6 @@
import { BaseEntityDict } from "oak-domain";
import { AsyncContext } from "oak-domain/lib/store/AsyncRowStore";
import { EntityDict, OakException } from "oak-domain/lib/types";
import { EntityDict } from "oak-domain/lib/types";
export type InternalErrorType = 'aspect' | 'trigger' | 'watcher' | 'timer' | 'checkpoint';
export type InternalErrorHandler<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = (ctx: Cxt, type: InternalErrorType, message: string, err: Error, oakException?: OakException<ED>) => Promise<void>;
export type InternalErrorHandler<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = (ctx: Cxt, type: InternalErrorType, message: string, err: Error) => Promise<void>;
export type ExceptionPublisher = (type: string, message: string, err: any) => Promise<void>;

View File

@ -26,7 +26,6 @@ import chalk from 'chalk';
import { checkNodeVersion, randomString } from '../utils';
import bcrypt from 'bcryptjs';
import { LogFormatter, polyfillConsole, removePolyfill } from './polyfill';
import { InternalErrorHandler } from '../types';
checkNodeVersion()
@ -57,18 +56,18 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
routine?: (context: AsyncContext<ED>) => Promise<void>,
): Promise<(() => Promise<any>) | any> {
let errorHandler: InternalErrorHandler<ED, Cxt> | undefined = undefined;
// let errorHandler: InternalErrorHandler<ED, Cxt> | undefined = undefined;
try {
errorHandler = require(join(
path,
'lib',
'configuration',
'exception'
)).default;
} catch (err) {
// 不存在exception配置
}
// try {
// errorHandler = require(join(
// path,
// 'lib',
// 'configuration',
// 'exception'
// )).default;
// } catch (err) {
// // 不存在exception配置
// }
const serverConfiguration: ServerConfiguration = require(join(
path,
@ -220,21 +219,22 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
return result;
}
if (errorHandler && typeof errorHandler === 'function') {
// polyfillConsole("startup", true, (props) => {
// if (props.level === "error") {
// appLoader.execRoutine(async (ctx) => {
// await errorHandler(props.caller, props.args, ctx);
// }).catch((err) => {
// console.warn('执行全局错误处理失败:', err);
// });
// if (errorHandler && typeof errorHandler === 'function') {
// // polyfillConsole("startup", true, (props) => {
// // if (props.level === "error") {
// // appLoader.execRoutine(async (ctx) => {
// // await errorHandler(props.caller, props.args, ctx);
// // }).catch((err) => {
// // console.warn('执行全局错误处理失败:', err);
// // });
// // }
// // return props.args;
// // });
// // appLoader.registerInternalErrorHandler(async (ctx, type, msg, err) => {
// // await errorHandler(ctx, type, msg, err);
// // });
// }
// return props.args;
// });
appLoader.registerInternalErrorHandler(async (ctx, type, msg, err, oakErr) => {
await errorHandler(ctx, type, msg, err, oakErr);
});
}
appLoader.regAllExceptionHandler()
// 否则启动服务器模式
koa.use(async (ctx, next) => {

View File

@ -1,7 +0,0 @@
import { BaseEntityDict } from "oak-domain";
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<ED extends EntityDict & BaseEntityDict, Cxt extends AsyncContext<ED>> = (ctx: Cxt, type: InternalErrorType, message:string, err: Error, oakException?: OakException<ED>, ) => Promise<void>;
export type ExceptionPublisher = (type: string, message: string, err: any) => Promise<void>;