feat: 改为从appLoader加载ExceptionHandler
This commit is contained in:
parent
2d8690abb6
commit
f0671efef6
|
|
@ -39,13 +39,17 @@ function concat(...paths) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
async function startup(path, connector, omitWatchers, omitTimers, routine) {
|
async function startup(path, connector, omitWatchers, omitTimers, routine) {
|
||||||
let errorHandler = undefined;
|
// let errorHandler: InternalErrorHandler<ED, Cxt> | undefined = undefined;
|
||||||
try {
|
// try {
|
||||||
errorHandler = require((0, path_1.join)(path, 'lib', 'configuration', 'exception')).default;
|
// errorHandler = require(join(
|
||||||
}
|
// path,
|
||||||
catch (err) {
|
// 'lib',
|
||||||
// 不存在exception配置
|
// 'configuration',
|
||||||
}
|
// 'exception'
|
||||||
|
// )).default;
|
||||||
|
// } catch (err) {
|
||||||
|
// // 不存在exception配置
|
||||||
|
// }
|
||||||
const serverConfiguration = require((0, path_1.join)(path, 'lib', 'configuration', 'server')).default;
|
const serverConfiguration = require((0, path_1.join)(path, 'lib', 'configuration', 'server')).default;
|
||||||
// 拿到package.json,用作项目的唯一标识,否则无法区分不同项目的Redis+socketIO连接
|
// 拿到package.json,用作项目的唯一标识,否则无法区分不同项目的Redis+socketIO连接
|
||||||
const packageJson = require((0, path_1.join)(path, 'package.json'));
|
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();
|
await appLoader.unmount();
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
if (errorHandler && typeof errorHandler === 'function') {
|
// if (errorHandler && typeof errorHandler === 'function') {
|
||||||
// polyfillConsole("startup", true, (props) => {
|
// // polyfillConsole("startup", true, (props) => {
|
||||||
// if (props.level === "error") {
|
// // if (props.level === "error") {
|
||||||
// appLoader.execRoutine(async (ctx) => {
|
// // appLoader.execRoutine(async (ctx) => {
|
||||||
// await errorHandler(props.caller, props.args, ctx);
|
// // await errorHandler(props.caller, props.args, ctx);
|
||||||
// }).catch((err) => {
|
// // }).catch((err) => {
|
||||||
// console.warn('执行全局错误处理失败:', err);
|
// // console.warn('执行全局错误处理失败:', err);
|
||||||
// });
|
// // });
|
||||||
|
// // }
|
||||||
|
// // return props.args;
|
||||||
|
// // });
|
||||||
|
// // appLoader.registerInternalErrorHandler(async (ctx, type, msg, err) => {
|
||||||
|
// // await errorHandler(ctx, type, msg, err);
|
||||||
|
// // });
|
||||||
// }
|
// }
|
||||||
// return props.args;
|
appLoader.regAllExceptionHandler();
|
||||||
// });
|
|
||||||
appLoader.registerInternalErrorHandler(async (ctx, type, msg, err, oakErr) => {
|
|
||||||
await errorHandler(ctx, type, msg, err, oakErr);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// 否则启动服务器模式
|
// 否则启动服务器模式
|
||||||
koa.use(async (ctx, next) => {
|
koa.use(async (ctx, next) => {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { BaseEntityDict } from "oak-domain";
|
import { BaseEntityDict } from "oak-domain";
|
||||||
import { AsyncContext } from "oak-domain/lib/store/AsyncRowStore";
|
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 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>;
|
export type ExceptionPublisher = (type: string, message: string, err: any) => Promise<void>;
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,6 @@ import chalk from 'chalk';
|
||||||
import { checkNodeVersion, randomString } from '../utils';
|
import { checkNodeVersion, randomString } from '../utils';
|
||||||
import bcrypt from 'bcryptjs';
|
import bcrypt from 'bcryptjs';
|
||||||
import { LogFormatter, polyfillConsole, removePolyfill } from './polyfill';
|
import { LogFormatter, polyfillConsole, removePolyfill } from './polyfill';
|
||||||
import { InternalErrorHandler } from '../types';
|
|
||||||
|
|
||||||
checkNodeVersion()
|
checkNodeVersion()
|
||||||
|
|
||||||
|
|
@ -57,18 +56,18 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
|
||||||
routine?: (context: AsyncContext<ED>) => Promise<void>,
|
routine?: (context: AsyncContext<ED>) => Promise<void>,
|
||||||
): Promise<(() => Promise<any>) | any> {
|
): Promise<(() => Promise<any>) | any> {
|
||||||
|
|
||||||
let errorHandler: InternalErrorHandler<ED, Cxt> | undefined = undefined;
|
// let errorHandler: InternalErrorHandler<ED, Cxt> | undefined = undefined;
|
||||||
|
|
||||||
try {
|
// try {
|
||||||
errorHandler = require(join(
|
// errorHandler = require(join(
|
||||||
path,
|
// path,
|
||||||
'lib',
|
// 'lib',
|
||||||
'configuration',
|
// 'configuration',
|
||||||
'exception'
|
// 'exception'
|
||||||
)).default;
|
// )).default;
|
||||||
} catch (err) {
|
// } catch (err) {
|
||||||
// 不存在exception配置
|
// // 不存在exception配置
|
||||||
}
|
// }
|
||||||
|
|
||||||
const serverConfiguration: ServerConfiguration = require(join(
|
const serverConfiguration: ServerConfiguration = require(join(
|
||||||
path,
|
path,
|
||||||
|
|
@ -220,21 +219,22 @@ export async function startup<ED extends EntityDict & BaseEntityDict, FrontCxt e
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errorHandler && typeof errorHandler === 'function') {
|
// if (errorHandler && typeof errorHandler === 'function') {
|
||||||
// polyfillConsole("startup", true, (props) => {
|
// // polyfillConsole("startup", true, (props) => {
|
||||||
// if (props.level === "error") {
|
// // if (props.level === "error") {
|
||||||
// appLoader.execRoutine(async (ctx) => {
|
// // appLoader.execRoutine(async (ctx) => {
|
||||||
// await errorHandler(props.caller, props.args, ctx);
|
// // await errorHandler(props.caller, props.args, ctx);
|
||||||
// }).catch((err) => {
|
// // }).catch((err) => {
|
||||||
// console.warn('执行全局错误处理失败:', err);
|
// // console.warn('执行全局错误处理失败:', err);
|
||||||
// });
|
// // });
|
||||||
|
// // }
|
||||||
|
// // return props.args;
|
||||||
|
// // });
|
||||||
|
// // appLoader.registerInternalErrorHandler(async (ctx, type, msg, err) => {
|
||||||
|
// // await errorHandler(ctx, type, msg, err);
|
||||||
|
// // });
|
||||||
// }
|
// }
|
||||||
// return props.args;
|
appLoader.regAllExceptionHandler()
|
||||||
// });
|
|
||||||
appLoader.registerInternalErrorHandler(async (ctx, type, msg, err, oakErr) => {
|
|
||||||
await errorHandler(ctx, type, msg, err, oakErr);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// 否则启动服务器模式
|
// 否则启动服务器模式
|
||||||
koa.use(async (ctx, next) => {
|
koa.use(async (ctx, next) => {
|
||||||
|
|
|
||||||
|
|
@ -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>;
|
|
||||||
Loading…
Reference in New Issue