message.js
This file
Path
lib/return/message.js
156 lines · 4.7 KiB · JavaScript
Last changed by
Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
History of this fileTake it
The walk stopped after 200 commits: lines older than 9f65b86 point at it.
Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 1why, one step back1import {inspect} from 'node:util';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 2–6why, one step back2import stripFinalNewline from 'strip-final-newline';3import {isUint8Array, uint8ArrayToString} from '../utils/uint-array.js';4import {fixCwdError} from '../arguments/cwd.js';5import {escapeLines} from '../arguments/escape.js';6import {getMaxBufferMessage} from '../io/max-buffer.js';Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 7why, one step back7import {getSignalDescription} from '../terminate/signal.js';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 8–13why, one step back8import {DiscardedError, isExecaError} from './final-error.js';910// Computes `error.message`, `error.shortMessage` and `error.originalMessage`11export const createMessages = ({12 stdio,13 all,Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 14why, one step back14 ipcOutput,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 15–21why, one step back15 originalError,16 signal,17 signalDescription,18 exitCode,19 escapedCommand,20 timedOut,21 isCanceled,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 22why, one step back22 isGracefullyCanceled,100% test coverage (#1018)ehmickybefore adoption9f65b86
line 23why, one step back23 isMaxBuffer,Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 24–26why, one step back24 isForcefullyTerminated,25 forceKillAfterDelay,26 killSignal,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 27–30why, one step back27 maxBuffer,28 timeout,29 cwd,30}) => {Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
line 31why, one step back31 const errorCode = getErrorCode(originalError);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 32–42why, one step back32 const prefix = getErrorPrefix({33 originalError,34 timedOut,35 timeout,36 isMaxBuffer,37 maxBuffer,38 errorCode,39 signal,40 signalDescription,41 exitCode,42 isCanceled,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 43why, one step back43 isGracefullyCanceled,Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 44–46why, one step back44 isForcefullyTerminated,45 forceKillAfterDelay,46 killSignal,100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 47–51why, one step back47 });48 const originalMessage = getOriginalMessage(originalError, cwd);49 const suffix = originalMessage === undefined ? '' : `\n${originalMessage}`;50 const shortMessage = `${prefix}: ${escapedCommand}${suffix}`;51 const messageStdio = all === undefined ? [stdio[2], stdio[1]] : [all];Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 52–55why, one step back52 const message = [53 shortMessage,54 ...messageStdio,55 ...stdio.slice(3),Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360
line 56why, one step back56 ipcOutput.map(ipcMessage => serializeIpcMessage(ipcMessage)).join('\n'),Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
line 57why, one step back57 ]100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 58–63why, one step back58 .map(messagePart => escapeLines(stripFinalNewline(serializeMessagePart(messagePart))))59 .filter(Boolean)60 .join('\n\n');61 return {originalMessage, shortMessage, message};62};63Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
lines 64–66why, one step back64// `DOMException`, e.g. the default `cancelSignal` reason, has a legacy numeric `code`, which is not an error code like `ENOENT`65export const getErrorCode = error => typeof error?.code === 'string' ? error.code : undefined;66Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 67–77why, one step back67const getErrorPrefix = ({68 originalError,69 timedOut,70 timeout,71 isMaxBuffer,72 maxBuffer,73 errorCode,74 signal,75 signalDescription,76 exitCode,77 isCanceled,Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
line 78why, one step back78 isGracefullyCanceled,Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 79–84why, one step back79 isForcefullyTerminated,80 forceKillAfterDelay,81 killSignal,82}) => {83 const forcefulSuffix = getForcefulSuffix(isForcefullyTerminated, forceKillAfterDelay);84100% test coverage (#1018)ehmickybefore adoption9f65b86
line 85why, one step back85 if (timedOut) {Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 86why, one step back86 return `Command timed out after ${timeout} milliseconds${forcefulSuffix}`;Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5
lines 87–96why, one step back87 }8889 if (isGracefullyCanceled) {90 if (signal === undefined) {91 return `Command was gracefully canceled with exit code ${exitCode}`;92 }9394 return isForcefullyTerminated95 ? `Command was gracefully canceled${forcefulSuffix}`96 : `Command was gracefully canceled with ${signal} (${signalDescription})`;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 97–99why, one step back97 }9899 if (isCanceled) {Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 100why, one step back100 return `Command was canceled${forcefulSuffix}`;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 101–103why, one step back101 }102103 if (isMaxBuffer) {Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 104why, one step back104 return `${getMaxBufferMessage(originalError, maxBuffer)}${forcefulSuffix}`;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 105–107why, one step back105 }106107 if (errorCode !== undefined) {Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
line 108why, one step back108 return `Command failed with ${errorCode}${forcefulSuffix}`;100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 109–110why, one step back109 }110Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 111–114why, one step back111 if (isForcefullyTerminated) {112 return `Command was killed with ${killSignal} (${getSignalDescription(killSignal)})${forcefulSuffix}`;113 }114100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 115–118why, one step back115 if (signal !== undefined) {116 return `Command was killed with ${signal} (${signalDescription})`;117 }118TweaksSindre Sorhusbefore adoption698a1b8
line 119why, one step back119 return exitCode === undefined ? 'Command failed' : `Command failed with exit code ${exitCode}`;100% test coverage (#1018)ehmickybefore adoption9f65b86
line 120why, one step back120};Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d
lines 121–124why, one step back121122const getForcefulSuffix = (isForcefullyTerminated, forceKillAfterDelay) => isForcefullyTerminated123 ? ` and was forcefully terminated after ${forceKillAfterDelay} milliseconds`124 : '';100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 125–130why, one step back125126const getOriginalMessage = (originalError, cwd) => {127 if (originalError instanceof DiscardedError) {128 return;129 }130Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
line 131why, one step back131 // An `ExecaError` has no `originalMessage` when it was not caused by another error, e.g. with a non-zero exit code100% test coverage (#1018)ehmickybefore adoption9f65b86
line 132why, one step back132 const originalMessage = isExecaError(originalError)Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
line 133why, one step back133 ? originalError.originalMessage ?? ''100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 134–137why, one step back134 : String(originalError?.message ?? originalError);135 const escapedOriginalMessage = escapeLines(fixCwdError(originalMessage, cwd));136 return escapedOriginalMessage === '' ? undefined : escapedOriginalMessage;137};Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de
lines 138–141why, one step back138139const serializeIpcMessage = ipcMessage => typeof ipcMessage === 'string'140 ? ipcMessage141 : inspect(ipcMessage);100% test coverage (#1018)ehmickybefore adoption9f65b86
line 142why, one step back142Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
line 143why, one step back143// With the `lines` option, empty lines are kept. Only objects are omitted.100% test coverage (#1018)ehmickybefore adoption9f65b86
line 144why, one step back144const serializeMessagePart = messagePart => Array.isArray(messagePart)Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
line 145why, one step back145 ? messagePart.filter(messageItem => isTextItem(messageItem)).map(messageItem => stripFinalNewline(serializeMessageItem(messageItem))).join('\n')100% test coverage (#1018)ehmickybefore adoption9f65b86
line 146why, one step back146 : serializeMessageItem(messagePart);Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff
lines 147–148why, one step back147148const isTextItem = messageItem => typeof messageItem === 'string' || isUint8Array(messageItem);100% test coverage (#1018)ehmickybefore adoption9f65b86
lines 149–154why, one step back149150const serializeMessageItem = messageItem => {151 if (typeof messageItem === 'string') {152 return messageItem;153 }154TweaksSindre Sorhusbefore adoption698a1b8
line 155why, one step back155 return isUint8Array(messageItem) ? uint8ArrayToString(messageItem) : '';100% test coverage (#1018)ehmickybefore adoption9f65b86
line 156why, one step back156};