core: server: wrap bail method

This commit is contained in:
undefined committed 2021-07-08 12:09:04 +08:00
1 parent c184d94dae
commit 8d15219757
2 files changed
+15 -10

No files matched your search

+1 -1
View File
@@ -173,7 +173,7 @@ export async function serial<K extends keyof EventMap>(name: K, ...args: Paramet
}
}
export async function bail<K extends keyof EventMap>(name: K, ...args: Parameters<EventMap[K]>): ReturnType<EventMap[K]> {
export async function bail<K extends keyof EventMap>(name: K, ...args: Parameters<EventMap[K]>): Promise<ReturnType<EventMap[K]>> {
if (argv.options.showBus && name !== 'message/log') logger.debug('bail: %s %o', name, args);
const hooks = Array.from(_hooks[name] || []);
for (const callback of hooks) {
+14 -9
View File
@@ -638,6 +638,11 @@ export class Handler extends HandlerCommon {
}
}
async function bail(name: string, ...args: any[]) {
const r = await bus.bail(name, ...args);
if (r instanceof Error) throw r;
}
async function handle(ctx, HandlerClass, checker) {
global.Hydro.stat.reqCount++;
const args = {
@@ -670,21 +675,21 @@ async function handle(ctx, HandlerClass, checker) {
throw new MethodNotAllowedError(method);
}
await bus.bail('handler/init', h);
await bus.bail(`handler/before-prepare/${HandlerClass.name.replace(/Handler$/, '')}`, h);
await bus.bail('handler/before-prepare', h);
await bail('handler/init', h);
await bail(`handler/before-prepare/${HandlerClass.name.replace(/Handler$/, '')}`, h);
await bail('handler/before-prepare', h);
if (h._prepare) await h._prepare(args);
if (h.prepare) await h.prepare(args);
await bus.bail(`handler/before/${HandlerClass.name.replace(/Handler$/, '')}`, h);
await bus.bail('handler/before', h);
await bail(`handler/before/${HandlerClass.name.replace(/Handler$/, '')}`, h);
await bail('handler/before', h);
if (h[method]) await h[method](args);
if (operation) await h[`post${operation}`](args);
await bus.bail(`handler/after/${HandlerClass.name.replace(/Handler$/, '')}`, h);
await bus.bail('handler/after', h);
await bail(`handler/after/${HandlerClass.name.replace(/Handler$/, '')}`, h);
await bail('handler/after', h);
if (h.cleanup) await h.cleanup(args);
if (h.finish) await h.finish(args);
await bus.bail(`handler/finish/${HandlerClass.name.replace(/Handler$/, '')}`, h);
await bus.bail('handler/finish', h);
await bail(`handler/finish/${HandlerClass.name.replace(/Handler$/, '')}`, h);
await bail('handler/finish', h);
} catch (e) {
try {
await h.onerror(e);