Skip to content

Commit

Permalink
Add onError()
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jun 5, 2022
1 parent 9ff31a8 commit 408d890
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
5 changes: 2 additions & 3 deletions src/bin/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import { fileURLToPath } from 'url'
import { readPackageUp } from 'read-pkg-up'
import UpdateNotifier from 'update-notifier'

import { onError } from '../error/handler.js'
import { ErrorTypes, ERROR_PROPS } from '../error/main.js'
import { mergeErrorCause } from '../error/merge/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 @@ -58,7 +57,7 @@ const COMMANDS = { run, show, remove, dev }

// Print CLI errors and exit, depending on the error type
const handleCliError = function (error) {
const errorA = allowErrorTypes(mergeErrorCause(error), ErrorTypes)
const errorA = onError(error, ErrorTypes)
const { exitCode, printStack, indented } = ERROR_PROPS[errorA.name]
const errorMessage = printStack ? errorA.stack : errorA.message
const errorMessageA = indented ? addPadding(errorMessage) : errorMessage
Expand Down
12 changes: 5 additions & 7 deletions src/config/normalize/lib/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { list } from 'wild-wild-path'

import { mergeErrorCause } from '../../../error/merge/main.js'
import { allowErrorTypes } from '../../../error/types.js'
import { onError } from '../../../error/handler.js'
import { cleanObject } from '../../../utils/clean.js'

import { InputError, ErrorTypes } from './error.js'
Expand Down Expand Up @@ -117,12 +116,11 @@ 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 = mergeErrorCause(error)
const errorB = allowErrorTypes(errorA, ErrorTypes)
const errorA = onError(error, ErrorTypes)

if (soft && errorB instanceof InputError) {
return { error: errorB, warnings: [] }
if (soft && errorA instanceof InputError) {
return { error: errorA, warnings: [] }
}

throw errorB
throw errorA
}
8 changes: 3 additions & 5 deletions src/config/plugin/lib/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { mergeErrorCause } from '../../../error/merge/main.js'
import { allowErrorTypes } from '../../../error/types.js'
import { onError } from '../../../error/handler.js'

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

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

Expand Down
10 changes: 10 additions & 0 deletions src/error/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { mergeErrorCause } from './merge/main.js'
import { allowErrorTypes } from './types.js'

// Error handler that normalizes an error, merge its `error.cause` and ensure
// its type is among an allowed list of types.
export const onError = function (error, ErrorTypes) {
const errorA = mergeErrorCause(error)
const errorB = allowErrorTypes(errorA, ErrorTypes)
return errorB
}
11 changes: 5 additions & 6 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import 'error-cause/auto'
import { listCombinations } from './combination/list.js'
import { getConfig } from './config/main.js'
import { performDev } from './dev/main.js'
import { onError } from './error/handler.js'
import { ErrorTypes } from './error/main.js'
import { mergeErrorCause } from './error/merge/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'
Expand All @@ -32,7 +31,7 @@ export const run = async function (configFlags) {
checkLimits(programmaticResult)
return programmaticResult
} catch (error) {
throw allowErrorTypes(mergeErrorCause(error), ErrorTypes)
throw onError(error, ErrorTypes)
}
}

Expand All @@ -45,7 +44,7 @@ export const show = async function (configFlags) {
checkLimits(programmaticResult)
return programmaticResult
} catch (error) {
throw allowErrorTypes(mergeErrorCause(error), ErrorTypes)
throw onError(error, ErrorTypes)
}
}

Expand All @@ -59,7 +58,7 @@ export const remove = async function (configFlags) {
await removeFromHistory(targetRawResults, config)
return programmaticResult
} catch (error) {
throw allowErrorTypes(mergeErrorCause(error), ErrorTypes)
throw onError(error, ErrorTypes)
}
}

Expand All @@ -70,6 +69,6 @@ export const dev = async function (configFlags) {
const combinations = await listCombinations(config)
await performDev(combinations, config)
} catch (error) {
throw allowErrorTypes(mergeErrorCause(error), ErrorTypes)
throw onError(error, ErrorTypes)
}
}
6 changes: 2 additions & 4 deletions src/runners/common/error.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { createErrorType } from '../../error/create.js'
import { mergeErrorCause } from '../../error/merge/main.js'
import { allowErrorTypes } from '../../error/types.js'
import { onError } from '../../error/handler.js'

// Error from the library itself
export const CoreError = createErrorType('CoreError')
Expand All @@ -27,7 +26,6 @@ const ErrorTypes = [

// Serialize an error to send to parent
export const serializeError = function (error) {
const errorA = mergeErrorCause(error)
const { name, message, stack } = allowErrorTypes(errorA, ErrorTypes)
const { name, message, stack } = onError(error, ErrorTypes)
return { name, message, stack }
}

0 comments on commit 408d890

Please sign in to comment.