Skip to content

Commit

Permalink
Improve plugin error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 29, 2022
1 parent b2978d4 commit cd36eb3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 32 deletions.
8 changes: 4 additions & 4 deletions src/config/plugin/lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ const normalizeSharedConfig = async function ({
return await safeNormalizeConfig(pluginConfig, [...shared, ...dummyRules], {
all: { cwd, prefix, parent, context: { ...context, plugin } },
keywords,
UserErrorType: ConsumerError,
SystemErrorType: UserError,
InputErrorType: ConsumerError,
DefinitionErrorType: UserError,
})
}

Expand All @@ -98,8 +98,8 @@ const normalizeSpecificConfig = async function ({
{
all: { cwd, prefix, parent, context },
keywords,
UserErrorType: ConsumerError,
SystemErrorType: PluginError,
InputErrorType: ConsumerError,
DefinitionErrorType: PluginError,
},
)
}
4 changes: 2 additions & 2 deletions src/config/plugin/lib/location_normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ export const normalizeLocation = async function ({
context: { locationType, builtins, modulePrefix },
},
keywords,
UserErrorType: ConsumerError,
SystemErrorType: CoreError,
InputErrorType: ConsumerError,
DefinitionErrorType: CoreError,
})
}

Expand Down
33 changes: 13 additions & 20 deletions src/config/plugin/lib/normalize.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
import { wrapError } from '../../../error/wrap.js'
import { normalizeInputs } from '../../normalize/lib/main.js'

// Call `normalizeConfig` while assigning the right error types
export const safeNormalizeConfig = async function (
config,
rules,
{ UserErrorType, ...opts },
) {
const { inputs, error } = await callNormalizeConfig(config, rules, opts)
import { CoreError } from './error.js'

if (error) {
throw wrapError(error, '', UserErrorType)
// Call `normalizeConfig` while assigning the right error types
export const safeNormalizeConfig = async function (config, rules, opts) {
try {
const { inputs } = await normalizeInputs(config, rules, opts)
return inputs
} catch (error) {
throw wrapError(error, '', getErrorType(error, opts))
}

return inputs
}

const callNormalizeConfig = async function (
config,
rules,
{ SystemErrorType, ...opts },
) {
try {
return await normalizeInputs(config, rules, { ...opts, soft: true })
} catch (error) {
throw wrapError(error, '', SystemErrorType)
const getErrorType = function (error, { InputErrorType, DefinitionErrorType }) {
if (error.name === 'InputError') {
return InputErrorType
}

return error.name === 'DefinitionError' ? DefinitionErrorType : CoreError
}
8 changes: 4 additions & 4 deletions src/config/plugin/lib/shape.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export const normalizeShape = async function ({
context: { sharedPropNames, locationType, originalLocation },
},
keywords,
UserErrorType: PluginError,
SystemErrorType: CoreError,
InputErrorType: PluginError,
DefinitionErrorType: CoreError,
})

if (shape === undefined) {
Expand All @@ -31,8 +31,8 @@ export const normalizeShape = async function ({
{
all: { prefix: PLUGIN_PREFIX, context },
keywords,
UserErrorType: PluginError,
SystemErrorType: UserError,
InputErrorType: PluginError,
DefinitionErrorType: UserError,
},
)
}
Expand Down
4 changes: 2 additions & 2 deletions src/config/plugin/lib/top.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const normalizePluginConfigTop = async function (
await safeNormalizeConfig(pluginConfig, [normalizeTop], {
all: { cwd, prefix, parent: name, context: { pluginProp, builtins } },
keywords,
UserErrorType: ConsumerError,
SystemErrorType: CoreError,
InputErrorType: ConsumerError,
DefinitionErrorType: CoreError,
})
return { originalLocation, pluginConfig: pluginConfigA, locationName }
}
Expand Down

0 comments on commit cd36eb3

Please sign in to comment.