Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by ehmicky · committed by GitHub · parent 9cbb817 · Browse files · 6 files+270 added−146 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

6 files+270 added−146 removed

  • tests:+142 added−83 removed
  • other:+128 added−63 removed

6 files+270 added−146 removed

added:lib/clone.js+70 added
modified:lib/error.js+55 added-62 removed
modified:lib/kill.js+2 added-1 removed
1import {addAbortListener} from 'node:events';2import os from 'node:os';3import {setTimeout} from 'node:timers/promises';4import {onExit} from 'signal-exit';5import {normalizeExitPayload, getErrorPrefix, isErrorInstance} from './error.js';5import {normalizeExitPayload, getErrorPrefix} from './error.js';6import {isErrorInstance} from './clone.js';78const DEFAULT_FORCE_KILL_TIMEOUT = 1000 * 5;910// Monkey-patches `childProcess.kill()` to add `forceKillAfterDelay` behavior and `.kill(error)`
109 unmodified lines
added:test/clone.js+142 added
1import test from 'ava';2import {execa} from '../index.js';3import {setFixtureDir} from './helpers/fixtures-dir.js';4import {foobarString} from './helpers/input.js';56setFixtureDir();78const testUnusualError = async (t, error, expectedOriginalMessage = String(error)) => {9 const childProcess = execa('empty.js');10 childProcess.emit('error', error);11 const {originalMessage, shortMessage, message} = await t.throwsAsync(childProcess);12 t.is(originalMessage, expectedOriginalMessage);13 t.true(shortMessage.includes(expectedOriginalMessage));14 t.is(message, shortMessage);15};1617test('error instance can be null', testUnusualError, null);18test('error instance can be false', testUnusualError, false);19test('error instance can be a string', testUnusualError, 'test');20test('error instance can be a number', testUnusualError, 0);21test('error instance can be a BigInt', testUnusualError, 0n);22test('error instance can be a symbol', testUnusualError, Symbol('test'));23test('error instance can be a function', testUnusualError, () => {});24test('error instance can be an array', testUnusualError, ['test', 'test']);25// eslint-disable-next-line unicorn/error-message26test('error instance can be an error with an empty message', testUnusualError, new Error(''), '');27test('error instance can be undefined', testUnusualError, undefined, '');2829test('error instance can be a plain object', async t => {30 const childProcess = execa('empty.js');31 childProcess.emit('error', {message: foobarString});32 await t.throwsAsync(childProcess, {message: new RegExp(foobarString)});33});3435const runAndFail = (t, fixtureName, argument, error) => {36 const childProcess = execa(fixtureName, [argument]);37 childProcess.emit('error', error);38 return t.throwsAsync(childProcess);39};4041const runAndClone = async (t, initialError) => {42 const previousError = await runAndFail(t, 'empty.js', 'foo', initialError);43 t.is(previousError, initialError);4445 return runAndFail(t, 'empty.js', 'bar', previousError);46};4748const testErrorCopy = async (t, getPreviousArgument, argument = 'two') => {49 const fixtureName = 'empty.js';50 const firstArgument = 'foo';5152 const previousArgument = await getPreviousArgument(t, fixtureName);53 const previousError = await runAndFail(t, fixtureName, firstArgument, previousArgument);54 const error = await runAndFail(t, fixtureName, argument, previousError);55 const message = `Command failed: ${fixtureName} ${argument}\n${foobarString}`;5657 t.not(error, previousError);58 t.is(error.command, `${fixtureName} ${argument}`);59 t.is(error.message, message);60 t.true(error.stack.includes(message));61 t.is(error.stack, previousError.stack.replace(firstArgument, argument));62 t.is(error.shortMessage, message);63 t.is(error.originalMessage, foobarString);64};6566test('error instance can be shared', testErrorCopy, () => new Error(foobarString));67test('error TypeError can be shared', testErrorCopy, () => new TypeError(foobarString));68test('error string can be shared', testErrorCopy, () => foobarString);69test('error copy can be shared', testErrorCopy, (t, fixtureName) => runAndFail(t, fixtureName, 'bar', new Error(foobarString)));70test('error with same message can be shared', testErrorCopy, () => new Error(foobarString), 'foo');7172const testErrorCopyProperty = async (t, propertyName, isCopied) => {73 const propertyValue = 'test';74 const initialError = new Error(foobarString);75 initialError[propertyName] = propertyValue;7677 const error = await runAndClone(t, initialError);78 t.is(error[propertyName] === propertyValue, isCopied);79};8081test('error.code can be copied', testErrorCopyProperty, 'code', true);82test('error.errno can be copied', testErrorCopyProperty, 'errno', true);83test('error.syscall can be copied', testErrorCopyProperty, 'syscall', true);84test('error.path can be copied', testErrorCopyProperty, 'path', true);85test('error.dest can be copied', testErrorCopyProperty, 'dest', true);86test('error.address can be copied', testErrorCopyProperty, 'address', true);87test('error.port can be copied', testErrorCopyProperty, 'port', true);88test('error.info can be copied', testErrorCopyProperty, 'info', true);89test('error.other cannot be copied', testErrorCopyProperty, 'other', false);9091test('error.name can be copied', async t => {92 const initialError = new TypeError('test');93 const error = await runAndClone(t, initialError);94 t.deepEqual(Object.getOwnPropertyDescriptor(Object.getPrototypeOf(initialError), 'name'), Object.getOwnPropertyDescriptor(error, 'name'));95});9697test('error.cause can be copied', async t => {98 const initialError = new Error('test', {cause: new Error('innerTest')});99 const error = await runAndClone(t, initialError);100 t.deepEqual(Object.getOwnPropertyDescriptor(initialError, 'cause'), Object.getOwnPropertyDescriptor(error, 'cause'));101});102103test('error.errors can be copied', async t => {104 const initialError = new AggregateError([], 'test');105 const error = await runAndClone(t, initialError);106 t.deepEqual(Object.getOwnPropertyDescriptor(initialError, 'errors'), Object.getOwnPropertyDescriptor(error, 'errors'));107});108109test('error.stack is set even if memoized', async t => {110 const message = 'test';111 const error = new Error(message);112 t.is(typeof error.stack, 'string');113114 const newMessage = 'newTest';115 error.message = newMessage;116 t.false(error.stack.includes(newMessage));117 error.message = message;118119 const childProcess = execa('empty.js');120 childProcess.emit('error', error);121 t.is(await t.throwsAsync(childProcess), error);122 t.is(error.message, `Command failed: empty.js\n${message}`);123 t.true(error.stack.startsWith(`Error: Command failed: empty.js\n${message}`));124});125126test('Cloned errors keep the stack trace', async t => {127 const message = 'test';128 const error = new Error(message);129 const stack = error.stack.split('\n').filter(line => line.trim().startsWith('at ')).join('\n');130131 const childProcess = execa('empty.js');132 childProcess.emit('error', error);133 t.is(await t.throwsAsync(childProcess), error);134135 const secondChildProcess = execa('empty.js');136 secondChildProcess.emit('error', error);137 const secondError = await t.throwsAsync(secondChildProcess);138 t.not(secondError, error);139 t.is(secondError.message, `Command failed: empty.js\n${message}`);140 t.is(secondError.stack, `Error: Command failed: empty.js\n${message}\n${stack}`);141});142
modified:test/error.js-83 removed
2 unmodified lines
3import {execa, execaSync} from '../index.js';4import {setFixtureDir} from './helpers/fixtures-dir.js';5import {fullStdio, getStdio} from './helpers/stdio.js';6import {noopGenerator, outputObjectGenerator} from './helpers/generator.js';7import {foobarString} from './helpers/input.js';78const isWindows = process.platform === 'win32';910setFixtureDir();
316 unmodified lines
327test('error.code is defined on failure if applicable', async t => {328 const {code} = await t.throwsAsync(execa('noop.js', {uid: true}));329 t.is(code, 'ERR_INVALID_ARG_TYPE');330});332333const testUnusualError = async (t, error) => {334 const childProcess = execa('empty.js');335 childProcess.emit('error', error);336 const {originalMessage, shortMessage, message} = await t.throwsAsync(childProcess);337 t.is(originalMessage, String(error));338 t.true(shortMessage.includes(String(error)));339 t.is(message, shortMessage);340};341342test('error instance can be null', testUnusualError, null);343test('error instance can be false', testUnusualError, false);344test('error instance can be a string', testUnusualError, 'test');345test('error instance can be a number', testUnusualError, 0);346test('error instance can be a BigInt', testUnusualError, 0n);347test('error instance can be a symbol', testUnusualError, Symbol('test'));348test('error instance can be a function', testUnusualError, () => {});349test('error instance can be an array', testUnusualError, ['test', 'test']);350351test('error instance can be undefined', async t => {352 const childProcess = execa('empty.js');353 childProcess.emit('error');354 const {originalMessage, shortMessage, message} = await t.throwsAsync(childProcess);355 t.is(originalMessage, '');356 t.is(shortMessage, 'Command failed: empty.js');357 t.is(message, shortMessage);358});359360test('error instance can be a plain object', async t => {361 const childProcess = execa('empty.js');362 childProcess.emit('error', {message: foobarString});363 await t.throwsAsync(childProcess, {message: new RegExp(foobarString)});364});365366const runAndFail = (t, fixtureName, argument, error) => {367 const childProcess = execa(fixtureName, [argument]);368 childProcess.emit('error', error);369 return t.throwsAsync(childProcess);370};371372const testErrorCopy = async (t, getPreviousArgument) => {373 const fixtureName = 'empty.js';374 const argument = 'two';375376 const previousArgument = await getPreviousArgument(t, fixtureName);377 const previousError = await runAndFail(t, fixtureName, 'foo', previousArgument);378 const error = await runAndFail(t, fixtureName, argument, previousError);379 const message = `Command failed: ${fixtureName} ${argument}\n${foobarString}`;380381 t.not(error, previousError);382 t.is(error.command, `${fixtureName} ${argument}`);383 t.is(error.message, message);384 t.true(error.stack.includes(message));385 t.is(error.shortMessage, message);386 t.is(error.originalMessage, foobarString);387};388389test('error instance can be shared', testErrorCopy, () => new Error(foobarString));390test('error string can be shared', testErrorCopy, () => foobarString);391test('error copy can be shared', testErrorCopy, (t, fixtureName) => runAndFail(t, fixtureName, 'bar', new Error(foobarString)));392393const testErrorCopyProperty = async (t, propertyName, isCopied) => {394 const propertyValue = 'test';395 const initialError = new Error(foobarString);396 initialError[propertyName] = propertyValue;397398 const previousError = await runAndFail(t, 'empty.js', 'foo', initialError);399 t.is(previousError, initialError);400401 const error = await runAndFail(t, 'empty.js', 'bar', previousError);402 t.is(error[propertyName] === propertyValue, isCopied);403};404405test('error.code can be copied', testErrorCopyProperty, 'code', true);406test('error.errno can be copied', testErrorCopyProperty, 'errno', true);407test('error.syscall can be copied', testErrorCopyProperty, 'syscall', true);408test('error.path can be copied', testErrorCopyProperty, 'path', true);409test('error.dest can be copied', testErrorCopyProperty, 'dest', true);410test('error.address can be copied', testErrorCopyProperty, 'address', true);411test('error.port can be copied', testErrorCopyProperty, 'port', true);412test('error.info can be copied', testErrorCopyProperty, 'info', true);413test('error.other cannot be copied', testErrorCopyProperty, 'other', false);
modified:package.json+1 added
78 unmodified lines
79 ]80 },81 "ava": {82 "workerThreads": false,83 "concurrency": 1,84 "timeout": "60s"85 },86 "xo": {87 "rules": {
5 unmodified lines