Skip to content
Myr

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 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

Simplify logic related to error handling (#819)ehmickybefore adoption268ad95

line 33why, one step back
33const 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

Simplify logic related to error handling (#819)ehmickybefore adoption268ad95

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

Simplify logic related to error handling (#819)ehmickybefore adoption268ad95

lines 82–84why, one step back
82// Indicates that the error is used only to interrupt control flow, but not in the return value
83export class DiscardedError extends Error {}84

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

lines 85–86why, one step back
85// `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 back
87const normalizeExitPayload = (rawExitCode, rawSignal) => {

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

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

Meta tweaksSindre Sorhusbefore adoption09df476

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

Improve process failure logic (#807)ehmickybefore adoption8ff263c

line 95why, one step back
95 error: rawError,

Meta tweaksSindre Sorhusbefore adoption09df476

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

Avoid code duplication with return value's logic (#829)ehmickybefore adoption100f04f

lines 100–103why, one step back
100 exitCode: rawExitCode,
101 signal: rawSignal,102 stdio,103 all,

Improve process failure logic (#807)ehmickybefore adoption8ff263c

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

Meta tweaksSindre Sorhusbefore adoption09df476

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

Simplify logic related to error handling (#819)ehmickybefore adoption268ad95

line 106why, one step back
106 const initialError = rawError instanceof DiscardedError ? undefined : rawError;

Improve process failure logic (#807)ehmickybefore adoption8ff263c

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

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

lines 108–121why, one step back
108 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 back
122
123 error.shortMessage = shortMessage;

Improve copying previous Execa errors (#806)ehmickybefore adoption121a052

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

Meta tweaksSindre Sorhusbefore adoption09df476

lines 125–126why, one step back
125 error.command = command;
126 error.escapedCommand = escapedCommand;

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

lines 127–129why, one step back
127 error.cwd = cwd;
128129 error.failed = true;

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

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

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

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

Meta tweaksSindre Sorhusbefore adoption09df476

lines 133–135why, one step back
133 error.exitCode = exitCode;
134 error.signal = signal;135 error.signalDescription = signalDescription;

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

line 136why, one step back
136

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

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

Meta tweaksSindre Sorhusbefore adoption09df476

lines 139–143why, one step back
139
140 if (all !== undefined) {141 error.all = all;142 }143

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

lines 144–145why, one step back
144 error.stdio = stdio;
145

Meta tweaksSindre Sorhusbefore adoption09df476

lines 146–151why, one step back
146 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 back
152
153export 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});