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 fileTake 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 back1import {signalsByName} from 'human-signals';Improve `error.message` (#705)ehmickybefore adoption67b476b
line 2why, one step back2import stripFinalNewline from 'strip-final-newline';Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
line 3why, one step back3import {isBinary, binaryToString} from './stdio/utils.js';Improve `cwd` option (#803)ehmickybefore adoption2fd9351
line 4why, one step back4import {fixCwdError} from './cwd.js';Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
line 5why, one step back5Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 6why, one step back6export const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => {Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
line 7why, one step back7 if (timedOut) {Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 8why, one step back8 return `Command timed out after ${timeout} milliseconds`;Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
lines 9–11why, one step back9 }1011 if (isCanceled) {Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 12why, one step back12 return 'Command was canceled';Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
lines 13–15why, one step back13 }1415 if (errorCode !== undefined) {Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 16why, one step back16 return `Command failed with ${errorCode}`;Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
lines 17–19why, one step back17 }1819 if (signal !== undefined) {Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 20why, one step back20 return `Command was killed with ${signal} (${signalDescription})`;Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
lines 21–23why, one step back21 }2223 if (exitCode !== undefined) {Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 24why, one step back24 return `Command failed with exit code ${exitCode}`;Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
lines 25–26why, one step back25 }26Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 27why, one step back27 return 'Command failed';Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
lines 28–29why, one step back28};29Improve process failure logic (#807)ehmickybefore adoption8ff263c
lines 30–49why, one step back30const 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};49Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 50–56why, one step back50const 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 back57 }Fix `error.message` when using `encoding: 'buffer'` (#701)ehmickybefore adoptionb8c1f55
line 58why, one step back58Allow any type with transforms (#709)ehmickybefore adoptioned8b2ce
lines 59–60why, one step back59 if (isBinary(messageItem)) {60 return binaryToString(messageItem);Improve `error.message` (#705)ehmickybefore adoption67b476b
lines 61–65why, one step back61 }6263 return '';64};65Require Node.js 12.20 and move to ESM (#478)Minh Nguyenbefore adoption7707880
line 66why, one step back66export const makeError = ({Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
line 67why, one step back67 stdio,Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
line 68why, one step back68 all,Improve process failure logic (#807)ehmickybefore adoption8ff263c
lines 69–71why, one step back69 error: rawError,70 signal: rawSignal,71 exitCode: rawExitCode,Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
line 72why, one step back72 command,Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 73why, one step back73 escapedCommand,Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
lines 74–75why, one step back74 timedOut,75 isCanceled,Improve process failure logic (#807)ehmickybefore adoption8ff263c
line 76why, one step back76 options: {timeoutDuration, timeout = timeoutDuration, cwd},Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
line 77why, one step back77}) => {Improve process failure logic (#807)ehmickybefore adoption8ff263c
lines 78–79why, one step back78 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 back80 const errorCode = error?.code;Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
line 81why, one step back81 const prefix = getErrorPrefix({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled});Improve process failure logic (#807)ehmickybefore adoption8ff263c
lines 82–83why, one step back82 const {originalMessage, suffix} = getErrorSuffix(error, cwd);83 const shortMessage = `${prefix}: ${command}${suffix}`;Improve `error.message` (#705)ehmickybefore adoption67b476b
lines 84–86why, one step back84 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 back87 .filter(Boolean)Improve `error.message` (#705)ehmickybefore adoption67b476b
line 88why, one step back88 .join('\n\n');Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
line 89why, one step back89Improve copying previous Execa errors (#806)ehmickybefore adoption121a052
line 90why, one step back90 if (Object.prototype.toString.call(error) !== '[object Error]') {Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
line 91why, one step back91 error = new Error(message);Improve copying previous Execa errors (#806)ehmickybefore adoption121a052
lines 92–95why, one step back92 } 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 back96 } else {97 error.message = message;Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
lines 98–99why, one step back98 }99Improve copying previous Execa errors (#806)ehmickybefore adoption121a052
lines 100–101why, one step back100 previousErrors.add(error);101Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
line 102why, one step back102 error.shortMessage = shortMessage;Improve copying previous Execa errors (#806)ehmickybefore adoption121a052
line 103why, one step back103 error.originalMessage = originalMessage;Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
line 104why, one step back104 error.command = command;Add `.escapedCommand` property (#466)ehmickybefore adoption712bafc
line 105why, one step back105 error.escapedCommand = escapedCommand;Re-order return value properties (#801)ehmickybefore adoption72836f3
lines 106–111why, one step back106 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 back112 error.exitCode = exitCode;113 error.signal = signal;114 error.signalDescription = signalDescription;Re-order return value properties (#801)ehmickybefore adoption72836f3
line 115why, one step back115Add `result.stdio` and `error.stdio` (#676)ehmickybefore adoption62a5207
lines 116–117why, one step back116 error.stdout = stdio[1];117 error.stderr = stdio[2];Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
lines 118–122why, one step back118119 if (all !== undefined) {120 error.all = all;121 }122Re-order return value properties (#801)ehmickybefore adoption72836f3
lines 123–124why, one step back123 error.stdio = stdio;124Rename `master` branch to `main`Sindre Sorhusbefore adoptionfd3c7be
lines 125–130why, one step back125 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 back131132const 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 back152153const previousErrors = new WeakSet();