Skip to content
Myr

Make error messages more consistent (#230)

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

authored by ehmicky · committed by Sindre Sorhus · parent 4fb924c · Browse files · 3 files+10 added−49 removed
This commit

State

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

Diff stats

3 files+10 added−49 removed

  • other:+10 added−49 removed

3 files+10 added−49 removed

deleted:fixtures/error-message.js-5 removed
modified:index.js+6 added-5 removed
175 unmodified lines
176 const {joinedCommand, timedOut, isCanceled, parsed: {options: {timeout}}} = options;177178 const [exitCodeName, exitCode] = getCode(result, code);179180 if (!(error instanceof Error)) {181 const message = [joinedCommand, stderr, stdout].filter(Boolean).join('\n');180 const prefix = getErrorPrefix({timedOut, timeout, signal, exitCodeName, exitCode, isCanceled});181 const message = `Command ${prefix}: ${joinedCommand}`;182183 if (error instanceof Error) {184 error.message = `${message}${error.message}`;185 } else {186 error = new Error(message);187 }188185 const prefix = getErrorPrefix({timedOut, timeout, signal, exitCodeName, exitCode, isCanceled});186 error.message = `Command ${prefix}: ${error.message}`;187189 error.code = exitCode || exitCodeName;190 error.exitCode = exitCode;191 error.exitCodeName = exitCodeName;192 error.stdout = stdout;
280 unmodified lines
modified:test.js+4 added-39 removed
10 unmodified lines
1112process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;13process.env.FOO = 'foo';1415const NO_NEWLINES_REGEXP = /^[^\n]*$/;16const STDERR_STDOUT_REGEXP = /stderr[^]*stdout/;15const TIMEOUT_REGEXP = /timed out after/;1617const getExitRegExp = exitMessage => new RegExp(`failed with exit code ${exitMessage}`);18
41 unmodified lines
60 t.is(typeof error.stderr, 'string');61 t.is(typeof error.all, 'string');62});6366test('include stdout and stderr in errors for improved debugging', async t => {67 await t.throwsAsync(execa('fixtures/error-message.js'), {message: STDERR_STDOUT_REGEXP, code: 1});68});6970test('do not include in errors when `stdio` is set to `inherit`', async t => {71 await t.throwsAsync(execa('fixtures/error-message.js', {stdio: 'inherit'}), {message: NO_NEWLINES_REGEXP});72});7374test('do not include `stderr` and `stdout` in errors when set to `inherit`', async t => {75 await t.throwsAsync(execa('fixtures/error-message.js', {stdout: 'inherit', stderr: 'inherit'}), {message: NO_NEWLINES_REGEXP});76});7778test('do not include `stderr` and `stdout` in errors when `stdio` is set to `inherit`', async t => {79 await t.throwsAsync(execa('fixtures/error-message.js', {stdio: [undefined, 'inherit', 'inherit']}), {message: NO_NEWLINES_REGEXP});80});8182test('do not include `stdout` in errors when set to `inherit`', async t => {83 const error = await t.throwsAsync(execa('fixtures/error-message.js', {stdout: 'inherit'}), {message: /stderr/});84 t.notRegex(error.message, /stdout/);85});8687test('do not include `stderr` in errors when set to `inherit`', async t => {88 const error = await t.throwsAsync(execa('fixtures/error-message.js', {stderr: 'inherit'}), {message: /stdout/});89 t.notRegex(error.message, /stderr/);90});9164test('pass `stdout` to a file descriptor', async t => {65 const file = tempfile('.txt');66 await execa('fixtures/noop', ['foo bar'], {stdout: fs.openSync(file, 'w')});67 t.is(fs.readFileSync(file, 'utf8'), 'foo bar\n');
48 unmodified lines
116117test('execa.sync() throws error if written to stderr', t => {118 t.throws(() => {119 execa.sync('foo');148 }, process.platform === 'win32' ? /'foo' is not recognized as an internal or external command/ : /spawnSync foo ENOENT/);120 }, process.platform === 'win32' ? /failed with exit code 1/ : /spawnSync foo ENOENT/);121});122151test('execa.sync() includes stdout and stderr in errors for improved debugging', t => {152 t.throws(() => {153 execa.sync('node', ['fixtures/error-message.js']);154 }, {message: STDERR_STDOUT_REGEXP, code: 1});155});156123test('skip throwing when using reject option in execa.sync()', t => {158 const error = execa.sync('node', ['fixtures/error-message.js'], {reject: false});159 t.is(typeof error.stdout, 'string');160 t.is(typeof error.stderr, 'string');124 const error = execa.sync('noop-err', ['foo'], {reject: false});125 t.is(error.stderr, 'foo');126});127128test('stripEof option (legacy)', async t => {129 const {stdout} = await execa('noop', ['foo'], {stripEof: false});
6 unmodified lines
136});137138test('preferLocal option', async t => {139 await execa('ava', ['--version'], {env: {PATH: ''}});175 const errorRegExp = process.platform === 'win32' ? /not recognized/ : /spawn ava ENOENT/;140 const errorRegExp = process.platform === 'win32' ? /failed with exit code 1/ : /spawn ava ENOENT/;141 await t.throwsAsync(execa('ava', ['--version'], {preferLocal: false, env: {PATH: ''}}), errorRegExp);142});143144test('localDir option', async t => {
483 unmodified lines