Skip to content

Commit

Permalink
Improve bugs plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Oct 30, 2022
1 parent 8338411 commit 406b95f
Show file tree
Hide file tree
Showing 12 changed files with 74 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/combination/tasks/load.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { computeRunnerVersions } from '../../top/system/versions/compute.js'
// Select the runners and retrieve their related spawn options using
// `runner.launch()`
export const loadRunner = async function (
{ id, config, launch },
{ id, config, launch, bugs },
cwd,
commonVersions,
) {
Expand All @@ -20,7 +20,7 @@ export const loadRunner = async function (
spawnOptions,
cwd,
})
return { id, spawn, spawnOptions, versions: versionsA, config }
return { id, spawn, spawnOptions, versions: versionsA, config, bugs }
}

// Fire `runner.launch()`.
Expand Down
10 changes: 7 additions & 3 deletions src/combination/tasks/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { handleRunnerError } from '../../runners/plugin/error.js'
import { handlePluginError } from '../../config/plugin/error.js'
import { AnyError, PluginError } from '../../error/main.js'
import { getCommonVersions } from '../../top/system/versions/common.js'
import { getCombsNoDimensions } from '../filter.js'

Expand Down Expand Up @@ -97,7 +98,10 @@ const getDimensions = function (runner) {
}

