Add stdout/stderr to error messages (#397)
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 09cd28a · Browse files · 6 files·+62 added−11 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+62 added−11 removed
- tests:+24 added−1 removed
- other:+38 added−10 removed
Provenance
- Commit
bc520f9- Tree
5f33a35- Author
ehmicky <ehmicky@users.noreply.github.com>- Committer
Sindre Sorhus <sindresorhus@gmail.com>- Parent
09cd28a
6 files changed+62 added−11 removed
modified:lib/error.js+5 added-2 removed
45 unmodified lines4647484950505152535455545657585960616263646520 unmodified lines45 unmodified lines
const errorCode = error && error.code;
const prefix = getErrorPrefix({timedOut, timeout, errorCode, signal, signalDescription, exitCode, isCanceled}); const message = `Command ${prefix}: ${command}`; const execaMessage = `Command ${prefix}: ${command}`; const shortMessage = error instanceof Error ? `${execaMessage}\n${error.message}` : execaMessage; const message = [shortMessage, stderr, stdout].filter(Boolean).join('\n');
if (error instanceof Error) { error.originalMessage = error.message; error.message = `${message}\n${error.message}`; error.message = message; } else { error = new Error(message); }
error.shortMessage = shortMessage; error.command = command; error.exitCode = exitCode; error.signal = signal; error.signalDescription = signalDescription;20 unmodified linesadded:test/fixtures/echo-fail+5 added
1+#!/usr/bin/env node2+'use strict';3+console.log('stdout');4+console.error('stderr');5+process.exit(1);modified:test/error.js+19 added-1 removed
49 unmodified lines50 test('exitCode is 3', testExitCode, 3);51 test('exitCode is 4', testExitCode, 4);52 53 test('error.message contains the command', async t => {54- await t.throwsAsync(execa('exit', ['2', 'foo', 'bar'], {message: /exit 2 foo bar/}));54+ await t.throwsAsync(execa('exit', ['2', 'foo', 'bar']), {message: /exit 2 foo bar/});55 });56 57+test('error.message contains stdout/stderr if available', async t => {58+ const {message} = await t.throwsAsync(execa('echo-fail'));59+ t.true(message.includes('stderr'));60+ t.true(message.includes('stdout'));61+});62+63+test('error.message does not contain stdout/stderr if not available', async t => {64+ const {message} = await t.throwsAsync(execa('echo-fail', {stdio: 'ignore'}));65+ t.false(message.includes('stderr'));66+ t.false(message.includes('stdout'));67+});68+69+test('error.shortMessage does not contain stdout/stderr', async t => {70+ const {shortMessage} = await t.throwsAsync(execa('echo-fail'));71+ t.false(shortMessage.includes('stderr'));72+ t.false(shortMessage.includes('stdout'));73+});74+75 test('Original error.message is kept', async t => {76 const {originalMessage} = await t.throwsAsync(execa('wrong command'));77 t.is(originalMessage, 'spawn wrong command ENOENT');78 });132 unmodified linesmodified:index.d.ts+11 added-4 removed
42 unmodified lines43 44 This can be either an absolute path or a path relative to the `cwd` option.45 46 Requires `preferLocal` to be `true`.47- 47+48 For example, this can be used together with [`get-node`](https://github.com/ehmicky/get-node) to run a specific Node.js version in a child process.49 50 @default process.execPath51 */283 unmodified lines335 interface ExecaSyncError<StdoutErrorType = string>336 extends Error,337 ExecaReturnBase<StdoutErrorType> {338 /**339- The error message.339+ Error message when the child process failed to run. In addition to the underlying error message, it also contains some information related to why the child process errored.340+341+ The child process stderr then stdout are appended to the end, separated with newlines and not interleaved.342 */343 message: string;344 345 /**344- Original error message. This is `undefined` unless the child process exited due to an `error` event or a timeout.346+ This is the same as the `message` property except it does not include the child process stdout/stderr.347+ */348+ shortMessage: string;349 346- The `message` property contains both the `originalMessage` and some additional information added by Execa.350+ /**351+ Original error message. This is the same as the `message` property except it includes neither the child process stdout/stderr nor some additional information added by Execa.352+353+ This is `undefined` unless the child process exited due to an `error` event or a timeout.354 */355 originalMessage?: string;356 }357 192 unmodified linesmodified:index.test-d.ts+2 added
38 unmodified lines39 expectType<boolean>(execaError.isCanceled);40 expectType<boolean>(execaError.killed);41 expectType<string | undefined>(execaError.signal);42 expectType<string | undefined>(execaError.signalDescription);43+ expectType<string>(execaError.shortMessage);44 expectType<string | undefined>(execaError.originalMessage);45 }46 47 try {22 unmodified lines70 expectError(execaError.isCanceled);71 expectType<boolean>(execaError.killed);72 expectType<string | undefined>(execaError.signal);73 expectType<string | undefined>(execaError.signalDescription);74+ expectType<string>(execaError.shortMessage);75 expectType<string | undefined>(execaError.originalMessage);76 }77 78 execa('unicorns', {cleanup: false});113 unmodified linesmodified:readme.md+20 added-4 removed
58 unmodified lines59 console.log(error);60 /*61 {62 message: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',63- errno: 'ENOENT',63+ errno: -2,64 code: 'ENOENT',65 syscall: 'spawn unknown',66 path: 'unknown',67 spawnargs: ['command'],68 originalMessage: 'spawn unknown ENOENT',69+ shortMessage: 'Command failed with ENOENT: unknown command spawn unknown ENOENT',70 command: 'unknown command',71 stdout: '',72 stderr: '',73 all: '',38 unmodified lines112 console.log(error);113 /*114 {115 message: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',115- errno: 'ENOENT',116+ errno: -2,117 code: 'ENOENT',118 syscall: 'spawnSync unknown',119 path: 'unknown',120 spawnargs: ['command'],121 originalMessage: 'spawnSync unknown ENOENT',122+ shortMessage: 'Command failed with ENOENT: unknown command spawnSync unknown ENOENT',123 command: 'unknown command',124 stdout: '',125 stderr: '',126 all: '',176 unmodified lines303 A human-friendly description of the signal that was used to terminate the process. For example, `Floating point arithmetic error`.304 305 If a signal terminated the process, this property is defined and included in the error message. Otherwise it is `undefined`. It is also `undefined` when the signal is very uncommon which should seldomly happen.306 307+#### message308+309+Type: `string`310+311+Error message when the child process failed to run. In addition to the [underlying error message](#originalMessage), it also contains some information related to why the child process errored.312+313+The child process [stderr](#stderr) then [stdout](#stdout) are appended to the end, separated with newlines and not interleaved.314+315+#### shortMessage316+317+Type: `string`318+319+This is the same as the [`message` property](#message) except it does not include the child process stdout/stderr.320+321 #### originalMessage322 323 Type: `string | undefined`324 309-Original error message. This is `undefined` unless the child process exited due to an `error` event or a timeout.325+Original error message. This is the same as the `message` property except it includes neither the child process stdout/stderr nor some additional information added by Execa.326 311-The `message` property contains both the `originalMessage` and some additional information added by Execa.327+This is `undefined` unless the child process exited due to an `error` event or a timeout.328 329 ### options330 331 Type: `object`318 unmodified lines