Skip to content
Myr

Simplify logic related to error handling (#819)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent be7eb85 · Browse files · 2 files+11 added−13 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+11 added−13 removed

  • other:+11 added−13 removed

2 files+11 added−13 removed

modified:lib/error.js+8 added-5 removed
modified:lib/kill.js+3 added-8 removed
1import {addAbortListener} from 'node:events';2import os from 'node:os';3import {setTimeout} from 'node:timers/promises';4import {onExit} from 'signal-exit';5import {normalizeExitPayload, getErrorPrefix} from './error.js';5import {isErrorInstance} from './clone.js';6import {DiscardedError} from './error.js';78const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;910// Monkey-patches `childProcess.kill()` to add `forceKillAfterDelay` behavior and `.kill(error)`
60 unmodified lines
71export const waitForSuccessfulExit = async exitPromise => {72 const [exitCode, signal] = await exitPromise;7374 if (!isProcessErrorExit(exitCode, signal) && isFailedExit(exitCode, signal)) {75 const message = getErrorPrefix(normalizeExitPayload(exitCode, signal));76 throw setDiscardedError(new Error(message));75 throw new DiscardedError();76 }7778 return [exitCode, signal];79};
4 unmodified lines
84const killAfterTimeout = async (spawned, timeout, context, {signal}) => {85 await setTimeout(timeout, undefined, {signal});86 context.timedOut = true;87 spawned.kill();89 const message = getErrorPrefix({timedOut: true, timeout});90 throw setDiscardedError(new Error(message));88 throw new DiscardedError();89};9091// `timeout` option handling92export const throwOnTimeout = (spawned, timeout, context, controller) => timeout === 0 || timeout === undefined
5 unmodified lines
98 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);99 }100};101104// Indicates that the error is used only to interrupt control flow, but not in the return value105const setDiscardedError = error => Object.assign(error, {discarded: true});106102// `cleanup` option handling103export const cleanupOnExit = (spawned, {cleanup, detached}, {signal}) => {104 if (!cleanup || detached) {105 return;
9 unmodified lines