Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 29, 2022
1 parent b7a173c commit 4e1e48b
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 48 deletions.
8 changes: 4 additions & 4 deletions src/bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { fileURLToPath } from 'url'
import { readPackageUp } from 'read-pkg-up'
import UpdateNotifier from 'update-notifier'

import { getErrorProps } from '../error/main.js'
import { normalizeError } from '../error/utils.js'
import { ErrorTypes, ERROR_PROPS } from '../error/main.js'
import { allowErrorTypes } from '../error/types.js'
import { run, show, remove, dev } from '../main.js'
import { addPadding } from '../report/utils/indent.js'

Expand Down Expand Up @@ -57,8 +57,8 @@ const COMMANDS = { run, show, remove, dev }

// Print CLI errors and exit, depending on the error type
const handleCliError = function (error) {
const errorA = normalizeError(error)
const { exitCode, printStack, indented } = getErrorProps(errorA)
const errorA = allowErrorTypes(error, ErrorTypes)
const { exitCode, printStack, indented } = ERROR_PROPS[errorA.name]
const errorMessage = printStack ? errorA.stack : errorA.message
const errorMessageA = indented ? addPadding(errorMessage) : errorMessage
console.error(errorMessageA)
Expand Down
39 changes: 20 additions & 19 deletions src/error/main.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import { createErrorType } from './utils.js'

// User aborting the benchmark
export const StopError = createErrorType('StopError')
// Bug in the library itself
export const CoreError = createErrorType('CoreError')
// Bug in a plugin (reporter|runner)
export const PluginError = createErrorType('PluginError')
// Invalid options
export const UserError = createErrorType('UserError')
// Invalid tasks or tasks file
export const UserCodeError = createErrorType('UserCodeError')
// Invalid options
export const UserError = createErrorType('UserError')
// `limit` option threshold was reached
export const LimitError = createErrorType('LimitError')
// User aborting the benchmark
export const StopError = createErrorType('StopError')

// Retrieve error type-specific behavior
export const getErrorProps = function (error) {
const { name } = error
const nameA =
typeof name === 'string' && name in ERROR_PROPS ? name : DEFAULT_ERROR_NAME
return ERROR_PROPS[nameA]
}
// All error types, with first being default type
export const ErrorTypes = [
CoreError,
PluginError,
UserCodeError,
UserError,
LimitError,
StopError,
]

const ERROR_PROPS = {
StopError: { exitCode: 0, printStack: false, indented: true },
CoreError: { exitCode: 1, printStack: true, indented: false },
PluginError: { exitCode: 2, printStack: true, indented: false },
UserError: { exitCode: 3, printStack: false, indented: false },
// Error type-specific behavior
export const ERROR_PROPS = {
CoreError: { exitCode: 5, printStack: true, indented: false },
PluginError: { exitCode: 4, printStack: true, indented: false },
UserCodeError: { exitCode: 3, printStack: true, indented: false },
LimitError: { exitCode: 4, printStack: false, indented: false },
UserError: { exitCode: 2, printStack: false, indented: false },
LimitError: { exitCode: 1, printStack: false, indented: false },
StopError: { exitCode: 0, printStack: false, indented: true },
}

const DEFAULT_ERROR_NAME = 'CoreError'
69 changes: 44 additions & 25 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,71 @@
import { listCombinations } from './combination/list.js'
import { getConfig } from './config/main.js'
import { performDev } from './dev/main.js'
import { ErrorTypes } from './error/main.js'
import { allowErrorTypes } from './error/types.js'
import { checkLimits } from './history/compare/limit.js'
import { getFromHistory, removeFromHistory } from './history/data/main.js'
import { getTargetRawResults } from './history/merge/results.js'
import { reportResult } from './report/main.js'
import { createResult } from './run/create.js'
import { performRun } from './run/main.js'
// eslint-disable-next-line import/max-dependencies
import { initPreview } from './run/preview/start_end/main.js'

// Measure code defined in a tasks file and report the results.
// Default command.
export const run = async function (configFlags) {
const config = await getConfig('run', configFlags)
const previewState = initPreview(config)
const { newResult, history } = await createResult(config)
const programmaticResult = await performRun({
newResult,
history,
previewState,
config,
})
checkLimits(programmaticResult)
return programmaticResult
try {
const config = await getConfig('run', configFlags)
const previewState = initPreview(config)
const { newResult, history } = await createResult(config)
const programmaticResult = await performRun({
newResult,
history,
previewState,
config,
})
checkLimits(programmaticResult)
return programmaticResult
} catch (error) {
throw allowErrorTypes(error, ErrorTypes)
}
}

// Show a previous result
export const show = async function (configFlags) {
const config = await getConfig('show', configFlags)
const { rawResult, history } = await getFromHistory(config)
const programmaticResult = await reportResult(rawResult, history, config)
checkLimits(programmaticResult)
return programmaticResult
try {
const config = await getConfig('show', configFlags)
const { rawResult, history } = await getFromHistory(config)
const programmaticResult = await reportResult(rawResult, history, config)
checkLimits(programmaticResult)
return programmaticResult
} catch (error) {
throw allowErrorTypes(error, ErrorTypes)
}
}

// Remove a previous result
export const remove = async function (configFlags) {
const config = await getConfig('remove', configFlags)
const { rawResult, history } = await getFromHistory(config)
const targetRawResults = getTargetRawResults(rawResult, history)
const programmaticResult = await reportResult(rawResult, history, config)
await removeFromHistory(targetRawResults, config)
return programmaticResult
try {
const config = await getConfig('remove', configFlags)
const { rawResult, history } = await getFromHistory(config)
const targetRawResults = getTargetRawResults(rawResult, history)
const programmaticResult = await reportResult(rawResult, history, config)
await removeFromHistory(targetRawResults, config)
return programmaticResult
} catch (error) {
throw allowErrorTypes(error, ErrorTypes)
}
}

// Execute tasks without benchmarking them
export const dev = async function (configFlags) {
const config = await getConfig('dev', configFlags)
const combinations = await listCombinations(config)
await performDev(combinations, config)
try {
const config = await getConfig('dev', configFlags)
const combinations = await listCombinations(config)
await performDev(combinations, config)
} catch (error) {
throw allowErrorTypes(error, ErrorTypes)
}
}

0 comments on commit 4e1e48b

Please sign in to comment.