error.js
This file
Path
lib/error.js
148 lines · 3.8 KiB · JavaScript
Last changed by
Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890
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};32Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 33why, one step back33export const 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 }61Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890
lines 62–63why, one step back62 const originalErrorMessage = isPreviousError(error) ? error.originalMessage : String(error?.message ?? error);63 return fixCwdError(originalErrorMessage, 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};81Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890
lines 82–90why, one step back82// `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`.83// We normalize them to `undefined`84export const normalizeExitPayload = (rawExitCode, rawSignal) => {85 const exitCode = rawExitCode === null ? undefined : rawExitCode;86 const signal = rawSignal === null ? undefined : rawSignal;87 const signalDescription = signal === undefined ? undefined : signalsByName[rawSignal].description;88 return {exitCode, signal, signalDescription};89};90Meta tweaksSindre Sorhusbefore adoption09df476
line 91why, one step back91export const makeError = ({Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 92why, one step back92 stdio,Meta tweaksSindre Sorhusbefore adoption09df476
line 93why, one step back93 all,Improve process failure logic (#807)ehmickybefore adoption8ff263c
lines 94–96why, one step back94 error: rawError,95 signal: rawSignal,96 exitCode: rawExitCode,Meta tweaksSindre Sorhusbefore adoption09df476
lines 97–100why, one step back97 command,98 escapedCommand,99 timedOut,100 isCanceled,Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 101why, one step back101 options: {timeoutDuration, timeout = timeoutDuration, cwd},Meta tweaksSindre Sorhusbefore adoption09df476
line 102why, one step back102}) => {Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890
line 103why, one step back103 const initialError = rawError?.discarded ? undefined : rawError;Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 104why, one step back104 const {exitCode, signal, signalDescription} = normalizeExitPayload(rawExitCode, rawSignal);Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890
lines 105–118why, one step back105 const {originalMessage, shortMessage, message} = createMessages({106 stdio,107 all,108 error: initialError,109 signal,110 signalDescription,111 exitCode,112 command,113 timedOut,114 isCanceled,115 timeout,116 cwd,117 });118 const error = getFinalError(initialError, message);Meta tweaksSindre Sorhusbefore adoption09df476
lines 119–120why, one step back119120 error.shortMessage = shortMessage;Improve copying previous Execa errors (#806)ehmickybefore adoption121a052
line 121why, one step back121 error.originalMessage = originalMessage;Meta tweaksSindre Sorhusbefore adoption09df476
lines 122–123why, one step back122 error.command = command;123 error.escapedCommand = escapedCommand;Re-order return value properties (#801)ehmickybefore adoption72836f3
lines 124–126why, one step back124 error.cwd = cwd;125126 error.failed = true;Do not prevent process normal exit (#809)ehmickybefore adoption7ddf0a0
line 127why, one step back127 error.timedOut = timedOut;Re-order return value properties (#801)ehmickybefore adoption72836f3
lines 128–129why, one step back128 error.isCanceled = isCanceled;129 error.isTerminated = signal !== undefined;Meta tweaksSindre Sorhusbefore adoption09df476
lines 130–132why, one step back130 error.exitCode = exitCode;131 error.signal = signal;132 error.signalDescription = signalDescription;Re-order return value properties (#801)ehmickybefore adoption72836f3
line 133why, one step back133Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
lines 134–135why, one step back134 error.stdout = stdio[1];135 error.stderr = stdio[2];Meta tweaksSindre Sorhusbefore adoption09df476
lines 136–140why, one step back136137 if (all !== undefined) {138 error.all = all;139 }140Re-order return value properties (#801)ehmickybefore adoption72836f3
lines 141–142why, one step back141 error.stdio = stdio;142Meta tweaksSindre Sorhusbefore adoption09df476
lines 143–148why, one step back143 if ('bufferedData' in error) {144 delete error.bufferedData;145 }146147 return error;148};