Improve copying previous Execa errors (#806)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent c999241 · Browse files · 2 files·+73 added−33 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+73 added−33 removed
- tests:+44 added−29 removed
- other:+29 added−4 removed
Provenance
- Commit
121a052- Tree
163360a- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
c999241
2 files changed+73 added−33 removed
modified:lib/error.js+29 added-4 removed
71 unmodified lines727374757676777878798081828083828485868788899091929320 unmodified lines11411511611711811912012112212312412512612712812913013113213313413513613713813971 unmodified lines .map(messagePart => stripFinalNewline(serializeMessagePart(messagePart))) .filter(Boolean) .join('\n\n');
if (Object.prototype.toString.call(error) !== '[object Error]' || previousErrors.has(error)) { if (Object.prototype.toString.call(error) !== '[object Error]') { error = new Error(message); error.originalMessage = originalMessage; } else if (previousErrors.has(error)) { const newError = new Error(message); copyErrorProperties(newError, error); error = newError; } else { previousErrors.add(error); error.message = message; error.originalMessage = originalMessage; }
previousErrors.add(error);
error.shortMessage = shortMessage; error.originalMessage = originalMessage; error.command = command; error.escapedCommand = escapedCommand; error.cwd = cwd;
20 unmodified lines
return error;};
const copyErrorProperties = (newError, previousError) => { for (const propertyName of COPIED_ERROR_PROPERTIES) { const descriptor = Object.getOwnPropertyDescriptor(previousError, propertyName); if (descriptor !== undefined) { Object.defineProperty(newError, propertyName, descriptor); } }};
// Known Node.js-specific error propertiesconst COPIED_ERROR_PROPERTIES = [ 'code', 'errno', 'syscall', 'path', 'dest', 'address', 'port', 'info',];
const previousErrors = new WeakSet();modified:test/error.js+44 added-29 removed
33 unmodified lines34 const error = await t.throwsAsync(execa('fail.js', {...fullStdio, all: true}));35 t.deepEqual(Reflect.ownKeys(error), [36 'stack',37 'message',38- 'originalMessage',38 'shortMessage',39+ 'originalMessage',40 'command',41 'escapedCommand',42 'cwd',43 'failed',309 unmodified lines353 childProcess.emit('error', {message: foobarString});354 await t.throwsAsync(childProcess, {message: new RegExp(foobarString)});355 });356 357-test('error instance can be shared', async t => {358- const originalMessage = foobarString;359- const error = new Error(originalMessage);357+const runAndFail = (t, fixtureName, argument, error) => {358+ const childProcess = execa(fixtureName, [argument]);359+ childProcess.emit('error', error);360+ return t.throwsAsync(childProcess);361+};362+363+const testErrorCopy = async (t, getPreviousArgument) => {364 const fixtureName = 'empty.js';365+ const argument = 'two';366 362- const firstArgument = 'one';363- const childProcess = execa(fixtureName, [firstArgument]);364- childProcess.emit('error', error);365- const firstError = await t.throwsAsync(childProcess);367+ const previousArgument = await getPreviousArgument(t, fixtureName);368+ const previousError = await runAndFail(t, fixtureName, 'foo', previousArgument);369+ const error = await runAndFail(t, fixtureName, argument, previousError);370+ const message = `Command failed: ${fixtureName} ${argument}\n${foobarString}`;371 367- const secondArgument = 'two';368- const secondChildProcess = execa(fixtureName, [secondArgument]);369- secondChildProcess.emit('error', error);370- const secondError = await t.throwsAsync(secondChildProcess);372+ t.not(error, previousError);373+ t.is(error.command, `${fixtureName} ${argument}`);374+ t.is(error.message, message);375+ t.true(error.stack.includes(message));376+ t.is(error.shortMessage, message);377+ t.is(error.originalMessage, foobarString);378+};379 372- const firstCommand = `${fixtureName} ${firstArgument}`;373- const firstMessage = `Command failed: ${firstCommand}\n${foobarString}`;374- t.is(firstError, error);375- t.is(firstError.command, firstCommand);376- t.is(firstError.message, firstMessage);377- t.true(firstError.stack.includes(firstMessage));378- t.is(firstError.shortMessage, firstMessage);379- t.is(firstError.originalMessage, originalMessage);380+test('error instance can be shared', testErrorCopy, () => new Error(foobarString));381+test('error string can be shared', testErrorCopy, () => foobarString);382+test('error copy can be shared', testErrorCopy, (t, fixtureName) => runAndFail(t, fixtureName, 'bar', new Error(foobarString)));383 381- const secondCommand = `${fixtureName} ${secondArgument}`;382- const secondMessage = `Command failed: ${secondCommand}\n${foobarString}`;383- t.not(secondError, error);384- t.is(secondError.command, secondCommand);385- t.is(secondError.message, secondMessage);386- t.true(secondError.stack.includes(secondMessage));387- t.is(secondError.shortMessage, secondMessage);388- t.is(secondError.originalMessage, originalMessage);389-});384+const testErrorCopyProperty = async (t, propertyName, isCopied) => {385+ const propertyValue = 'test';386+ const initialError = new Error(foobarString);387+ initialError[propertyName] = propertyValue;388+389+ const previousError = await runAndFail(t, 'empty.js', 'foo', initialError);390+ t.is(previousError, initialError);391+392+ const error = await runAndFail(t, 'empty.js', 'bar', previousError);393+ t.is(error[propertyName] === propertyValue, isCopied);394+};395+396+test('error.code can be copied', testErrorCopyProperty, 'code', true);397+test('error.errno can be copied', testErrorCopyProperty, 'errno', true);398+test('error.syscall can be copied', testErrorCopyProperty, 'syscall', true);399+test('error.path can be copied', testErrorCopyProperty, 'path', true);400+test('error.dest can be copied', testErrorCopyProperty, 'dest', true);401+test('error.address can be copied', testErrorCopyProperty, 'address', true);402+test('error.port can be copied', testErrorCopyProperty, 'port', true);403+test('error.info can be copied', testErrorCopyProperty, 'info', true);404+test('error.other cannot be copied', testErrorCopyProperty, 'other', false);