Skip to content

Commit

Permalink
fix: improve module load error handling (#996)
Browse files Browse the repository at this point in the history
  • Loading branch information
mdonnalley authored Mar 4, 2024
1 parent d3a6d78 commit 5cec2b2
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 23 deletions.
5 changes: 1 addition & 4 deletions src/config/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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))
}
Expand Down
30 changes: 11 additions & 19 deletions src/module-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@ const s_EXTENSIONS: string[] = ['.ts', '.js', '.mjs', '.cjs', '.mts', '.cts']

const isPlugin = (config: IConfig | IPlugin): config is IPlugin => (<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.
*
Expand All @@ -41,11 +49,7 @@ export async function load<T = any>(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)
}
}

Expand Down Expand Up @@ -77,13 +81,7 @@ export async function loadWithData<T = any>(
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)
}
}

Expand Down Expand Up @@ -120,13 +118,7 @@ export async function loadWithDataFromManifest<T = any>(
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)
}
}

Expand Down

0 comments on commit 5cec2b2

Please sign in to comment.