Skip to content

Commit

Permalink
Improve error handling of plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jun 5, 2022
1 parent a8c6e38 commit aaf23dc
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 16 deletions.
6 changes: 2 additions & 4 deletions src/config/plugin/lib/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { pathToFileURL } from 'url'

import isPlainObj from 'is-plain-obj'

import { wrapError } from '../../../error/wrap.js'

import { PluginError } from './error.js'

// Builtin modules are lazy loaded for performance reasons.
Expand All @@ -19,8 +17,8 @@ export const importPlugin = async function (
const { plugin, path } = await IMPORTERS[locationType](location, builtins)
const pluginA = isPlainObj(plugin) ? { ...plugin } : plugin
return { plugin: pluginA, path }
} catch (error) {
throw wrapError(error, `Could not load "${name}"\n`, PluginError)
} catch (cause) {
throw new PluginError(`Could not load "${name}".`, { cause })
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/config/plugin/lib/info.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { wrapError } from '../../../error/wrap.js'

import { normalizePluginConfig } from './config.js'
import { PluginError } from './error.js'
import { importPlugin } from './import.js'
Expand Down Expand Up @@ -45,6 +43,6 @@ export const getPluginInfo = async function (pluginConfig, opts) {

const handlePluginError = function (error, originalLocation) {
return error instanceof PluginError
? wrapError(error, `Invalid plugin "${originalLocation}":`)
? new Error(`Invalid plugin "${originalLocation}".`, { cause: error })
: error
}
4 changes: 3 additions & 1 deletion src/config/plugin/lib/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { mergeErrorCause } from '../../../error/merge/main.js'
import { allowErrorTypes } from '../../../error/types.js'

import { validateDuplicatePlugins } from './duplicates.js'
Expand Down Expand Up @@ -32,7 +33,8 @@ export const getPlugins = async function (pluginConfigs, opts) {
validateDuplicatePlugins(pluginInfos, optsA)
return pluginInfos
} catch (error) {
throw allowErrorTypes(error, ErrorTypes)
const errorA = mergeErrorCause(error)
throw allowErrorTypes(errorA, ErrorTypes)
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/config/plugin/lib/module.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { createRequire } from 'module'

import { wrapError } from '../../../error/wrap.js'

// Resolve Node module names to absolute file paths.
// We enforce a npm package naming convention for all plugins.
// TODO: use import.meta.resolve() when available
Expand All @@ -13,11 +11,11 @@ export const resolveModuleLocation = function (

try {
return createRequire(`${cwd}/`).resolve(moduleName)
} catch (error) {
throw wrapError(
error,
} catch (cause) {
throw new Error(
`must be ${getBuiltinsError(builtins)}
This Node module was not found, please ensure it is installed.\n`,
This Node module was not found, please ensure it is installed:\n`,
{ cause },
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/config/plugin/lib/normalize.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { wrapError } from '../../../error/wrap.js'
import { normalizeInputs } from '../../normalize/lib/main.js'

import { CoreError } from './error.js'
Expand All @@ -9,7 +8,8 @@ export const safeNormalizeConfig = async function (config, rules, opts) {
const { inputs } = await normalizeInputs(config, rules, opts)
return inputs
} catch (error) {
throw wrapError(error, '', getErrorType(error, opts))
const ErrorType = getErrorType(error, opts)
throw new ErrorType('', { cause: error })
}
}

Expand Down

0 comments on commit aaf23dc

Please sign in to comment.