Skip to content
Myr

error.js

This file

Path

lib/error.js

153 lines · 4.2 KiB · JavaScript

Last changed by

Improve process failure logic (#807)ehmickybefore adoption8ff263c

History of this file

Take it

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

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

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

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

line 5why, one step back
5

Improve process failure logic (#807)ehmickybefore adoption8ff263c

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

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

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

Improve process failure logic (#807)ehmickybefore adoption8ff263c

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

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

lines 9–11why, one step back
9 }
1011 if (isCanceled) {

Improve process failure logic (#807)ehmickybefore adoption8ff263c

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

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

lines 13–15why, one step back
13 }
1415 if (errorCode !== undefined) {

Improve process failure logic (#807)ehmickybefore adoption8ff263c

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

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

lines 17–19why, one step back
17 }
1819 if (signal !== undefined) {

Improve process failure logic (#807)ehmickybefore adoption8ff263c

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

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

lines 21–23why, one step back
21 }
2223 if (exitCode !== undefined) {

Improve process failure logic (#807)ehmickybefore adoption8ff263c

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

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

lines 25–26why, one step back
25 }
26

Improve process failure logic (#807)ehmickybefore adoption8ff263c

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

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

lines 28–29why, one step back
28};
29

Improve process failure logic (#807)ehmickybefore adoption8ff263c

lines 30–49why, one step back
30const getErrorSuffix = (error, cwd) => {
31 if (error === undefined) {32 return {originalMessage: '', suffix: ''};33 }3435 const originalErrorMessage = previousErrors.has(error) ? error.originalMessage : String(error?.message ?? error);36 const originalMessage = fixCwdError(originalErrorMessage, cwd);37 const suffix = `\n${originalMessage}`;38 return {originalMessage, suffix};39};4041// `signal` and `exitCode` emitted on `spawned.on('exit')` event can be `null`.42// We normalize them to `undefined`43export const normalizeExitPayload = (rawExitCode, rawSignal) => {44 const exitCode = rawExitCode === null ? undefined : rawExitCode;45 const signal = rawSignal === null ? undefined : rawSignal;46 const signalDescription = signal === undefined ? undefined : signalsByName[rawSignal].description;47 return {exitCode, signal, signalDescription};48};49

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

lines 50–56why, one step back
50const serializeMessagePart = messagePart => Array.isArray(messagePart)
51 ? messagePart.map(messageItem => serializeMessageItem(messageItem)).join('')52 : serializeMessageItem(messagePart);5354const serializeMessageItem = messageItem => {55 if (typeof messageItem === 'string') {56 return messageItem;

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

line 57why, one step back
57 }

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

line 58why, one step back
58

Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce

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

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

lines 61–65why, one step back
61 }
6263 return '';64};65

Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880

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

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

line 67why, one step back
67 stdio,

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

line 68why, one step back
68 all,

Improve process failure logic (#807)ehmickybefore adoption8ff263c

lines 69–71why, one step back
69 error: rawError,
70 signal: rawSignal,71 exitCode: rawExitCode,

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

line 72why, one step back
72 command,

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

line 73why, one step back
73 escapedCommand,

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

lines 74–75why, one step back
74 timedOut,
75 isCanceled,

Improve process failure logic (#807)ehmickybefore adoption8ff263c

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

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

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

Improve process failure logic (#807)ehmickybefore adoption8ff263c

lines 78–79why, one step back
78 let error = rawError?.discarded ? undefined : rawError;
79 const {exitCode, signal, signalDescription} = normalizeExitPayload(rawExitCode, rawSignal);

Fix return value being re-used between multiple calls (#796)ehmickybefore adoption7710440

line 80why, one step back
80 const errorCode = error?.code;

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

line 81why, one step back
81 const prefix = getErrorPrefix({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled});

Improve process failure logic (#807)ehmickybefore adoption8ff263c

lines 82–83why, one step back
82 const {originalMessage, suffix} = getErrorSuffix(error, cwd);
83 const shortMessage = `${prefix}: ${command}${suffix}`;

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

lines 84–86why, one step back
84 const messageStdio = all === undefined ? [stdio[2], stdio[1]] : [all];
85 const message = [shortMessage, ...messageStdio, ...stdio.slice(3)]86 .map(messagePart => stripFinalNewline(serializeMessagePart(messagePart)))

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

line 87why, one step back
87 .filter(Boolean)

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

line 88why, one step back
88 .join('\n\n');

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

line 89why, one step back
89

Improve copying previous Execa errors (#806)ehmickybefore adoption121a052

line 90why, one step back
90 if (Object.prototype.toString.call(error) !== '[object Error]') {

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

line 91why, one step back
91 error = new Error(message);

Improve copying previous Execa errors (#806)ehmickybefore adoption121a052

lines 92–95why, one step back
92 } else if (previousErrors.has(error)) {
93 const newError = new Error(message);94 copyErrorProperties(newError, error);95 error = newError;

Fix return value being re-used between multiple calls (#796)ehmickybefore adoption7710440

lines 96–97why, one step back
96 } else {
97 error.message = message;

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

lines 98–99why, one step back
98 }
99

Improve copying previous Execa errors (#806)ehmickybefore adoption121a052

lines 100–101why, one step back
100 previousErrors.add(error);
101

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

line 102why, one step back
102 error.shortMessage = shortMessage;

Improve copying previous Execa errors (#806)ehmickybefore adoption121a052

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

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

line 104why, one step back
104 error.command = command;

Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc

line 105why, one step back
105 error.escapedCommand = escapedCommand;

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

lines 106–111why, one step back
106 error.cwd = cwd;
107108 error.failed = true;109 error.timedOut = Boolean(timedOut);110 error.isCanceled = isCanceled;111 error.isTerminated = signal !== undefined;

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

lines 112–114why, one step back
112 error.exitCode = exitCode;
113 error.signal = signal;114 error.signalDescription = signalDescription;

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

line 115why, one step back
115

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

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

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

lines 118–122why, one step back
118
119 if (all !== undefined) {120 error.all = all;121 }122

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

lines 123–124why, one step back
123 error.stdio = stdio;
124

Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be

lines 125–130why, one step back
125 if ('bufferedData' in error) {
126 delete error.bufferedData;127 }128129 return error;130};

Improve copying previous Execa errors (#806)ehmickybefore adoption121a052

lines 131–151why, one step back
131
132const copyErrorProperties = (newError, previousError) => {133 for (const propertyName of COPIED_ERROR_PROPERTIES) {134 const descriptor = Object.getOwnPropertyDescriptor(previousError, propertyName);135 if (descriptor !== undefined) {136 Object.defineProperty(newError, propertyName, descriptor);137 }138 }139};140141// Known Node.js-specific error properties142const COPIED_ERROR_PROPERTIES = [143 'code',144 'errno',145 'syscall',146 'path',147 'dest',148 'address',149 'port',150 'info',151];

Fix return value being re-used between multiple calls (#796)ehmickybefore adoption7710440

lines 152–153why, one step back
152
153const previousErrors = new WeakSet();