Fix stack trace workaround (#818)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent fa04e3c · Browse files · 2 files·+14 added−6 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+14 added−6 removed
- tests:+9 added−1 removed
- other:+5 added−5 removed
Provenance
- Commit
be7eb85- Tree
4229b7c- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
fa04e3c
2 files changed+14 added−6 removed
modified:lib/clone.js+5 added-5 removed
17 unmodified lines1819202122222324252625 unmodified lines5253545556565758585960616262636464656667682 unmodified lines17 unmodified lines
const cloneError = (oldError, newMessage) => { const {name, message, stack} = oldError; const error = new Error(newMessage); error.stack = fixStack(name, stack, message, newMessage); error.stack = fixStack(stack, message, newMessage); Object.defineProperty(error, 'name', {value: name, enumerable: false, configurable: true, writable: true}); copyErrorProperties(error, oldError); return error;};25 unmodified lines// Fixes `error.stack` not being updated when it has been already accessed, since it is memoized by V8.// For example, this happens when calling `stream.destroy(error)`.// See https://github.com/nodejs/node/issues/51715const setErrorMessage = (error, newMessage) => { const {name, message, stack} = error; const {message, stack} = error; error.message = newMessage; error.stack = fixStack(name, stack, message, newMessage); error.stack = fixStack(stack, message, newMessage); return error;};
const fixStack = (name, stack, message, newMessage) => stack.includes(newMessage)const fixStack = (stack, message, newMessage) => stack.includes(newMessage) ? stack : stack.replace(`${name}: ${message}`, `${name}: ${newMessage}`); : stack.replace(`: ${message}`, `: ${newMessage}`);
// Two `execa()` calls might return the same error.// So we must close those before directly mutating them.export const isPreviousError = error => previousErrors.has(error);2 unmodified linesmodified:test/clone.js+9 added-1 removed
118 unmodified lines119 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}`));123+ t.true(error.stack.startsWith(`Error: ${error.message}`));124 });125 126+test('error.stack is set even if memoized with an unusual error.name', async t => {127+ const childProcess = execa('empty.js');128+ childProcess.stdin.destroy();129+ const error = await t.throwsAsync(childProcess);130+ t.is(error.message, 'Command failed with ERR_STREAM_PREMATURE_CLOSE: empty.js\nPremature close');131+ t.true(error.stack.startsWith(`Error [ERR_STREAM_PREMATURE_CLOSE]: ${error.message}`));132+});133+134 test('Cloned errors keep the stack trace', async t => {135 const message = 'test';136 const error = new Error(message);137 const stack = error.stack.split('\n').filter(line => line.trim().startsWith('at ')).join('\n');13 unmodified lines