From a4848743c2a9c587fca47bb30f9d13bac6c146df Mon Sep 17 00:00:00 2001 From: jacob Date: Tue, 29 Aug 2023 18:09:31 -0500 Subject: [PATCH] progress on better error handling --- src/core/module-loading.ts | 21 ++++++++++++++------- src/handlers/ready-event.ts | 9 ++++----- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/core/module-loading.ts b/src/core/module-loading.ts index 94f635a..80977c9 100644 --- a/src/core/module-loading.ts +++ b/src/core/module-loading.ts @@ -23,21 +23,28 @@ export type ModuleResult = Promise>; * export default commandModule({}) */ export async function importModule(absPath: string) { - let module = await import(absPath).then(esm => esm.default); + let fileModule = await import(absPath); - assert(module, `Found no export for module at ${absPath}. Forgot to ignore with "!"? (!${basename(absPath)})?`); - if ('default' in module) { - module = module.default; + let commandModule = fileModule.default, + onError = fileModule.onError; + + assert(commandModule , `Found no export @ ${absPath}. Forgot to ignore with "!"? (!${basename(absPath)})?`); + if ('default' in commandModule ) { + commandModule = commandModule.default; } return Result - .wrap(() => module.getInstance()) - .unwrapOr(module) as T; + .wrap(() => ({ module: commandModule.getInstance(), onError })) + .unwrapOr({ module: commandModule, onError }) as T; +} +interface FileModuleImports { + module: T, + onError : Function } export async function defaultModuleLoader(absPath: string): ModuleResult { let module = await importModule(absPath); assert(module, `Found an undefined module: ${absPath}`); - return { module, absPath }; + return { module, absPath, errorHandler: '' }; } export const fmtFileName = (fileName: string) => parse(fileName).name; diff --git a/src/handlers/ready-event.ts b/src/handlers/ready-event.ts index b9da8f9..30d02fe 100644 --- a/src/handlers/ready-event.ts +++ b/src/handlers/ready-event.ts @@ -17,11 +17,10 @@ export function startReadyEvent( return concat(ready$, buildModules(allPaths, moduleManager)) .pipe(callInitPlugins(sEmitter)) - .subscribe(module => { - register(moduleManager, module).expect( - SernError.InvalidModuleType + ' ' + util.inspect(module), - ); - }); + .subscribe(module => + register(moduleManager, module) + .expect(SernError.InvalidModuleType + ' ' + util.inspect(module)) + ); } const once = () => pipe(