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
Provenance
- Commit
5f8bd52- Tree
967e635- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
4fb924c
3 files changed+10 added−49 removed
deleted:fixtures/error-message.js-5 removed
12345#!/usr/bin/env node'use strict';console.log('stdout');console.error('stderr');process.exit(1);modified:index.js+6 added-5 removed
175 unmodified lines176 const {joinedCommand, timedOut, isCanceled, parsed: {options: {timeout}}} = options;177 178 const [exitCodeName, exitCode] = getCode(result, code);179 180- 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}`;182+183+ if (error instanceof Error) {184+ error.message = `${message}${error.message}`;185+ } else {186 error = new Error(message);187 }188 185- const prefix = getErrorPrefix({timedOut, timeout, signal, exitCodeName, exitCode, isCanceled});186- error.message = `Command ${prefix}: ${error.message}`;187-189 error.code = exitCode || exitCodeName;190 error.exitCode = exitCode;191 error.exitCodeName = exitCodeName;192 error.stdout = stdout;280 unmodified linesmodified:test.js+4 added-39 removed
10 unmodified lines11 12 process.env.PATH = path.join(__dirname, 'fixtures') + path.delimiter + process.env.PATH;13 process.env.FOO = 'foo';14 15-const NO_NEWLINES_REGEXP = /^[^\n]*$/;16-const STDERR_STDOUT_REGEXP = /stderr[^]*stdout/;15 const TIMEOUT_REGEXP = /timed out after/;16 17 const getExitRegExp = exitMessage => new RegExp(`failed with exit code ${exitMessage}`);18 41 unmodified lines60 t.is(typeof error.stderr, 'string');61 t.is(typeof error.all, 'string');62 });63 66-test('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-});69-70-test('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-});73-74-test('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-});77-78-test('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-});81-82-test('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-});86-87-test('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-});91-64 test('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 lines116 117 test('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 });122 151-test('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-});156-123 test('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 });127 128 test('stripEof option (legacy)', async t => {129 const {stdout} = await execa('noop', ['foo'], {stripEof: false});6 unmodified lines136 });137 138 test('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 });143 144 test('localDir option', async t => {483 unmodified lines