From 5cec2b20e637c536d2bbeefc1c6119676f65cfad Mon Sep 17 00:00:00 2001 From: Mike Donnalley Date: Mon, 4 Mar 2024 16:04:44 -0700 Subject: [PATCH] fix: improve module load error handling (#996) --- src/config/plugin.ts | 5 +---- src/module-loader.ts | 30 +++++++++++------------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/src/config/plugin.ts b/src/config/plugin.ts index 4821cad01..e6ba4e9a3 100644 --- a/src/config/plugin.ts +++ b/src/config/plugin.ts @@ -140,8 +140,6 @@ export class Plugin implements IPlugin { version!: string - protected warned = false - _base = `${_pjson.name}@${_pjson.version}` // eslint-disable-next-line new-cap @@ -324,7 +322,7 @@ export class Plugin implements IPlugin { return [id, cached] } catch (error: any) { - const scope = 'cacheCommand' + const scope = `findCommand (${id})` if (Boolean(errorOnManifestCreate) === false) this.warn(error, scope) else throw this.addErrorScope(error, scope) } @@ -432,7 +430,6 @@ export class Plugin implements IPlugin { } private warn(err: CLIError | Error | string, scope?: string): void { - if (this.warned) return if (typeof err === 'string') err = new Error(err) process.emitWarning(this.addErrorScope(err, scope)) } diff --git a/src/module-loader.ts b/src/module-loader.ts index ed3e8cc27..9488c799e 100644 --- a/src/module-loader.ts +++ b/src/module-loader.ts @@ -18,6 +18,14 @@ const s_EXTENSIONS: string[] = ['.ts', '.js', '.mjs', '.cjs', '.mts', '.cts'] const isPlugin = (config: IConfig | IPlugin): config is IPlugin => (config).type !== undefined +function handleError(error: any, isESM: boolean | undefined, path: string): never { + if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') { + throw new ModuleLoadError(`${isESM ? 'import()' : 'require'} failed to load ${path}: ${error.message}`) + } + + throw error +} + /** * Loads and returns a module. * @@ -41,11 +49,7 @@ export async function load(config: IConfig | IPlugin, modulePath: strin ;({filePath, isESM} = await resolvePath(config, modulePath)) return (isESM ? await import(pathToFileURL(filePath).href) : require(filePath)) as T } catch (error: any) { - if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') { - throw new ModuleLoadError(`${isESM ? 'import()' : 'require'} failed to load ${filePath || modulePath}`) - } - - throw error + handleError(error, isESM, filePath ?? modulePath) } } @@ -77,13 +81,7 @@ export async function loadWithData( const module = isESM ? await import(pathToFileURL(filePath).href) : require(filePath) return {filePath, isESM, module} } catch (error: any) { - if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') { - throw new ModuleLoadError( - `${isESM ? 'import()' : 'require'} failed to load ${filePath || modulePath}: ${error.message}`, - ) - } - - throw error + handleError(error, isESM, filePath ?? modulePath) } } @@ -120,13 +118,7 @@ export async function loadWithDataFromManifest( const module = isESM ? await import(pathToFileURL(filePath).href) : require(filePath) return {filePath, isESM, module} } catch (error: any) { - if (error.code === 'MODULE_NOT_FOUND' || error.code === 'ERR_MODULE_NOT_FOUND') { - throw new ModuleLoadError( - `${isESM ? 'import()' : 'require'} failed to load ${filePath || modulePath}: ${error.message}`, - ) - } - - throw error + handleError(error, isESM, filePath ?? modulePath) } }