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
Provenance
- Commit
268ad95- Tree
f051251- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
be7eb85
2 files changed+11 added−13 removed
modified:lib/error.js+8 added-5 removed
28 unmodified lines2930313233333435363720 unmodified lines58596061626362636465666710 unmodified lines78798081828384858684878889909110 unmodified lines10210310410510310610710810911041 unmodified lines28 unmodified lines .join('\n\n'); return {originalMessage, shortMessage, message};};
export const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => {const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => { if (timedOut) { return `Command timed out after ${timeout} milliseconds`; }
20 unmodified lines if (error === undefined) { return ''; }
const originalErrorMessage = isPreviousError(error) ? error.originalMessage : String(error?.message ?? error); return fixCwdError(originalErrorMessage, cwd); const originalMessage = isPreviousError(error) ? error.originalMessage : String(error?.message ?? error); return fixCwdError(originalMessage, cwd);};
const serializeMessagePart = messagePart => Array.isArray(messagePart) ? messagePart.map(messageItem => serializeMessageItem(messageItem)).join('')10 unmodified lines
return '';};
// Indicates that the error is used only to interrupt control flow, but not in the return valueexport class DiscardedError extends Error {}
// `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`.// We normalize them to `undefined`export const normalizeExitPayload = (rawExitCode, rawSignal) => {const normalizeExitPayload = (rawExitCode, rawSignal) => { const exitCode = rawExitCode === null ? undefined : rawExitCode; const signal = rawSignal === null ? undefined : rawSignal; const signalDescription = signal === undefined ? undefined : signalsByName[rawSignal].description; return {exitCode, signal, signalDescription};10 unmodified lines timedOut, isCanceled, options: {timeoutDuration, timeout = timeoutDuration, cwd},}) => { const initialError = rawError?.discarded ? undefined : rawError; const initialError = rawError instanceof DiscardedError ? undefined : rawError; const {exitCode, signal, signalDescription} = normalizeExitPayload(rawExitCode, rawSignal); const {originalMessage, shortMessage, message} = createMessages({ stdio, all,41 unmodified linesmodified:lib/kill.js+3 added-8 removed
1 import {addAbortListener} from 'node:events';2 import os from 'node:os';3 import {setTimeout} from 'node:timers/promises';4 import {onExit} from 'signal-exit';5-import {normalizeExitPayload, getErrorPrefix} from './error.js';5 import {isErrorInstance} from './clone.js';6+import {DiscardedError} from './error.js';7 8 const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;9 10 // Monkey-patches `childProcess.kill()` to add `forceKillAfterDelay` behavior and `.kill(error)`60 unmodified lines71 export const waitForSuccessfulExit = async exitPromise => {72 const [exitCode, signal] = await exitPromise;73 74 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 }77 78 return [exitCode, signal];79 };4 unmodified lines84 const 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 };90 91 // `timeout` option handling92 export const throwOnTimeout = (spawned, timeout, context, controller) => timeout === 0 || timeout === undefined5 unmodified lines98 throw new TypeError(`Expected the \`timeout\` option to be a non-negative integer, got \`${timeout}\` (${typeof timeout})`);99 }100 };101 104-// Indicates that the error is used only to interrupt control flow, but not in the return value105-const setDiscardedError = error => Object.assign(error, {discarded: true});106-102 // `cleanup` option handling103 export const cleanupOnExit = (spawned, {cleanup, detached}, {signal}) => {104 if (!cleanup || detached) {105 return;9 unmodified lines