const getDimensionsTasks = async function ({
dimensions: { runner },
dimensions: {
runner,
runner: { bugs },
},
noDimensions,
cwd,
commonVersions,
Expand All @@ -111,7 +115,7 @@ const getDimensionsTasks = async function ({
)
return tasks.flat()
} catch (error) {
throw handleRunnerError(error, runner)
throw handlePluginError({ error, bugs, PluginError, AnyError })
}
}

Expand Down
11 changes: 11 additions & 0 deletions src/config/plugin/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Add `bugs` URL on `PluginError`
export const handlePluginError = function ({
error,
bugs,
PluginError,
AnyError,
}) {
return error instanceof PluginError && typeof bugs === 'string' && bugs !== ''
? new AnyError('', { cause: error, bugs })
: error
}
8 changes: 0 additions & 8 deletions src/config/plugin/lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,3 @@ export const PluginError = AnyError.subclass('PluginError')

// Error from a plugin user
export const ConsumerError = AnyError.subclass('ConsumerError')

// When `options.bugs` is defined, user errors report it.
// When `plugin.bugs` is defined, plugin errors report it.
export const addErrorBugs = function (error, bugsOpt) {
return typeof bugsOpt === 'string' && bugsOpt !== ''
? new AnyError('', { cause: error, bugs: bugsOpt })
: error
}
11 changes: 5 additions & 6 deletions src/config/plugin/lib/info.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { handlePluginError } from '../error.js'

import { normalizePluginConfig } from './config.js'
import { AnyError, PluginError, addErrorBugs } from './error.js'
import { AnyError, PluginError } from './error.js'
import { importPlugin } from './import.js'
import { normalizeLocation } from './location_normalize.js'
import { getLocationType } from './location_type.js'
Expand Down Expand Up @@ -40,6 +42,7 @@ export const getPluginInfo = async function (pluginConfig, opts) {
const normalizePluginInfo = async function ({
pluginConfig,
plugin,
plugin: { bugs },
locationType,
originalLocation,
opts,
Expand All @@ -61,14 +64,10 @@ const normalizePluginInfo = async function ({
})
return { plugin: pluginB, config: pluginConfigA }
} catch (error) {
throw addPluginErrorBugsUrl(error, plugin)
throw handlePluginError({ error, bugs, PluginError, AnyError })
}
}

const addPluginErrorBugsUrl = function (error, { bugs }) {
return error instanceof PluginError ? addErrorBugs(error, bugs) : error
}

const addPluginErrorLocation = function (error, originalLocation) {
return error instanceof PluginError
? new AnyError(`Invalid plugin "${originalLocation}".`, { cause: error })
Expand Down
12 changes: 9 additions & 3 deletions src/config/plugin/lib/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { handlePluginError } from '../error.js'

import { validateDuplicatePlugins } from './duplicates.js'
import { AnyError, UserError, addErrorBugs } from './error.js'
import { AnyError, UserError } from './error.js'
import { getPluginInfo } from './info.js'
import { normalizeMultipleOpts, normalizeSingleOpts } from './options.js'

Expand Down Expand Up @@ -66,6 +68,10 @@ const validateDefined = function (value, { name }) {
}

const handleError = function (error, { bugs }) {
const errorA = AnyError.normalize(error)
return errorA instanceof UserError ? addErrorBugs(errorA, bugs) : errorA
return handlePluginError({
error: AnyError.normalize(error),
bugs,
PluginError: UserError,
AnyError,
})
}
7 changes: 4 additions & 3 deletions src/report/contents/get.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { excludeKeys } from 'filter-obj'

import { PluginError } from '../../error/main.js'
import { getReporterPluginError } from '../error.js'
import { FORMATS } from '../formats/list.js'

// Retrieve reporter's contents by calling all `reporter.report()` then
Expand All @@ -25,7 +25,6 @@ const callReportFunc = async function ({
startData,
config: reporterConfig,
config: { format, output, colors },
bugs,
},
}) {
const reporterSpecificConfig = excludeKeys(
Expand All @@ -41,7 +40,9 @@ const callReportFunc = async function ({
])
return { content, result, format, footerString, output, colors }
} catch (cause) {
throw new PluginError(`Could not call reporter "${id}".`, { cause, bugs })
throw getReporterPluginError(reporter, `Could not call reporter "${id}".`, {
cause,
})
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/report/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { handlePluginError } from '../config/plugin/error.js'
import { PluginError, AnyError } from '../error/main.js'

// Create a reporter plugin error
export const getReporterPluginError = function (reporter, ...args) {
const error = new PluginError(...args)
return handlePluginError({
error,
bugs: reporter.bugs,
PluginError,
AnyError,
})
}
18 changes: 13 additions & 5 deletions src/report/start_end.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PluginError } from '../error/main.js'
import { getReporterPluginError } from './error.js'

// Call all `reporter.start()`
export const startReporters = async function (config) {
Expand All @@ -7,7 +7,7 @@ export const startReporters = async function (config) {
}

const startReporter = async function (reporter) {
const { start, id, bugs } = reporter
const { start, id } = reporter

if (start === undefined) {
return reporter
Expand All @@ -17,7 +17,11 @@ const startReporter = async function (reporter) {
const startData = await start()
return { ...reporter, startData }
} catch (cause) {
throw new PluginError(`Could not start reporter "${id}".`, { cause, bugs })
throw getReporterPluginError(
reporter,
`Could not start reporter "${id}".`,
{ cause },
)
}
}

Expand All @@ -26,14 +30,18 @@ export const endReporters = async function ({ reporters }) {
await Promise.all(reporters.map(endReporter))
}

const endReporter = async function ({ end, startData, id, bugs }) {
const endReporter = async function (reporter) {
const { end, startData, id } = reporter

if (end === undefined) {
return
}

try {
await end(startData)
} catch (cause) {
throw new PluginError(`Could not end reporter "${id}".`, { cause, bugs })
throw getReporterPluginError(reporter, `Could not end reporter "${id}".`, {
cause,
})
}
}
11 changes: 8 additions & 3 deletions src/run/measure/single.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getCombinationPrefix } from '../../combination/ids/name.js'
import { handlePluginError } from '../../config/plugin/error.js'
import { useConfigSelectors } from '../../config/select/use.js'
import { AnyError } from '../../error/main.js'
import { handleRunnerError } from '../../runners/plugin/error.js'
import { AnyError, PluginError } from '../../error/main.js'
import { startLogs, stopLogs, hasLogs } from '../logs/create.js'
import { addErrorTaskLogs } from '../logs/error.js'
import { startLogsStream, stopLogsStream } from '../logs/stream.js'
Expand Down Expand Up @@ -35,7 +35,12 @@ export const measureCombination = async function ({ index, config, ...args }) {
await endCombinationPreview(previewState)
return { ...combination, stats, taskIds }
} catch (error) {
throw handleRunnerError(error, combination.dimensions.runner)
throw handlePluginError({
error,
bugs: combination.dimensions.runner.bugs,
PluginError,
AnyError,
})
} finally {
await updateDescription(previewState, '')
}
Expand Down
7 changes: 2 additions & 5 deletions src/runners/common/error.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import modernErrors from 'modern-errors'
import modernErrorsBugs from 'modern-errors-bugs'
import modernErrorsSerialize from 'modern-errors-serialize'

import { bugs } from '../../utils/package.js'
export const AnyError = modernErrors([modernErrorsSerialize])

export const AnyError = modernErrors([modernErrorsBugs, modernErrorsSerialize])

export const UnknownError = AnyError.subclass('UnknownError', { bugs })
export const UnknownError = AnyError.subclass('UnknownError')

// Could not JSON-stringify IPC payload
export const IpcSerializationError = AnyError.subclass('IpcSerializationError')
Expand Down
8 changes: 0 additions & 8 deletions src/runners/plugin/error.js

This file was deleted.

0 comments on commit 406b95f

Please sign in to comment.