Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 09df476 point at it.

Meta tweaksSindre Sorhusbefore adoption09df476

line 1why, one step back
1import {signalsByName} from 'human-signals';

Improve `error.message` (#705)ehmickybefore adoption67b476b

line 2why, one step back
2import stripFinalNewline from 'strip-final-newline';

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

line 3why, one step back
3import {isBinary, binaryToString} from './stdio/utils.js';

Improve `cwd` option (#803)ehmickybefore adoption2fd9351

line 4why, one step back
4import {fixCwdError} from './cwd.js';

Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890

line 5why, one step back
5import {getFinalError, isPreviousError} from './clone.js';

Meta tweaksSindre Sorhusbefore adoption09df476

line 6why, one step back
6

Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890

lines 7–32why, one step back
7const 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};32

Improve process failure logic (#807)ehmickybefore adoption8ff263c

line 33why, one step back
33export const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => {

Meta tweaksSindre Sorhusbefore adoption09df476

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

Improve process failure logic (#807)ehmickybefore adoption8ff263c

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

Meta tweaksSindre Sorhusbefore adoption09df476

lines 36–38why, one step back
36 }
3738 if (isCanceled) {

Improve process failure logic (#807)ehmickybefore adoption8ff263c

line 39why, one step back
39 return 'Command was canceled';

Meta tweaksSindre Sorhusbefore adoption09df476

lines 40–42why, one step back
40 }
4142 if (errorCode !== undefined) {

Improve process failure logic (#807)ehmickybefore adoption8ff263c

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

Meta tweaksSindre Sorhusbefore adoption09df476

lines 44–46why, one step back
44 }
4546 if (signal !== undefined) {

Improve process failure logic (#807)ehmickybefore adoption8ff263c

line 47why, one step back
47 return `Command was killed with ${signal} (${signalDescription})`;

Meta tweaksSindre Sorhusbefore adoption09df476

lines 48–50why, one step back
48 }
4950 if (exitCode !== undefined) {

Improve process failure logic (#807)ehmickybefore adoption8ff263c

line 51why, one step back
51 return `Command failed with exit code ${exitCode}`;

Meta tweaksSindre Sorhusbefore adoption09df476

lines 52–53why, one step back
52 }
53

Improve process failure logic (#807)ehmickybefore adoption8ff263c

line 54why, one step back
54 return 'Command failed';

Meta tweaksSindre Sorhusbefore adoption09df476

lines 55–56why, one step back
55};
56

Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890

line 57why, one step back
57const getOriginalMessage = (error, cwd) => {

Improve process failure logic (#807)ehmickybefore adoption8ff263c

line 58why, one step back
58 if (error === undefined) {

Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890

line 59why, one step back
59 return '';

Improve process failure logic (#807)ehmickybefore adoption8ff263c

lines 60–61why, one step back
60 }
61

Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890

lines 62–63why, one step back
62 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 back
64};
65

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 66–72why, one step back
66const 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 back
73 }

Fix `error.message` when using `encoding: 'buffer'` (#701)ehmickybefore adoptionb8c1f55

line 74why, one step back
74

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 75–76why, one step back
75 if (isBinary(messageItem)) {
76 return binaryToString(messageItem);

Improve `error.message` (#705)ehmickybefore adoption67b476b

lines 77–81why, one step back
77 }
7879 return '';80};81

Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890

lines 82–90why, one step back
82// `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};90

Meta tweaksSindre Sorhusbefore adoption09df476

line 91why, one step back
91export const makeError = ({

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

line 92why, one step back
92 stdio,

Meta tweaksSindre Sorhusbefore adoption09df476

line 93why, one step back
93 all,

Improve process failure logic (#807)ehmickybefore adoption8ff263c

lines 94–96why, one step back
94 error: rawError,
95 signal: rawSignal,96 exitCode: rawExitCode,

Meta tweaksSindre Sorhusbefore adoption09df476

lines 97–100why, one step back
97 command,
98 escapedCommand,99 timedOut,100 isCanceled,

Improve process failure logic (#807)ehmickybefore adoption8ff263c

line 101why, one step back
101 options: {timeoutDuration, timeout = timeoutDuration, cwd},

Meta tweaksSindre Sorhusbefore adoption09df476

line 102why, one step back
102}) => {

Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890

line 103why, one step back
103 const initialError = rawError?.discarded ? undefined : rawError;

Improve process failure logic (#807)ehmickybefore adoption8ff263c

line 104why, one step back
104 const {exitCode, signal, signalDescription} = normalizeExitPayload(rawExitCode, rawSignal);

Fix `error.stack` with `stream.destroy(error)` (#814)ehmickybefore adoption07a9890

lines 105–118why, one step back
105 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 back
119
120 error.shortMessage = shortMessage;

Improve copying previous Execa errors (#806)ehmickybefore adoption121a052

line 121why, one step back
121 error.originalMessage = originalMessage;

Meta tweaksSindre Sorhusbefore adoption09df476

lines 122–123why, one step back
122 error.command = command;
123 error.escapedCommand = escapedCommand;

Re-order return value properties (#801)ehmickybefore adoption72836f3

lines 124–126why, one step back
124 error.cwd = cwd;
125126 error.failed = true;

Do not prevent process normal exit (#809)ehmickybefore adoption7ddf0a0

line 127why, one step back
127 error.timedOut = timedOut;

Re-order return value properties (#801)ehmickybefore adoption72836f3

lines 128–129why, one step back
128 error.isCanceled = isCanceled;
129 error.isTerminated = signal !== undefined;

Meta tweaksSindre Sorhusbefore adoption09df476

lines 130–132why, one step back
130 error.exitCode = exitCode;
131 error.signal = signal;132 error.signalDescription = signalDescription;

Re-order return value properties (#801)ehmickybefore adoption72836f3

line 133why, one step back
133

Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207

lines 134–135why, one step back
134 error.stdout = stdio[1];
135 error.stderr = stdio[2];

Meta tweaksSindre Sorhusbefore adoption09df476

lines 136–140why, one step back
136
137 if (all !== undefined) {138 error.all = all;139 }140

Re-order return value properties (#801)ehmickybefore adoption72836f3

lines 141–142why, one step back
141 error.stdio = stdio;
142

Meta tweaksSindre Sorhusbefore adoption09df476

lines 143–148why, one step back
143 if ('bufferedData' in error) {
144 delete error.bufferedData;145 }146147 return error;148};