error.js
This file
Path
lib/error.js
188 lines · 4.5 KiB · JavaScript
Last changed by
Avoid code duplication with return value's logic (#829)ehmickybefore adoption100f04f
History of this fileTake it
The walk stopped after 200 commits: lines older than 09df476 point at it.
Meta tweaksSindre Sorhusbefore adoption09df476
line 1why, one step back1import {signalsByName} from 'human-signals';Improve `error.message` (#705)ehmickybefore adoption67b476b
line 2why, one step back2import stripFinalNewline from 'strip-final-newline';Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 3why, one step back3import {isBinary, binaryToString} from './stdio/utils.js';Improve `cwd` option (#803)ehmickybefore adoption2fd9351
line 4why, one step back4import {fixCwdError} from './cwd.js';Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890
line 5why, one step back5import {getFinalError, isPreviousError} from './clone.js';Meta tweaksSindre Sorhusbefore adoption09df476
line 6why, one step back6Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890
lines 7–32why, one step back7const createMessages = ({8 stdio,9 all,10 error,11 signal,12 signalDescription,13 exitCode,14 command,15 timedOut,16 isCanceled,17 timeout,18 cwd,19}) => {20 const errorCode = error?.code;21 const prefix = getErrorPrefix({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled});22 const originalMessage = getOriginalMessage(error, cwd);23 const newline = originalMessage === '' ? '' : '\n';24 const shortMessage = `${prefix}: ${command}${newline}${originalMessage}`;25 const messageStdio = all === undefined ? [stdio[2], stdio[1]] : [all];26 const message = [shortMessage, ...messageStdio, ...stdio.slice(3)]27 .map(messagePart => stripFinalNewline(serializeMessagePart(messagePart)))28 .filter(Boolean)29 .join('\n\n');30 return {originalMessage, shortMessage, message};31};32Simplify logic related to error handling (#819)ehmickybefore adoption268ad95
line 33why, one step back33const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => {Meta tweaksSindre Sorhusbefore adoption09df476
line 34why, one step back34 if (timedOut) {Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 35why, one step back35 return `Command timed out after ${timeout} milliseconds`;Meta tweaksSindre Sorhusbefore adoption09df476
lines 36–38why, one step back36 }3738 if (isCanceled) {Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 39why, one step back39 return 'Command was canceled';Meta tweaksSindre Sorhusbefore adoption09df476
lines 40–42why, one step back40 }4142 if (errorCode !== undefined) {Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 43why, one step back43 return `Command failed with ${errorCode}`;Meta tweaksSindre Sorhusbefore adoption09df476
lines 44–46why, one step back44 }4546 if (signal !== undefined) {Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 47why, one step back47 return `Command was killed with ${signal} (${signalDescription})`;Meta tweaksSindre Sorhusbefore adoption09df476
lines 48–50why, one step back48 }4950 if (exitCode !== undefined) {Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 51why, one step back51 return `Command failed with exit code ${exitCode}`;Meta tweaksSindre Sorhusbefore adoption09df476
lines 52–53why, one step back52 }53Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 54why, one step back54 return 'Command failed';Meta tweaksSindre Sorhusbefore adoption09df476
lines 55–56why, one step back55};56Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890
line 57why, one step back57const getOriginalMessage = (error, cwd) => {Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 58why, one step back58 if (error === undefined) {Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890
line 59why, one step back59 return '';Improve process failure logic (#807)ehmickybefore adoption8ff263c
lines 60–61why, one step back60 }61Simplify logic related to error handling (#819)ehmickybefore adoption268ad95
lines 62–63why, one step back62 const originalMessage = isPreviousError(error) ? error.originalMessage : String(error?.message ?? error);63 return fixCwdError(originalMessage, cwd);Improve process failure logic (#807)ehmickybefore adoption8ff263c
lines 64–65why, one step back64};65Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 66–72why, one step back66const serializeMessagePart = messagePart => Array.isArray(messagePart)67 ? messagePart.map(messageItem => serializeMessageItem(messageItem)).join('')68 : serializeMessageItem(messagePart);6970const serializeMessageItem = messageItem => {71 if (typeof messageItem === 'string') {72 return messageItem;Improve `error.message` (#705)ehmickybefore adoption67b476b
line 73why, one step back73 }Fix `error.message` when using `encoding: 'buffer'` (#701)ehmickybefore adoptionb8c1f55
line 74why, one step back74Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 75–76why, one step back75 if (isBinary(messageItem)) {76 return binaryToString(messageItem);Improve `error.message` (#705)ehmickybefore adoption67b476b
lines 77–81why, one step back77 }7879 return '';80};81Simplify logic related to error handling (#819)ehmickybefore adoption268ad95
lines 82–84why, one step back82// Indicates that the error is used only to interrupt control flow, but not in the return value83export class DiscardedError extends Error {}84Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890
lines 85–86why, one step back85// `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`.86// We normalize them to `undefined`Simplify logic related to error handling (#819)ehmickybefore adoption268ad95
line 87why, one step back87const normalizeExitPayload = (rawExitCode, rawSignal) => {Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890
lines 88–93why, one step back88 const exitCode = rawExitCode === null ? undefined : rawExitCode;89 const signal = rawSignal === null ? undefined : rawSignal;90 const signalDescription = signal === undefined ? undefined : signalsByName[rawSignal].description;91 return {exitCode, signal, signalDescription};92};93Meta tweaksSindre Sorhusbefore adoption09df476
line 94why, one step back94export const makeError = ({Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 95why, one step back95 error: rawError,Meta tweaksSindre Sorhusbefore adoption09df476
lines 96–99why, one step back96 command,97 escapedCommand,98 timedOut,99 isCanceled,Avoid code duplication with return value's logic (#829)ehmickybefore adoption100f04f
lines 100–103why, one step back100 exitCode: rawExitCode,101 signal: rawSignal,102 stdio,103 all,Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 104why, one step back104 options: {timeoutDuration, timeout = timeoutDuration, cwd},Meta tweaksSindre Sorhusbefore adoption09df476
line 105why, one step back105}) => {Simplify logic related to error handling (#819)ehmickybefore adoption268ad95
line 106why, one step back106 const initialError = rawError instanceof DiscardedError ? undefined : rawError;Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 107why, one step back107 const {exitCode, signal, signalDescription} = normalizeExitPayload(rawExitCode, rawSignal);Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890
lines 108–121why, one step back108 const {originalMessage, shortMessage, message} = createMessages({109 stdio,110 all,111 error: initialError,112 signal,113 signalDescription,114 exitCode,115 command,116 timedOut,117 isCanceled,118 timeout,119 cwd,120 });121 const error = getFinalError(initialError, message);Meta tweaksSindre Sorhusbefore adoption09df476
lines 122–123why, one step back122123 error.shortMessage = shortMessage;Improve copying previous Execa errors (#806)ehmickybefore adoption121a052
line 124why, one step back124 error.originalMessage = originalMessage;Meta tweaksSindre Sorhusbefore adoption09df476
lines 125–126why, one step back125 error.command = command;126 error.escapedCommand = escapedCommand;Re-order return value properties (#801)ehmickybefore adoption72836f3
lines 127–129why, one step back127 error.cwd = cwd;128129 error.failed = true;Do not prevent process normal exit (#809)ehmickybefore adoption7ddf0a0
line 130why, one step back130 error.timedOut = timedOut;Re-order return value properties (#801)ehmickybefore adoption72836f3
lines 131–132why, one step back131 error.isCanceled = isCanceled;132 error.isTerminated = signal !== undefined;Meta tweaksSindre Sorhusbefore adoption09df476
lines 133–135why, one step back133 error.exitCode = exitCode;134 error.signal = signal;135 error.signalDescription = signalDescription;Re-order return value properties (#801)ehmickybefore adoption72836f3
line 136why, one step back136Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
lines 137–138why, one step back137 error.stdout = stdio[1];138 error.stderr = stdio[2];Meta tweaksSindre Sorhusbefore adoption09df476
lines 139–143why, one step back139140 if (all !== undefined) {141 error.all = all;142 }143Re-order return value properties (#801)ehmickybefore adoption72836f3
lines 144–145why, one step back144 error.stdio = stdio;145Meta tweaksSindre Sorhusbefore adoption09df476
lines 146–151why, one step back146 if ('bufferedData' in error) {147 delete error.bufferedData;148 }149150 return error;151};Avoid code duplication with return value's logic (#829)ehmickybefore adoption100f04f
lines 152–188why, one step back152153export const makeEarlyError = ({154 error,155 command,156 escapedCommand,157 stdioStreamsGroups,158 options,159}) => makeError({160 error,161 command,162 escapedCommand,163 timedOut: false,164 isCanceled: false,165 stdio: Array.from({length: stdioStreamsGroups.length}),166 options,167});168169export const makeSuccessResult = ({170 command,171 escapedCommand,172 stdio,173 all,174 options: {cwd},175}) => ({176 command,177 escapedCommand,178 cwd,179 failed: false,180 timedOut: false,181 isCanceled: false,182 isTerminated: false,183 exitCode: 0,184 stdout: stdio[1],185 stderr: stdio[2],186 all,187 stdio,188});