Skip to content

Add error.originalMessage property #373

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 8, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ declare namespace execa {
The signal that was used to terminate the process.
*/
signal?: string;

/**
Original error message when the child process exits due to an error or a timeout.
*/
originalMessage?: string;
}

interface ExecaSyncReturnValue<StdoutErrorType = string>
Expand Down
4 changes: 4 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ try {
expectType<boolean>(unicornsResult.isCanceled);
expectType<boolean>(unicornsResult.killed);
expectType<string | undefined>(unicornsResult.signal);
expectType<string | undefined>(unicornsResult.originalMessage);
} catch (error) {
const execaError: ExecaError = error;

Expand All @@ -40,6 +41,7 @@ try {
expectType<boolean>(execaError.isCanceled);
expectType<boolean>(execaError.killed);
expectType<string | undefined>(execaError.signal);
expectType<string | undefined>(execaError.originalMessage);
}

try {
Expand All @@ -55,6 +57,7 @@ try {
expectError(unicornsResult.isCanceled);
expectType<boolean>(unicornsResult.killed);
expectType<string | undefined>(unicornsResult.signal);
expectType<string | undefined>(unicornsResult.originalMessage);
} catch (error) {
const execaError: ExecaSyncError = error;

Expand All @@ -69,6 +72,7 @@ try {
expectError(execaError.isCanceled);
expectType<boolean>(execaError.killed);
expectType<string | undefined>(execaError.signal);
expectType<string | undefined>(execaError.originalMessage);
}

execa('unicorns', {cleanup: false});
Expand Down
1 change: 1 addition & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const makeError = ({
const message = `Command ${prefix}: ${command}`;

if (error instanceof Error) {
error.originalMessage = error.message;
error.message = `${message}\n${error.message}`;
} else {
error = new Error(message);
Expand Down
6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,12 @@ Type: `string | undefined`

The signal that was used to terminate the process.

#### originalMessage

Type: `string | undefined`

Original error message when the child process exits due to an error or a timeout.

### options

Type: `object`
Expand Down
5 changes: 5 additions & 0 deletions test/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,8 @@ errorMessage.title = (message, expected) => `error.message matches: ${expected}`

test(errorMessage, /Command failed with exit code 2.*: exit 2 foo bar/, 2, 'foo', 'bar');
test(errorMessage, /Command failed with exit code 3.*: exit 3 baz quz/, 3, 'baz', 'quz');

test('Original error message is kept', async t => {
const {originalMessage} = await t.throwsAsync(execa('wrong command'));
t.is(originalMessage, 'spawn wrong command ENOENT');
});