Skip to content
Myr

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 file

Take 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 back
1import {inspect} from 'node:util';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 2–6why, one step back
2import 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 back
7import {getSignalDescription} from '../terminate/signal.js';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 8–13why, one step back
8import {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 back
14 ipcOutput,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 15–21why, one step back
15 originalError,
16 signal,17 signalDescription,18 exitCode,19 escapedCommand,20 timedOut,21 isCanceled,

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

line 22why, one step back
22 isGracefullyCanceled,

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 23why, one step back
23 isMaxBuffer,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 24–26why, one step back
24 isForcefullyTerminated,
25 forceKillAfterDelay,26 killSignal,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 27–30why, one step back
27 maxBuffer,
28 timeout,29 cwd,30}) => {

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

line 31why, one step back
31 const errorCode = getErrorCode(originalError);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 32–42why, one step back
32 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 back
43 isGracefullyCanceled,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 44–46why, one step back
44 isForcefullyTerminated,
45 forceKillAfterDelay,46 killSignal,

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 47–51why, one step back
47 });
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 back
52 const message = [
53 shortMessage,54 ...messageStdio,55 ...stdio.slice(3),

Rename `result.ipc` to `result.ipcOutput` (#1075)ehmickybefore adoptionae9b360

line 56why, one step back
56 ipcOutput.map(ipcMessage => serializeIpcMessage(ipcMessage)).join('\n'),

Add `result.ipc`/`error.ipc` (#1067)ehmickybefore adoption3ab34de

line 57why, one step back
57 ]

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 58–63why, one step back
58 .map(messagePart => escapeLines(stripFinalNewline(serializeMessagePart(messagePart))))
59 .filter(Boolean)60 .join('\n\n');61 return {originalMessage, shortMessage, message};62};63

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

lines 64–66why, one step back
64// `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;66

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 67–77why, one step back
67const 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 back
78 isGracefullyCanceled,

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 79–84why, one step back
79 isForcefullyTerminated,
80 forceKillAfterDelay,81 killSignal,82}) => {83 const forcefulSuffix = getForcefulSuffix(isForcefullyTerminated, forceKillAfterDelay);84

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 85why, one step back
85 if (timedOut) {

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 86why, one step back
86 return `Command timed out after ${timeout} milliseconds${forcefulSuffix}`;

Add `gracefulCancel` option (#1109)ehmickybefore adoptiond8190e5

lines 87–96why, one step back
87 }
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 back
97 }
9899 if (isCanceled) {

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 100why, one step back
100 return `Command was canceled${forcefulSuffix}`;

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 101–103why, one step back
101 }
102103 if (isMaxBuffer) {

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 104why, one step back
104 return `${getMaxBufferMessage(originalError, maxBuffer)}${forcefulSuffix}`;

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 105–107why, one step back
105 }
106107 if (errorCode !== undefined) {

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

line 108why, one step back
108 return `Command failed with ${errorCode}${forcefulSuffix}`;

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 109–110why, one step back
109 }
110

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 111–114why, one step back
111 if (isForcefullyTerminated) {
112 return `Command was killed with ${killSignal} (${getSignalDescription(killSignal)})${forcefulSuffix}`;113 }114

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 115–118why, one step back
115 if (signal !== undefined) {
116 return `Command was killed with ${signal} (${signalDescription})`;117 }118

TweaksSindre Sorhusbefore adoption698a1b8

line 119why, one step back
119 return exitCode === undefined ? 'Command failed' : `Command failed with exit code ${exitCode}`;

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 120why, one step back
120};

Add `error.isForcefullyTerminated` (#1111)ehmickybefore adoptione69985d

lines 121–124why, one step back
121
122const getForcefulSuffix = (isForcefullyTerminated, forceKillAfterDelay) => isForcefullyTerminated123 ? ` and was forcefully terminated after ${forceKillAfterDelay} milliseconds`124 : '';

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 125–130why, one step back
125
126const getOriginalMessage = (originalError, cwd) => {127 if (originalError instanceof DiscardedError) {128 return;129 }130

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

line 131why, one step back
131 // An `ExecaError` has no `originalMessage` when it was not caused by another error, e.g. with a non-zero exit code

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 132why, one step back
132 const originalMessage = isExecaError(originalError)

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

line 133why, one step back
133 ? originalError.originalMessage ?? ''

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 134–137why, one step back
134 : 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 back
138
139const serializeIpcMessage = ipcMessage => typeof ipcMessage === 'string'140 ? ipcMessage141 : inspect(ipcMessage);

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 142why, one step back
142

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

line 143why, one step back
143// With the `lines` option, empty lines are kept. Only objects are omitted.

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 144why, one step back
144const serializeMessagePart = messagePart => Array.isArray(messagePart)

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

line 145why, one step back
145 ? messagePart.filter(messageItem => isTextItem(messageItem)).map(messageItem => stripFinalNewline(serializeMessageItem(messageItem))).join('\n')

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 146why, one step back
146 : serializeMessageItem(messagePart);

Fix errors, early errors and `result.all` edge casesSindre Sorhusbefore adoptionc4262ff

lines 147–148why, one step back
147
148const isTextItem = messageItem => typeof messageItem === 'string' || isUint8Array(messageItem);

100% test coverage (#1018)ehmickybefore adoption9f65b86

lines 149–154why, one step back
149
150const serializeMessageItem = messageItem => {151 if (typeof messageItem === 'string') {152 return messageItem;153 }154

TweaksSindre Sorhusbefore adoption698a1b8

line 155why, one step back
155 return isUint8Array(messageItem) ? uint8ArrayToString(messageItem) : '';

100% test coverage (#1018)ehmickybefore adoption9f65b86

line 156why, one step back
156};