Improve `error.message` (#705)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by ehmicky · committed by GitHub · parent 6bbd265 · Browse files · 4 files·+52 added−31 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+52 added−31 removed
- tests:+36 added−25 removed
- other:+16 added−6 removed
Provenance
- Commit
67b476b- Tree
61a99a6- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
6bbd265
4 files changed+52 added−31 removed
modified:lib/error.js+16 added-6 removed
123456718 unmodified lines262728292930313031323334353637383940414243444516 unmodified lines6263646566576768695960707172737430 unmodified linesimport process from 'node:process';import {signalsByName} from 'human-signals';import stripFinalNewline from 'strip-final-newline';import {isUint8Array} from './stdio/utils.js';
const getErrorPrefix = ({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}) => { if (timedOut) {18 unmodified lines
return 'failed';};
const serializeMessagePart = messagePart => isUint8Array(messagePart) ? new TextDecoder().decode(messagePart) : messagePart;const serializeMessagePart = messagePart => { if (typeof messagePart === 'string') { return messagePart; }
if (isUint8Array(messagePart)) { return new TextDecoder().decode(messagePart); }
return '';};
export const makeError = ({ stdio, all, error,16 unmodified lines const prefix = getErrorPrefix({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}); const execaMessage = `Command ${prefix}: ${command}`; const isError = Object.prototype.toString.call(error) === '[object Error]'; const shortMessage = isError ? `${execaMessage}\n${error.message}` : execaMessage; const messageStdio = all === undefined ? [stdio[2], stdio[1]] : [all]; const message = [shortMessage, stdio[2], stdio[1], ...stdio.slice(3)] const message = [shortMessage, ...messageStdio, ...stdio.slice(3)] .map(messagePart => stripFinalNewline(serializeMessagePart(messagePart))) .filter(Boolean) .map(messagePart => serializeMessagePart(messagePart)) .join('\n'); .join('\n\n');
if (isError) { error.originalMessage = error.message; error.message = message;30 unmodified linesadded:test/fixtures/noop-both-fail.js+6 added
123456#!/usr/bin/env nodeimport process from 'node:process';
process.stdout.write(process.argv[2]);process.stderr.write(process.argv[3]);process.exit(1);modified:test/helpers/stdio.js+2 added-2 removed
12345567891010111213143 unmodified linesimport process from 'node:process';
export const identity = value => value;
export const getStdio = (indexOrName, stdioOption) => {export const getStdio = (indexOrName, stdioOption, length) => { if (typeof indexOrName === 'string') { return {[indexOrName]: stdioOption}; }
const stdio = ['pipe', 'pipe', 'pipe']; const stdio = Array.from({length}).fill('pipe'); stdio[indexOrName] = stdioOption; return {stdio};};
3 unmodified linesmodified:test/error.js+28 added-23 removed
1 import process from 'node:process';2 import test from 'ava';3 import {execa, execaSync} from '../index.js';4 import {FIXTURES_DIR, setFixtureDir} from './helpers/fixtures-dir.js';5-import {fullStdio} from './helpers/stdio.js';5+import {fullStdio, getStdio} from './helpers/stdio.js';6 7 const isWindows = process.platform === 'win32';8 9 setFixtureDir();87 unmodified lines97 test('error.message contains the command', async t => {98 await t.throwsAsync(execa('exit.js', ['2', 'foo', 'bar']), {message: /exit.js 2 foo bar/});99 });100 101-const testStdioMessage = async (t, encoding) => {102- const {message} = await t.throwsAsync(execa('echo-fail.js', {...fullStdio, encoding}));103- t.true(message.endsWith('stderr\nstdout\nfd3'));101+const testStdioMessage = async (t, encoding, all) => {102+ const {message} = await t.throwsAsync(execa('echo-fail.js', {...fullStdio, encoding, all}));103+ const output = all ? 'stdout\nstderr' : 'stderr\n\nstdout';104+ t.true(message.endsWith(`echo-fail.js\n\n${output}\n\nfd3`));105 };106 106-test('error.message contains stdout/stderr/stdio if available', testStdioMessage, 'utf8');107-test('error.message contains stdout/stderr/stdio even with encoding "buffer"', testStdioMessage, 'buffer');107+test('error.message contains stdout/stderr/stdio if available', testStdioMessage, 'utf8', false);108+test('error.message contains stdout/stderr/stdio even with encoding "buffer"', testStdioMessage, 'buffer', false);109+test('error.message contains all if available', testStdioMessage, 'utf8', true);110+test('error.message contains all even with encoding "buffer"', testStdioMessage, 'buffer', true);111 109-test('error.message does not contain stdout if not available', async t => {110- const {message} = await t.throwsAsync(execa('echo-fail.js', {stdio: ['pipe', 'ignore', 'pipe', 'pipe']}));111- t.true(message.endsWith('stderr\nfd3'));112-});112+const testPartialIgnoreMessage = async (t, index, stdioOption, output) => {113+ const {message} = await t.throwsAsync(execa('echo-fail.js', getStdio(index, stdioOption, 4)));114+ t.true(message.endsWith(`echo-fail.js\n\n${output}\n\nfd3`));115+};116 114-test('error.message does not contain stderr if not available', async t => {115- const {message} = await t.throwsAsync(execa('echo-fail.js', {stdio: ['pipe', 'pipe', 'ignore', 'pipe']}));116- t.true(message.endsWith('stdout\nfd3'));117-});117+test('error.message does not contain stdout if not available', testPartialIgnoreMessage, 1, 'ignore', 'stderr');118+test('error.message does not contain stderr if not available', testPartialIgnoreMessage, 2, 'ignore', 'stdout');119 119-test('error.message does not contain stdout/stderr/stdio if not available', async t => {120- const {message} = await t.throwsAsync(execa('echo-fail.js', {stdio: 'ignore'}));120+const testFullIgnoreMessage = async (t, options, resultProperty) => {121+ const {[resultProperty]: message} = await t.throwsAsync(execa('echo-fail.js', options));122 t.false(message.includes('stderr'));123 t.false(message.includes('stdout'));124 t.false(message.includes('fd3'));124-});125+};126 126-test('error.shortMessage does not contain stdout/stderr/stdio', async t => {127- const {shortMessage} = await t.throwsAsync(execa('echo-fail.js', fullStdio));128- t.false(shortMessage.includes('stderr'));129- t.false(shortMessage.includes('stdout'));130- t.false(shortMessage.includes('fd3'));131-});127+test('error.message does not contain stdout/stderr/stdio if not available', testFullIgnoreMessage, {stdio: 'ignore'}, 'message');128+test('error.shortMessage does not contain stdout/stderr/stdio', testFullIgnoreMessage, fullStdio, 'shortMessage');129 130+const testErrorMessageConsistent = async (t, stdout) => {131+ const {message} = await t.throwsAsync(execa('noop-both-fail.js', [stdout, 'stderr']));132+ t.true(message.endsWith(`noop-both-fail.js ${stdout} stderr\n\nstderr\n\nstdout`));133+};134+135+test('error.message newlines are consistent - no newline', testErrorMessageConsistent, 'stdout');136+test('error.message newlines are consistent - newline', testErrorMessageConsistent, 'stdout\n');137+138 test('Original error.message is kept', async t => {139 const {originalMessage} = await t.throwsAsync(execa('noop.js', {cwd: 1}));140 t.true(originalMessage.startsWith('The "options.cwd" property must be of type string or an instance of Buffer or URL. Received type number'));141 });139 unmodified lines