progress on better error handling

This commit is contained in:
jacob
2023-08-29 18:09:31 -05:00
parent 800531453f
commit a4848743c2
2 changed files with 18 additions and 12 deletions

View File

@@ -23,21 +23,28 @@ export type ModuleResult<T> = Promise<ImportPayload<T>>;
* export default commandModule({})
*/
export async function importModule<T>(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<T extends Module> {
module: T,
onError : Function
}
export async function defaultModuleLoader<T extends Module>(absPath: string): ModuleResult<T> {
let module = await importModule<T>(absPath);
assert(module, `Found an undefined module: ${absPath}`);
return { module, absPath };
return { module, absPath, errorHandler: '' };
}
export const fmtFileName = (fileName: string) => parse(fileName).name;

View File

@@ -17,11 +17,10 @@ export function startReadyEvent(
return concat(ready$, buildModules<AnyModule>(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(