Skip to content

Commit

Permalink
Use modern-errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jun 12, 2022
1 parent ac3d910 commit cc0f671
Show file tree
Hide file tree
Showing 16 changed files with 77 additions and 247 deletions.
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"junk": "^4.0.0",
"map-obj": "^5.0.1",
"merge-error-cause": "^1.1.2",
"modern-errors": "^1.0.0",
"moize": "^6.1.1",
"normalize-exception": "^1.1.3",
"nvexeca": "^8.0.0",
Expand Down
14 changes: 5 additions & 9 deletions src/config/normalize/lib/error.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { modernErrors } from '../../../error/modern/main.js'
import { packageJson } from '../../../utils/package.js'
import modernErrors from 'modern-errors'

const { InputError, DefinitionError, KeywordError, onError } = modernErrors(
['InputError', 'DefinitionError', 'KeywordError'],
{ bugsUrl: packageJson.bugs.url },
)
import { packageJson } from '../../../utils/package.js'

export {
export const {
// Invalid `inputs`
InputError,
// Invalid `rules` or `options`
DefinitionError,
// Bug in a keyword|plugin
KeywordError,
// Top-level error handler
onError,
}
errorHandler,
} = modernErrors({ bugsUrl: packageJson.bugs.url })
6 changes: 3 additions & 3 deletions src/config/normalize/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { list } from 'wild-wild-path'

import { cleanObject } from '../../../utils/clean.js'

import { InputError, onError } from './error.js'
import { errorHandler } from './error.js'
import { getInfo } from './info.js'
import { applyKeywords } from './keywords/main.js'
import { normalizeOpts } from './options.js'
Expand Down Expand Up @@ -115,9 +115,9 @@ const LIST_OPTS = { childFirst: true, sort: true, missing: true, entries: true }
// When in `sort` mode, input errors are returned instead of being thrown.
// Other errors are always propagated.
const handleError = function (error, soft) {
const errorA = onError(error)
const errorA = errorHandler(error)

if (soft && errorA instanceof InputError) {
if (soft && errorA.name === 'InputError') {
return { error: errorA, warnings: [] }
}

Expand Down
18 changes: 0 additions & 18 deletions src/config/plugin/error.js

This file was deleted.

14 changes: 5 additions & 9 deletions src/config/plugin/lib/error.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import { modernErrors } from '../../../error/modern/main.js'
import { packageJson } from '../../../utils/package.js'
import modernErrors from 'modern-errors'

const { UserError, PluginError, ConsumerError, onError } = modernErrors(
['UserError', 'PluginError', 'ConsumerError'],
{ bugsUrl: packageJson.bugs.url },
)
import { packageJson } from '../../../utils/package.js'

export {
export const {
// Error from the library's user, who defines available plugin types
UserError,
// Error from a plugin author, who defines a specific plugin
PluginError,
// Error from a plugin user
ConsumerError,
// Top-level error handler
onError,
}
errorHandler,
} = modernErrors({ bugsUrl: packageJson.bugs.url })
6 changes: 3 additions & 3 deletions src/config/plugin/lib/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { validateDuplicatePlugins } from './duplicates.js'
import { UserError, onError } from './error.js'
import { UserError, errorHandler } from './error.js'
import { getPluginInfo } from './info.js'
import { normalizeMultipleOpts, normalizeSingleOpts } from './options.js'

Expand Down Expand Up @@ -30,7 +30,7 @@ export const getPlugins = async function (pluginConfigs, opts) {
validateDuplicatePlugins(pluginInfos, optsA)
return pluginInfos
} catch (error) {
throw onError(error)
throw errorHandler(error)
}
}

Expand All @@ -55,7 +55,7 @@ export const getPlugin = async function (pluginConfig, opts) {
const plugin = await getPluginInfo(pluginConfig, optsA)
return plugin
} catch (error) {
throw onError(error)
throw errorHandler(error)
}
}

Expand Down
20 changes: 17 additions & 3 deletions src/config/plugin/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { PluginError, UserError } from '../../error/main.js'
import { normalizeReporters } from '../../report/config/main.js'
import { REPORTER_PLUGIN_TYPE } from '../../report/reporters/plugin/main.js'
import { RUNNER_PLUGIN_TYPE } from '../../runners/plugin/main.js'
import { cleanObject } from '../../utils/clean.js'
import { PREFIX } from '../normalize/main.js'

import { handlePluginsError } from './error.js'
import { getPlugins } from './lib/main.js'

// Handle the configuration plugins: runners and reporters
Expand Down Expand Up @@ -51,11 +51,25 @@ const normalizePluginConfigs = async function ({
})
const pluginInfosA = pluginInfos.map(normalizePluginInfo)
return { [name]: pluginInfosA }
} catch (error) {
throw handlePluginsError(error)
} catch (cause) {
throw handlePluginsError(cause)
}
}

const normalizePluginInfo = function ({ plugin, config }) {
return cleanObject({ ...plugin, config })
}

// Translate error classes from the plugins library to error classes from this
// library
const handlePluginsError = function (cause) {
const ErrorType = cause.name in ERROR_MAP ? ERROR_MAP[cause.name] : Error
return new ErrorType('', { cause })
}

const ERROR_MAP = {
SystemError: Error,
UserError: Error,
PluginError,
ConsumerError: UserError,
}
22 changes: 5 additions & 17 deletions src/error/main.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import { packageJson } from '../utils/package.js'

import { modernErrors } from './modern/main.js'
import modernErrors from 'modern-errors'

const {
PluginError,
UserCodeError,
UserError,
LimitError,
StopError,
onError,
} = modernErrors(
['PluginError', 'UserCodeError', 'UserError', 'LimitError', 'StopError'],
{ bugsUrl: packageJson.bugs.url },
)
import { packageJson } from '../utils/package.js'

export {
export const {
// Bug in a plugin (reporter|runner)
PluginError,
// Invalid tasks or tasks file
Expand All @@ -26,5 +14,5 @@ export {
// User aborting the benchmark
StopError,
// Top-level error handler
onError,
}
errorHandler,
} = modernErrors({ bugsUrl: packageJson.bugs.url })
27 changes: 0 additions & 27 deletions src/error/modern/handler.js

This file was deleted.

33 changes: 0 additions & 33 deletions src/error/modern/main.js

This file was deleted.

51 changes: 0 additions & 51 deletions src/error/modern/opts.js

This file was deleted.

45 changes: 0 additions & 45 deletions src/error/modern/types.js

This file was deleted.

Loading

0 comments on commit cc0f671

Please sign in to comment.