Skip to content

Commit

Permalink
Improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Jun 5, 2022
1 parent aaf23dc commit 8095ed4
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 38 deletions.
5 changes: 2 additions & 3 deletions src/runners/cli/handler/measure.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { execa, execaCommand } from 'execa'
import now from 'precise-now'

import { wrapError } from '../../../error/wrap.js'
import { TasksRunError } from '../../common/error.js'

// `beforeAll` and `afterAll`
Expand Down Expand Up @@ -87,8 +86,8 @@ const spawnProcess = async function (command, { env, shell }) {
return shell === 'none'
? await execaCommand(command, { ...EXECA_OPTIONS, env })
: await execa(command, { ...EXECA_OPTIONS, env, shell })
} catch (error) {
throw wrapError(error, '', TasksRunError)
} catch (cause) {
throw new TasksRunError('', { cause })
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/runners/cli/handler/start/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { wrapError } from '../../../../error/wrap.js'
import { loadYamlFile } from '../../../../utils/yaml.js'
import { TasksLoadError } from '../../../common/error.js'

Expand Down Expand Up @@ -27,8 +26,8 @@ export const start = async function (
const importFile = async function (taskPath) {
try {
return await loadYamlFile(taskPath)
} catch (error) {
throw wrapError(error, '', TasksLoadError)
} catch (cause) {
throw new TasksLoadError('', { cause })
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/runners/common/error.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { createErrorType } from '../../error/create.js'
import { mergeErrorCause } from '../../error/merge/main.js'
import { allowErrorTypes } from '../../error/types.js'

// Error from the library itself
Expand Down Expand Up @@ -26,6 +27,7 @@ const ErrorTypes = [

// Serialize an error to send to parent
export const serializeError = function (error) {
const { name, message, stack } = allowErrorTypes(error, ErrorTypes)
const errorA = mergeErrorCause(error)
const { name, message, stack } = allowErrorTypes(errorA, ErrorTypes)
return { name, message, stack }
}
6 changes: 2 additions & 4 deletions src/runners/common/ipc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import { argv } from 'process'

import got from 'got'

import { wrapError } from '../../error/wrap.js'

import { serializeError, IpcSerializationError } from './error.js'

// Handles IPC communication with the parent process
Expand Down Expand Up @@ -48,8 +46,8 @@ const handlePayload = async function (payload, handlers, state) {
const safeSerializeBody = function (returnValue = {}) {
try {
return serializeBody(returnValue)
} catch (error) {
throw wrapError(error, '', IpcSerializationError)
} catch (cause) {
throw new IpcSerializationError('', { cause })
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/runners/common/validate/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { inspect } from 'util'

import isPlainObj from 'is-plain-obj'

import { wrapError } from '../../../error/wrap.js'
import { mapValues } from '../../../utils/map.js'
import { TasksSyntaxError } from '../error.js'

Expand All @@ -22,8 +21,8 @@ export const validateTasks = function ({ tasks, validators, normalizeTask }) {
const validateTask = function ({ taskId, task, validators, normalizeTask }) {
try {
return eValidateTask({ task, validators, normalizeTask })
} catch (error) {
throw wrapError(error, `Task "${taskId}"`)
} catch (cause) {
throw new Error(`Task "${taskId}":`, { cause })
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/runners/node/config/version.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { readPackageUp } from 'read-pkg-up'
import semver from 'semver'

import { normalizeNumberString } from '../../../config/normalize/transform.js'
import { wrapError } from '../../../error/wrap.js'

// Normalize and validate the Node.js version
const transformVersion = async function (version) {
Expand All @@ -33,8 +32,8 @@ const normalizeVersion = async function (version) {

try {
return await nvexeca(versionA, 'node', { progress: true, dry: true })
} catch (error) {
throw wrapError(error, 'must be a valid Node.js version:')
} catch (cause) {
throw new Error('must be a valid Node.js version:\n', { cause })
}
}

Expand Down
9 changes: 4 additions & 5 deletions src/runners/node/handler/hooks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { wrapError } from '../../../error/wrap.js'
import { TasksRunError } from '../../common/error.js'

// Perform `beforeAll`, if defined
Expand All @@ -9,8 +8,8 @@ export const before = async function ({ task: { beforeAll }, inputs }) {

try {
await beforeAll(inputs)
} catch (error) {
throw wrapError(error, '', TasksRunError)
} catch (cause) {
throw new TasksRunError('', { cause })
}
}

Expand All @@ -22,7 +21,7 @@ export const after = async function ({ task: { afterAll }, inputs }) {

try {
await afterAll(inputs)
} catch (error) {
throw wrapError(error, '', TasksRunError)
} catch (cause) {
throw new TasksRunError('', { cause })
}
}
5 changes: 2 additions & 3 deletions src/runners/node/handler/measure/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { wrapError } from '../../../../error/wrap.js'
import { TasksRunError } from '../../../common/error.js'

import { performLoopsAsync } from './async.js'
Expand Down Expand Up @@ -32,7 +31,7 @@ export const measure = async function (
maxLoops,
})
return { measures }
} catch (error) {
throw wrapError(error, '', TasksRunError)
} catch (cause) {
throw new TasksRunError('', { cause })
}
}
11 changes: 4 additions & 7 deletions src/runners/node/handler/start/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { wrapError } from '../../../../error/wrap.js'
import { importJsNamed } from '../../../../utils/import.js'
import { TasksLoadError } from '../../../common/error.js'

Expand Down Expand Up @@ -27,12 +26,10 @@ export const start = async function (
const importFile = async function (taskPath) {
try {
return await importJsNamed(taskPath)
} catch (error) {
throw wrapError(
error,
`Could not import the tasks file ${taskPath}\n`,
TasksLoadError,
)
} catch (cause) {
throw new TasksLoadError(`Could not import the tasks file "${taskPath}"`, {
cause,
})
}
}

Expand Down
10 changes: 4 additions & 6 deletions src/runners/node/handler/start/require_config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { pathToFileURL } from 'url'

import { wrapError } from '../../../../error/wrap.js'
import { ConfigError } from '../../../common/error.js'

// Use the `require` configuration property
Expand All @@ -12,11 +11,10 @@ const useRequiredModule = async function (requiredModule) {
try {
// eslint-disable-next-line import/no-dynamic-require
await import(pathToFileURL(requiredModule))
} catch (error) {
throw wrapError(
error,
`Configuration property "runner.require" with value "${requiredModule}" could not be imported.\n`,
ConfigError,
} catch (cause) {
throw new ConfigError(
`Configuration property "runner.require" with value "${requiredModule}" could not be imported.`,
{ cause },
)
}
}
Expand Down

0 comments on commit 8095ed4

Please sign in to comment.